Tuesday, February 23, 2016

Open Source Anti Virus - ClamAV

ClamAV is an open source (free) anti-virus engine, support stream scanning using Java API. It’s come with an advanced tool such as online virus pattern database update.

ClamAV supports in Unix, Linus, MaxOS and Window. ClamAV is under Cisco Systems and can be reliable.

In this article, I am going to demonstrate how to install ClamAV in Window and write a sample java application to send a file for scanning.
  1. Download and install ClamAV.
    • Download "clamav-0.99-x64.msi" at http://www.clamav.net/documents/installing-clamav.
    • Double click on "clamav-0.99-x64.msi" to start the installation by selecting default options.
    • ClamAV is installed in folder C:\Program Files\ClamAV-x64 if you follow default options.
  2. Configure ClamAV.
    • Copy clamd.conf.sample and freshclam.conf.sample from C:\Program Files\ClamAV-x64\conf_examples\ to C:\Program Files\ClamAV-x64\.
    • Rename clamd.conf.sample to clamd.conf.
    • Rename freshclam.conf.sample to freshclam.conf.
    • In clamd.conf, 
      • comment "Example" in line number 8.
      • uncomment "TCPSocket" in line number 101.
      • uncomment "TCPAddr" in line number 109.
    • In freshclam.conf,
      • comment "Example" in line number 8.
    • Run freshclam using command prompt to download antivirus database.
    • Start ClamAV by run clamd using command prompt.
  3. Test ClamAV
    • Go to folder C:\Program Files\ClamAV-x64.
    • Run clamscan and scan all files in the directory.
  4. Write a sample JAVA Program
    • Download clamavj-0.1.jar and org.apache.commons.logging.jar at http://soniyj.altervista.org/blog/free-solution-for-check-infected-files-with-java-and-clamav/?doing_wp_cron=1456213243.7796299457550048828125
    • Create Test.java and copy below code:
      import com.philvarner.clamavj.ClamScan;
      import com.philvarner.clamavj.ScanResult;
      
      import java.io.*;
      import java.io.FileInputStream;
      
      public class Test {
       
       public static void main(String args[]) {
        System.out.println("Start");
        ClamScan clamScan = new ClamScan("127.0.0.1", 3310, 20);
        
        try {
         ScanResult result = clamScan.scan(new FileInputStream("D:\\14k.png"));
         System.out.println(result.getStatus());
        } catch (FileNotFoundException e) {
         e.printStackTrace();
        }
        
        System.out.println("End");
       }
       
      }
      
    • Compile Test.java using the command:
      javac -cp clamavj-0.1.jar;org.apache.commons.logging.jar Test.java
      
    • Run Test.java using the command:
      java -cp .;clamavj-0.1.jar;org.apache.commons.logging.jar Test
      
    • The code actually sending "D:\14k.png" to scan and return pass. Passed means the file is clean.
References:

Tuesday, November 17, 2015

Scala map vs flatMap

map is loop thru all element and apply a function into it.
scala> val i = List("Apple", "Banana", "Orange")
i: List[String] = List(Apple, Banana, Orange)

scala> i.map(x => x.toUpperCase)
res2: List[Char] = List(APPLE, BANANA, ORANGE)
flatMap is loop thru all element, flatten the element and apply a function into it.
scala> val i = List("Apple", "Banana", "Orange")
i: List[String] = List(Apple, Banana, Orange)

scala> i.flatMap(x => x.toUpperCase)
res2: List[Char] = List(A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E)
Here is an example on different between map and flatMap in List(List(), List())
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)

scala> def x(v:Int) = List(v,v+1)
x: (v: Int)List[Int]

scala> l.map(i => x(i))
res5: List[List[Int]] = List(List(1, 2), List(2, 3), List(3, 4))

scala> l.flatMap(i => x(i))
res6: List[Int] = List(1, 2, 2, 3, 3, 4)
Here is an example on different between map and flatMap in Option (None and Some). flatMap remove empty value (None).
scala> val l = List(1,2,3,4,5)
l: List[Int] = List(1, 2, 3, 4, 5)

scala> def f(x:Int) = if (x > 2) Some(x) else None
f: (x: Int)Option[Int]

scala> l.map( x => f(x))
res11: List[Option[Int]] = List(None, None, Some(3), Some(4), Some(5))

scala> l.flatMap( x => f(x))
res12: List[Int] = List(3, 4, 5)
References:
http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
http://alvinalexander.com/scala/collection-scala-flatmap-examples-map-flatten

Sunday, November 15, 2015

Scala For Loop Syntax

For Loop with Counter

The simplest loop syntax is:
for (a  <- 1 to 5){
   println(“Print a: ” + a);
}

When the above code is compiled and executed, it produces following result:
Print a: 1
Print a: 2
Print a: 3
Print a: 4
Print a: 5
By default, counter increment is 1. You can change the number of counter increment using "by".
for (a  <- 1 to 5 by 2) {
   println(“Print a: ” + a);
}
When the above code is compiled and executed, it produces following result:
Print a: 1
Print a: 3
Print a: 5

For Loop with Filter

You can filter out some of the elements. Following is the example of for loop along with filters:
for (
   a  <- 1 to 5
   if ( a > 3)
) {
   println(“Print a: ” + a);
}
When the above code is compiled and executed, it produces following result:
Print a: 4
Print a: 5

For Loop with Yield

This is one of the awesome feature in Scala where you generate a static type variable using loop. I don’t really see in other programming language.

When For loop finishes running, it returns a collection of all these yielded values.

The type of the collection that is returned is the same type that you were iterating over.

// Loop through and generate a variable
val i = for (
   a  <- 1 to 5
   if ( a > 3)
) yield {
   a
}

// Print the result
for( a <- i){
   println( "Value of a: " + a );
}
When the above code is compiled and executed, it produces following result:
Print a: 4
Print a: 5

Wednesday, October 28, 2015

Generate CSV in Playframwork

Example of generate CSV by modify output header.

  def exportCSV = Action {
    val data = "data 1, data 2"

      Ok(data).withHeaders(
          CONTENT_TYPE -> "text/csv",
          CONTENT_DISPOSITION -> "attachment; filename=foo.csv"
      )
  }

This method only suitable if you have small amount of data. If not, I would recommend using streaming.

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