Sunday, November 16, 2014

Web Services - SOAP vs REST

SOAP and REST have been widely used for web services. SOAP is developed by Microsoft in 1998. Due to the complexity, REST has been introduced in 2006. Below table is the differentiation:


SOAP
REST
Transport Protocol
HTTP
Support
Support
TPC
Support
-
SMTP
Support
-
MQ
Support
-
IIOP
Support
-
Security
HTTPS
Support
Support
WS-Security (SOAP Security Extension)
Support
-
Output Format
XML
Support
Support
JSON
-
Support
MINE
-
Support
Other
Standards Based
Yes (Based on WS-* Specification)
No (Using HTTP Verbs POST, HEAD, GET, PUT and DELETE)
Caching
No
Yes (GET operations can be cached)
Performance
Good
Better (because of caching)
Simplicity
No
Yes
File Transfer
-
-
Who is using?
Google seems to be consistent in implementing their web services to use SOAP, with the exception of Blogger, which uses XML-RPC. You will find SOAP web services in lots of enterprises software as well.
All of Yahoo’s web services use REST, including Flickr. Both eBay and Amazon have web services for both REST and SOAP.

Tuesday, November 4, 2014

Using Scala Recursion Functions For Permutation Calculation

Permutation is an ordered combination - how many possible ordered combination.

There are 2 types of Permutation:


  1. Permutation with Repeat is Allowed

    Permutation Lock is an example of Repeat Allowed.

    There are 10 numbers to choose from (0,1,...9) and we choose 3 of them.

    10 x 10 x 10 = 1000 permutation


    Formula: nwhere n is the number of things to choose from, and r 
    is number of times
  2. Permutation with No Repeat



    A good example is lottery which number can not be repeat.

    There are 3 numbers to choose from (1,2 and 3) and we choose 3 of them.

    3 x 2 x 1 = 6 permutation

    Formula: Without repetition our choices get reduced each time.

But how do we write that mathematically? Answer: we use the "factorial function".
I am going to use recursion method to write factorial function. This program accepts 2 integer inputs (Number of things to choose from and Number of times to choose) and calculate how many permutation (possible combination) with not repeat.

object Permutations {
  def main(args: Array[String]) {
    print("How many lottery number? ")
    val num1 = readInt()
    println()
    print("How many lottery  number to pick? ")
    val num2 = readInt()
    println("Total Permutation: " + factorial(num1,num2).toString)
  }
  
  def factorial(x: BigInt, y: BigInt) : BigInt = {
    if (y > 1)
      x * factorial( x - 1, y - 1)
    else
      x
  }
}








  1. Tips: Remember to has exist call on recursive function to avoid unstopped loop


Sunday, November 2, 2014

MongoDB / Salat plugin for Play 2.3


Here is complete installation steps on Salat plugin for Play 2.3:

  1. You modify your project/Build.scala:
    import sbt._
    import Keys._
    import play.Play.autoImport._
    import PlayKeys._
    import play.twirl.sbt.Import.TwirlKeys
    
    object ApplicationBuild extends Build {
      val appName = "Playframework"
      val appVersion = "2.3.6"
      
      val appDependencies = Seq(
          "se.radley" %% "play-plugins-salat" % "1.5.0"
      )
    
      val main = Project(appName, file(".")).enablePlugins(play.PlayScala).settings(
        version := appVersion,
        libraryDependencies ++= appDependencies,
        routesImport += "se.radley.plugin.salat.Binders._",
        TwirlKeys.templateImports += "org.bson.types.ObjectId"
      )
    }
  2. Creating or appending conf/play.plugins
    500:se.radley.plugin.salat.SalatPlugin
  3. Continue edit conf/application.conf
    dbplugin = disabled
    evolutionplugin = disabled
    ehcacheplugin = disabled
    
    mongodb.default.db = "mydb"
    
    # Optional values
    #mongodb.default.host = "127.0.0.1"
    #mongodb.default.port = 27017
    #mongodb.default.user = "leon"
    #mongodb.default.password = "123456"
    
    # MongoURI
    # ~~~~~
    # a MongoURI can also be used http://www.mongodb.org/display/DOCS/Connections
    # mongodb.default.uri = "mongodb://127.0.0.1:27017,mongodb.org:1337/salat-test"
    
    # WriteConcern
    # ~~~~~
    # Can be any of the following
    #
    # fsyncsafe - Exceptions are raised for network issues and server errors; Write operations wait for the server to flush data to disk.
    # replicassafe - Exceptions are raised for network issues and server errors; waits for at least 2 servers for the write operation.
    # safe - Exceptions are raised for network issues and server errors; waits on a server for the write operation.
    # normal - Exceptions are raised for network issues but not server errors.
    
    #mongodb.default.writeconcern = "safe"
    
    # Replica sets
    # ~~~~~
    # http://www.mongodb.org/display/DOCS/Why+Replica+Sets
    #
    # To user a replicaset instead of a single host, omit optional values and use the configuration below instead.
    # Since replica sets use public key authentication, user and password won't work together with the replicaset option.
    
    #mongodb.default.replicaset {
    #    host1.host = "10.0.0.1"
    #
    #    host2.host = "10.0.0.2"
    #    host2.port = 27018
    #}
    
    # Mongo Options
    # ~~~~~
    # http://api.mongodb.org/java/2.8.0/com/mongodb/MongoOptions.html
    #
    # For passing custom options to the MongoConnection add the properties under "options". Add just the ones which are different from defaults.
    
    #mongodb.default.options {
    #    connectionsPerHost = 100
    #    threadsAllowedToBlockForConnectionMultiplier = 1000
    #    connectTimeout = 60000
    #}
    
  4. Create app/models/mongoContext.scala
    package models
    
    import com.novus.salat.dao._
    import com.novus.salat.annotations._
    import com.mongodb.casbah.Imports._
    import com.novus.salat.{TypeHintFrequency, StringTypeHintStrategy, Context}
    import play.api.Play
    import play.api.Play.current
    
    package object mongoContext {
      implicit val context = {
        val context = new Context {
          val name = "global"
          override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
        }
        context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id")
        context.registerClassLoader(Play.classloader)
        context
      }
    }
  5. Remove built.sbt as we are using Build.scala. This to avoid below runtime error.
    java.lang.RuntimeException: Overlapping output directories
  6. Start your play application using "activator run".

References:
https://github.com/leon/play-salat