Monday, 24 October 2016

Why Scala ?

As in my daily job all of my colleague ask me why scala ? 

Every time me telling them few things about scala, which I remember at that time, so finally I'm collecting  reason why scala ?
I will go adding into list, as I get know more Why Scala.

  • Mix of Functional and OO Programming
  • Strongly typed 
  • Strong Integration with Java
  • JVM Language
  • Rich collection API
  • Awesome syntax
  • REPL (Experient Orient Development)
    • Read 
    • Eval
    • Print
    • Loop  

Sunday, 2 October 2016

Beauty of Scala (QuickSort in two line) that is just Awesome


package programming

object QuickSort extends App {

  quickSort(Array(8, 5, 6, 9, 1, 7, 2)).foreach(println)

  def quickSort(a:Array[Int]):Array[Int] =
    if(a.length < 2)  a else
    (quickSort(a.filter(_< a(a.length-1))) :+ a(a.length-1)) ++ quickSort(a.filter(_> a(a.length-1)))
 
}

Saturday, 23 July 2016

CQRS (Command Query Responsibility Segregation)

CQRS

While reading handling exception in scala I came know CQRS.
While digging into CQRS found best article by Martin Fowler from ThoughtWorks.
It stands for  Command Query Responsibility Segregation pattern.
Basically it means separate your Create, Update, Delete operation (Command) from read (query).
But it seems more complicated as compare to daily CRUD model.
It has its won importance.
Following two figures show regular  CRUD model and CQRS model by Martin Fowler.

Regular CRUD Model


CQRS Model


Thursday, 14 July 2016

Java RMI (Remote Method Invocation)

As part of my current project, we have oracle as database and there is C++ component which talks to oracle 11g server. 
We have our Java apps that needs to talk this C++ Compound for DB Operation.
C++ Compound has API for CRUD operation.
We need to talks this C++ component using java.
First option comes in mind RMI.
This how we call our C++ component using RMI server.
We have RMI server installed on provide port.
Our Java apps talks to RMI server.
RMI server talks to C++ component using JNI. 

Following figure shows our architecture



Tuesday, 31 May 2016

Shorten ssh

This is very helpful for me at least, every time I was connecting to Amazon's AWS I need to do something like below 


ssh -i key.pem ubuntu@abc.compute.amazonaws.com

To run every time I have to remember the hostName, where is my key.pem file. 
To avoid all above, one way to edit ~/.ssh/config file.
So I want something below to connect to aws instance.

ssh awshost

To work above I did something below

mkdir -p ~/.ssh
cp ~/Downloads/key.pem ~/.ssh/
chmod 400 ~/.ssh/key.pem
chmod 700 ~/.ssh

After this edit ~/.ssh/config file, add following entry to config file

Host awshost
HostName abc.compute.amazonaws.com
User ubuntu
IdentityFile "~/.ssh/key.pem"

Save the file.
After doing this 

ssh awshost 

should connect to AWS instance.
Example of above how it is useful.

  1. Copy file from/to remote instance 
     scp -i key.pem hello.txt ubuntu@abc.compute.amazonaws.com:~/

     scp hello.tx awshost:~/



Saturday, 30 April 2016

Password Hashing In scala Using BCrypt

There are multiple option to hash password, But BCrypt I found secure and keep you away from brute force attack.

Why not just use good  MD5, SHA1 or SHA256?

MD5, SHA1 and SHA256 was not created for password hashing to begin with. They were created to calculate a digest for a huge amount of data where performance is high priority. This means that they are perfect for ensuring the integrity of data but very bad for hashing passwords. Because they are fast by nature means that hashed passwords can be cracked using brute force attacks very easily.

Why BCrypt ?

Bcrypt has a built in work factor which gives you the possibility to determine how expensive the hash function has to be. This means that you can make the hashing function slower while computers gets faster.
It will be a nightmare for a potential attacker to use brute force to crack your passwords if they get hold of a database dump.

How user it in Play 2.x ?
Add a dependency in build.sbt for jbcrypt:

name := """Play-Neo4j"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  "org.mindrot" % "jbcrypt" % "0.3m",
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"


Sample Code  (HashingPassword.scala)

package com.LifeTweet.example

import org.mindrot.jbcrypt.BCrypt

/**  * Created by sumit on 4/30/16.  *  Hashing Password Using BCrypt  *  */object HashingPassword {
  def getHash(str: String) : String = {
    BCrypt.hashpw(str, BCrypt.gensalt())
  }
  def checkHash(str: String, strHashed: String): Boolean = {
    BCrypt.checkpw(str,strHashed)
  }
}


Tuesday, 26 April 2016

Scala implicit Precedence

While scala compiler look for implicit, takes following precedence

1) First look in current scope

    implicit val n: Int = 4
    def add(x: Int)(implicit y: Int) = x+y 


2) Looks into import
3) Looks into wildcard  import
4) Find in companion Object

If found two matching implicit in above scope compiler will give error.

Tuesday, 19 April 2016

How to update Java Time Zone ?

1) To Download latest tzupdater.zip, refer following URL    http://www.oracle.com/technetwork/java/javase/downloads/tzupdater-download-513681.html.

2) Unzip downloade zip file.

3) Run the below command (Unzip_File_Path : location where you have unziped the zip file).

java -jar Unzip_File_Path/tzupdater.jar -u

Tuesday, 19 January 2016

The X-Frame-Options response header


This response header used to indicate whether a page should be displayed in
<frame>
<iframe>
<object>

There are three possible values for X-Frame-Options:

DENY
    The page cannot be displayed in a frame, regardless of the site attempting to do so.
SAMEORIGIN
    The page can only be displayed in a frame on the same origin as the page itself.
ALLOW-FROM uri
    The page can only be displayed in a frame on the specified origin.


We can configure serever to set  header X-Frame-Option for all pages.
For PlayFramework we have to use Filters.