Tag Archives: code

Arnoldc interpreter using Clojure instaparse

Wow, I didn’t realize I had taken such a long sabbatical since my last post. I haven’t let off the coding gas while I was away ūüôā ¬†One of the many things I have been working on is¬†writing a clojure Arnoldc interpreter. If you are unfamiliar with Arnoldc, check out this link¬†https://github.com/lhartikk/ArnoldC. ¬†I had come across clojure instaparse¬†during the advent of code 2015 and used it to solve day 7¬†and thought it was the perfect tool to accomplish my goal. ¬†Per the README on project page, “Instaparse aims to be the simplest way to build parsers in Clojure” and I couldn’t agree more.

The project is made up of the following clojure files:

 

The lexr.clj (Parser)

The lexr file contains the code utilizing the instaparse library to transform Arnoldc strings into hiccup structures. Below is a brief description of the clojure vars and functions within the lexr and interpreter.

NOTE: I left out code that supported my need to capture the actual thrown parse exception instead of the default implemented within the library. Refer to http://stackoverflow.com/questions/26338945/how-to-test-for-texts-not-fitting-an-instaparse-grammar-clojure for further details.

ADDITIONAL NOTE: I use ,,, in place of ellipsis as commas are whitespace with in clojure ūüôā I also bold parenthesis when used as part of clojure code in the hopes that it adds clarity.

 

(def tokens ,,,)

This contains a map of all the tokens of the Arnoldc language. I created this with the idea that I could show how easy it would be to transform the language and continue to have the same functionality. At the end of the post I describe an example of this with arnoldc pig latin lexr and the test

(defn arnold-grammar ,,,)

This function will create the string representation of the Arnoldc grammar. As clojure core doesn’t contain string interpolation, this is not the greatest¬†looking… When you combine the tokens and the string you would receive the following string

(def arnoldc (insta/parser arnold-grammar))

This is where the parsing magic happens.  Passing in a string will parse it into hiccup structures according to the Arnoldc grammar that I defined.

example usage is here

(def transform-ops ,,,)

This map was created to transform the parsed elements that match clojure keyswords. Early versions of the transform simply returned values  0, 1 for @I LIED and @NO PROBLEMO that were not in the hiccup style [:key value] . I needed these specific elements to conform to a hiccup shape so that I could evaluate it in a consistent way when I wrote my interpreter. For clarity:

  • I originally returned 0 then transformed it into ¬†[:false 0]
  • I originally returned 1 then transformed it into [:true 1]

I found that the original version(returning just the value 0 or 1) made the interpreter much more complicated as I was having to check which kind of structure I was receiving after recursing down to the returned value.  By adjusting the transform to return a hiccup structure I was able to remove the complex validation checks I was building due to the keyword that told me what I was dealing with.  This allowed me to continue to rely on multimethods that were guaranteed to get the correct structure and use the same recursive function for all the tokens.

 

(defn lights-camera-action ,,,) 

This wraps the Arnoldc instaparse parser and is what should be used to parse Arnoldc strings. 

 

The interpreter.clj

The interpreter is mostly a multimethod that is dispatched on the first element within a hiccup structure(a clojure keyword). A global symbol-table  is used to hold variables and state. I will describe a some of the more interesting items below.

(def symbol-table (atom {}))

This atom is a  map that holds the state for an Arnoldc program.

DISCLAIMER:¬†I made a trade-off in my design with how I implemented garbage collection…. I haven’t done it yet. Completing a program will clear the symbol-table, however running a long enough loop that calls a function with parameters will eventually cause problems. Details on how variables are created can be found below in the ¬†(defn transform-method-variables ,,,)¬†section.

(defmulti run (fn [s] (nth s 0)))

This multimethod is the engine that powers the interpreter. It is dispatching on the keywords within each hiccup structure(clojure vector) which is always in the 0 position of each vector.

(defmethod run :Program ,,,)

The Arnoldc language allows methods to be declared before, after, or both before and after the main program. The let form(see below) separates out all the method declarations via the  clojure group-by function so that I can define them(by run-statements which will dispatch to multimethods) before they are called within the main Arnoldc program(via (run bmain) ). bmain gets all statements that evaluate to true via group-by, while method-des gets all the method declarations.

I reset the symbol-table when the program completes so that I don’t pollute subsequent runs. To see what can happen, comment out the (reset! symbol-table {})¬†line and run the tests :). You will find that the symbol table keeps the state around and causes problems between tests that use the same method declarations and variables.

(defmethod run :Program [[e & rest :as statements]]
 (let [ {[bmain] true method-des false} 
                (group-by #(= :begin-main (first %)) rest)]
 (try 
   (run-statements method-des)
   (run bmain)
 (catch Exception e (throw e))
 (finally
  (reset! symbol-table {})))))

(defn arithmetic-helper ,,,)

This function handles the Arnoldc logic and arithmetic operators. The thing to note is the following lines of code. case is wrapped in a second set of parenthesis in order to immediately call the returned function from choose-logic-op or choose-op. The function will be invoked with operand and the result of (run varnum-node) as parameters.

A function case, will return a function and be passed and operand the the result of a multimethod dispatch against varnum-node. Higher order functions FTW!

(recur ( (case arith-key
           :logical-op (choose-logic-op operator)
           :arithmetic-op (choose-op operator)) 
         operand 
         (run varnum-node)) 
  rest)

(defn transform-method-variables ,,,)

This function¬†is called by the¬†:call-method multimethod. I used¬†gensym and the method name¬†to create a prefix to be passed to the¬†transform-method-variables¬†function. I did this as I am storing the variables all in the “global”¬†symbol-table atom. An¬†issue this function resolved was one that I encountered in recursive test cases where I would stomp on variables declared in the methods as they were called multiple times. I created¬†an issue¬†to clean this up.

(defmethod run :call-method ,,,)

Most of the complexity of this code is in the handling of arguments passed to the method(if/when they are passed). new-meth-args gets the same treatment as the variables mentioned in the transform-method-variables function mentioned above and gets a prefix. 

(defn roll-credits ,,,)

This function is the preferred way to interpret Arnoldc code. See the tests for an example.

(roll-credits  "IT'S SHOWTIME
         HEY CHRISTMAS TREE var
         YOU SET US UP 123
         YOU HAVE BEEN TERMINATED" )

Pig Latin Arnoldc lexr

As mentioned in the (def tokens ,,,) section above, we have finally reached the section where I describe why I defined the tokens outside of the arnoldc-grammar. The key to the transformation is the update-map function which will transform all the values within a map.

(def pig-latin-arnoldc ,,,)

The following function code will

  1. use the update-map function and take the arnoldc tokens map and return a map with the same keys, but with new pig latin arnoldc values(the qoutes that make up the language). The translate-to-pig-latin function will split a string on whitespace and map the pig-latin function to all the strings from the split and then join  them together to form a pig latin string. 
  2. pass the “new” map¬†of the pig latin arnoldc language(described in #1) to the arnold-grammar function. Since the keywords stay the same, the function will simply pull out the new values mapped to the original arnoldc keys and insert them into the grammar.
  3. Finally insta/parser will return an executable parser based on the pig-latin grammar.
(def pig-latin-arnoldc
  (-> (update-map arnie/tokens translate-to-pig-latin);#1
      (arnie/arnold-grammar);#2
      (insta/parser)));#3

(defn ights-camera-actionlay ,,,)

This function is the pig latinified lights-camera-action function from the arnoldc lexr.

  1. pass the arnoldc lexr’s parser function¬†the pig latin transformed string(s) represented by¬†expr.
  2. transform the parsed hiccup datastructures that match the keys within the transform-ops from the arnoldc lexr. This continues to work without modification because the map keys remain the same as the orignal arnoldc definitions(we only updated the values :D).
  3.  catch any thrown parse errors
(defn ights-camera-actionlay [& expr]
"interpret pig-latin text"
  (try (->> (arnie/parser pig-latin-arnoldc (clojure.string/join expr));#1
            (insta/transform arnie/transform-ops));#2
       (catch Exception e 
         (throw (Exception.  (str "EREWHAY ETHAY UCKFAY IDDAY IWAY OGAY ONGWRAY?" (.getMessage e)))))))

Conclusion

I was really satisfied with how quickly instaparse allowed me to create an interpreter. The hiccup structures were easy to work with and clojure is a joy to code in. I will certainly be keeping it within my toolbox and I encourage you to try it out.

 

Grails Starter kit

After years of working with Grails, I thought I would put down in one place a few things I tend to use frequently in my Grails projects. These are things I thought might help others getting started with Grails.

Templates

I often find that I need to extend the default session timeout(found in the src/templates). All that is needed is

> grails install-templates

You will then need to edit the web.xml contained in the following directory structure that was generated from the above command.

src -> templates -> war -> web.xml

you can now modify the timeout value

<session-config>
    <session-timeout>180</session-timeout>
 </session-config>

Custom Domain Classes

Update your domain class templates to reduce having to type in your company¬†standards for every domain class. One of the environments I worked in had a database¬†standard that required us to label¬†Primary Key column name(PK_column_name) and use oracle’s generator for ids. I addressed this by creating a private company “db standards” Grails plugin.

First I updated the

templates-> artifacts ->DomainClass.groovy file

@artifact.package@class @artifact.name@ {
    Date lastUpdated
    Date dateCreated
    String createdBy
    String updatedBy

    static constraints = {

    }
    static mapping={
        id generator: 'sequence', params: [sequence: '@artifact.name.uppercase@_SEQ']
        columns { id column: 'PK_@artifact.name.uppercase@_ID' }//prod/test
    }
}

I then copied and modified a core grails script(see comments in code) that creates domain classes and renamed it create-my-domain(well it was better names then this… but you get the gist :D). See below.
Then I can run

>create-my-domain Book

The following concrete domain class is generated(notice the sequence,id, and generator are all there).

class Book{

  Date lastUpdated
  Date dateCreated
  String createdBy
  String updatedBy
  static mapping = {
    id generator: 'sequence', params: [sequence: 'BOOK_SEQ']
    columns { id column: 'PK_BOOK_ID' }
}

DataSource(s)

There are times when I need to query a database that I don’t need/want a Domain Class for. ¬†I tend to create a datasource and inject it into a service that will be handling the connection/query with groovySql

Here is an example datasource in the DataSource.groovy file.

dataSource_salesInfo {
  driverClassName = "oracle.jdbc.driver.OracleDriver"
  url = ""//more on how to configure this later in this blog entry
  username = ""

  password = ""
  dialect = "org.hibernate.dialect.Oracle10gDialect"
  hibernate {
    cache {
      use_second_level_cache = false
      use_query_cache = false
    }
  }
}

Example service that uses the defined datasource entry.

class AwesomeService{

  def dataSource_salesInfo //Inject it

  def queryDB(){
  def sql = new Sql(dataSource_salesInfo)
  String myQuery ='..'
  sql.rows(myQuery).each { resultSet -> /*do something*/}
  }
}

Externalize Datasources

One way to externalize your jdbc connection url is to use an system environment variable. Another is to have your operations team generate a properties file based on the tnsnames.ora file and read it into your grails application. I will describe how the properties file based solution would work.
An Example tnsnames.properties entry might look like the following.

DATAWAREHOUSE=jdbc:oracle:thin:@xyaeyp49:1521:DATAWAREHOUSE

in datasource.groovy file, I add the following to the top of the file.

def props = new Properties()
new File('pathToTNS.properties').withInputStream { stream -> 
props.load(stream) }

then I reference the property in the URL entry for the datasource I am working with 

username='javazquez'
url = props.get("DATAWAREHOUSE")
...

 

Most of my day is spent with Oracle databases. The validation query that I use is

 

validationQuery = "SELECT sysdate from dual"

here is a complete example.

production {
  dataSource {
    url = props.get("DATAWAREHOUSE")
    username = ""
    password = ""
    properties {
      maxActive = -1
      minEvictableIdleTimeMillis = 1800000
      timeBetweenEvictionRunsMillis = 1800000
      numTestsPerEvictionRun = 3
      testOnBorrow = true
      testWhileIdle = true
      testOnReturn = true
      validationQuery = "SELECT sysdate from dual"
    }
  }
}

 

Here are the lines that actually allow you to externalize your config/datasources(Thanks to Burt Beckwith and his book )

 

production {
grails.config.locations = [
"file:path_2_external_config.properties" //NOTE: don’t leave spaces
]
}

I like to have the following in my path

grails.config.locations = [
 "file:path_${appName}/${appName}_${grails.util.Environment.current.name}_config.groovy",
]

This forces the file name to have¬†development || production || test¬†and the application name in the configuration file to prevent copy and paste errors(you would never do that right ūüėČ )

SQL Logging

There are a couple of ways to see what is going on under the hood when your application is querying the database.

The easiest is to add the logSql = true to your datasource

development {
  dataSource {
    url = props.get("DATAWAREHOUSE")
    logSql = true
    ...
    }
  }
}

If you need more information,  Burt Beckwith has the following Logging Hibernate SQL post that you should read. He shows how adding the following lines to your log4j closure will help with understanding what is happening under the hood.

log4j = {
   ...
   debug 'org.hibernate.SQL'
   trace 'org.hibernate.type.descriptor.sql.BasicBinder'
}

Misc

A log pattern that works with tomcat(YMMV)

appenders {
console name: 'stdout', threshold: org.apache.log4j.Level.INFO
rollingFile name: 'MyLogs', file: 'logs/MyLog.log', maxSize: 104576, threshold: org.apache.log4j.Level.INFO ,
layout:pattern(conversionPattern: "%d{yyyy-MM-dd HH:mm:ss} [%t] %p %c - %m%n")

}

Have your war tell you what environment you are deploying to

grails.project.war.file = "target/${appName}-${appVersion}-${System.getProperty('grails.env')[0..3]}.war"

Websites and links to check out

 

Many of the things I have learned have come from the above links and people. A big Thank You to the gr8 community for all the help over the years.

 

-Juan

Grails ldap integration with Active Directory via spring-security-ldap

The spring-security-ldap has great documentation. I put together a working example (at least in my environment) to complement the docs. When I was tasked with integrating our grails apps with Active Directory I remember there being a scarcity of examples.. so I hope this code will save you some time in getting ldap working with Active Directory in your grails environment.

Important files

grails-app/conf/Config.groovy
src/groovy/com/javazquez/ldapexample/MyUserDetailsContextMapper.groovy
src/groovy/com/javazquez/ldapexample/MyUserDetails.groovy
grails-app/conf/spring/resources.groovy

once you have your Active Directory configurations entered (grails-app/conf/Config.groovy), fire up your app and
test it out by logging in via the login controller.

Notes

  • You may have to update MyUserDetailsContextMapper.groovy as my Active Directory environment may differ from yours.
  • You may also want to update MyUserDetails.groovy to hold more or less info than my config.

-JV

Add Map, Reduce, and Filter to Groovy with an Extension Module

Having just read MichaŇā Mally’s blog that was posted on Google+,
I was intrigued with two benefits listed in the blog:

  • The idea of being able to augment Groovy with changes that would behave as “if they were a part of original GDK”
  • support from your IDE like code completion shall be available out-of-the-box

In order to get my head around how Extension modules worked, I used the following as references
Creating an extension module
Groovy Goodness: Adding Extra Methods Using Extension Modules
Groovy Extension Modules

Cédric Champeau had this to say after I asked about the benefits of Extension Modules over using MetaClass/Expando/Category

@Juan: extension modules are automatically loaded and made available globally. You don’t have to bother with metaclasses (and potential issues with external changes). As well, categories are lexically scoped, although extension modules are global (meaning that they can be used anywhere in the code as long as the extension module is found on classpath).

Last but not least, extension modules are compatible with type checking and static compilation ūüôā

To solidify my new understanding of Groovy’s Extension Modules, I decided that I needed to write some code. The example I came up with was to have the functional names (map, reduce, filter ) that I had come familiar with in using Clojure added to Groovy. These “extended methods” are using Groovy’s built-in collect, inject, and grep under the hood.

Source code can be found here

Here is the code for the new aliases found in the FuncProgUtilExtension.groovy class

package com.javazquez;

public class FuncProgUtilExtension {
    public static Collection filter(Collection self, Closure clozure) {
	   return self.grep(clozure)
   }
   public static Collection map(Collection self, Closure clozure) {
	   return self.collect(clozure)
   }
   public static Object reduce(Collection self, Closure clozure) {
	   return self.inject(clozure)
   }
   public static Object reduce(Collection self, String operator) {
	   switch(operator){
		   case '+' :
		      self.inject({acc, val -> acc + val})
			  break
		   case '-' :
			   self.inject({acc, val -> acc - val})
			   break
		   case'*' :
			   self.inject({acc, val -> acc * val})
			   break
		   case '/':
		   	   self.inject({acc, val -> acc / val})
			   break
		   case'**':
		   	   self.inject({acc, val -> Math.pow(acc, val)})
			   break
		   default:
			   throw new IllegalArgumentException()
			   break
	   }
   }
}

In a file named ‘org.codehaus.groovy.runtime.ExtensionModule’ located in ‘src/main/resources/META-INF/services/’

I have the following

moduleName=JavazquezFuncProgTest
moduleVersion=1.0
extensionClasses=com.javazquez.FuncProgUtilExtention

Using spock, I wrote the following tests :

package com.javazquez

import spock.lang.Specification

class FuncProgUtilSpec extends Specification{

	def "test map"(){
		expect:
			[ 1 ,2 ,3 ,4].map{it*2} == 	[ 1 ,2 ,3 ,4].collect{ it*2 } 	
	}	
	def "test reduce "(){
		expect:
			[ 1 ,2 ,3].reduce('*') == 6
			[ 1 ,2 ,3,4].reduce('+') == 10
			[ '1' ,'2' ,'3','4'].reduce('+') == '1234'
			[ 1 ,2 ,3].reduce('-') == -4
			[ 2, 2 ,2].reduce('**') == 16
			[ 1 ,2 ,3].reduce({acc, val -> acc + val}) ==[ 1 ,2 ,3].inject { acc, val -> acc + val}
	}
	def "test invalid argument"(){
	 	when:
	 		[ 1 ,2 ,3,4].reduce('%')
		then:
			thrown(IllegalArgumentException)
	}
	def "test filter"(){
		expect:
			[1,2,3,4,5,6,7,8,9].filter { it % 2 ==0 } == [2,4,6,8]
			[1,2,3,4,5,6,7,8,9].filter { it > 2 } == [3,4,5,6,7,8,9]
			"Juan Vazquez".toList().filter { it ==~ /[aeiou]/} == ['u','a','a','u','e']
	}
}

My biggest obstacle was getting the directory structure correct. It is amazing how little code was required to accomplish my goal. I hope my example project and listed references will help in your understanding of this powerful feature. My next step with this project going to be to make evaluation lazy.

Groovy Invoke Dynamic Support

The release of Groovy 2.1 comes with full Invoke Dynamic Support. Initially I had issues with trying to get a working example up and running as seen by the following message.

>groovy –indy mergesort.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during class generation: Cannot use invokedynamic, indy module was excluded from this build.

I checked the groovy version to make sure that I had Java 7 loaded as seen here
groovy -version
Groovy Version: 2.1.0 JVM: 1.7.0_11 Vendor: Oracle Corporation OS: Mac OS X

In order to get things working, I read that I needed to get the “indy” jar on my classpath. I added the following to my .bash_profile and restarted my terminal and the error cleared up

export CLASSPATH=$HOME/.gvm/groovy/current/indy/groovy-2.1.0-indy.jar

Hope this helps and a huge thanks to the Groovy Core Team for this latest update!

-Juan

Clojure Soundex

In need of a quick program to force myself to dive in to clojure, I chose to implement a soundex program that I at one time had written in C++. It was a fun exercise to step back and look at how my thought process changed based on the language I used. Hope you find this useful.

 

;steps
;1 keep first letter
;2 replace consonants
;3 remove w and h
;4 two adjacent are same, letters with h or w separating are also the same
;5 remove vowels
;6 continue until 1 letter 3 nums
(use 'clojure.contrib.str-utils)

(defn trnsfrm[ word]
  (->>
    (re-gsub #"(?i)[fbvp]" "1" word)
    (re-gsub #"(?i)[cgjkqsxz]" "2" ,,) 
    (re-gsub #"(?i)[dt]" "3" ,,) 
    (re-gsub #"(?i)[l]" "4" ,,)
    (re-gsub #"(?i)[mn]" "5" ,,)
    (re-gsub #"(?i)[r]" "6" ,,)))

(defn replace-adjacent [word] 
  (->> (re-gsub  #"(?i)[wh]" "" word ) 
  	trnsfrm 
  	(re-gsub #"(?i)([a-z0-9])\1+" "$1" )))  	

(defn pad [word](subs (str word "0000") 0 4))  	

(defn do-soundex [word]
    (pad ( str (first word)(re-gsub #"[aeiouy]"  "" (subs (replace-adjacent word) 1)))))

Update Refactored version
Not quite happy with the above example, I decided to see if I could refactor my code. Below is what I came up with(4 less lines code).

(use 'clojure.contrib.str-utils)

(def re-map{ #"(?i)[fbvp]" "1",#"(?i)[cgjkqsxz]" "2",#"(?i)[dt]" "3",#"(?i)[l]" "4",#"(?i)[mn]" "5",#"(?i)[r]" "6" })

(defn trns [word] (map #(re-gsub (key %1) (val %1) word) re-map))

(defn pad [word](subs (str word "0000") 0 4))

(defn rm1 [word] (apply str(drop 1 word)))

(defn do-soundex [word]
    (pad(str (first word) (->>
        (re-gsub #"(?i)[^aeiou\d]" "" (apply str (apply interleave (trns word ))))
        (re-gsub #"(?i)([a-z\d])\1+" "$1" )
        rm1
        (re-gsub #"(?i)[a-z]" "" )))))

 

UPDATE 2:Always one to go back to my previous works, I thought I would try a different approach. 

Now for the test cases

;;;Start test
(=(do-soundex  "Ashcroft") "A261")
(=(do-soundex  "Ashcraft") "A261")
(=(do-soundex  "Tymczak") "T522")
(=(do-soundex  "Pfister") "P236")
(=(do-soundex"lukaskiewicz")"l222")
(=(do-soundex"Rubin")"R150")
(=(do-soundex"Rupert")"R163")
(=(do-soundex"Robert")"R163")
(=(do-soundex "Vazquez")"V220")

;;;end test

Simple Groovy project using Gradle

Hello fellow Groovyists ūüôā

I have been kicking the tires on using Gradle for my Groovy projects. I had a few stumbles along the way and wanted to share what I came up with for getting a very simple example working.

build.gradle

apply plugin: 'groovy'
version = "1.0-${new Date().format('yyyyMMdd')}"

manifest.mainAttributes("Main-Class" : "com.javazquez.HelloThere")

repositories {
mavenCentral()
mavenRepo urls: "http://groovypp.artifactoryonline.com/groovypp/libs-releases-local"
}
dependencies {
groovy group: 'org.codehaus.groovy', name: 'groovy-all', version: '1.8.4'
groovy group: 'org.mongodb', name: 'mongo-java-driver', version: '2.6.5'
groovy group: 'com.gmongo', name: 'gmongo', version: '0.9.1'
testCompile "org.spockframework:spock-core:0.5-groovy-1.8"
}

jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

below is the the HelloThere.groovy file located src/main/groovy/com/javazquez/HelloThere

package com.javazquez
public class HelloThere {

public static void main(String []args) {
println "Hello coders!"

}

}

after running gradle build, I can navigate to the build/libs directory and run java -jar HelloThere-1.0-20111115.jar and get the following ouptut

Hello coders!

Gradle is a fantastic tool and I hope this article helps show the ease of getting a project set up.

Activiti GET/POST REST requests with Groovy

I have been working with Activiti lately and needed to test the REST API included with the demo. Below are the GET and POST requests I whipped up using Groovy. Hope you find this useful ūüôā


//---Get Request
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0' )
import groovyx.net.http.RESTClient

def client = new RESTClient('http://localhost:8080/activiti-rest/service/process-engine')
println client.get(headers:[Authorization:"Basic ${'kermit:kermit'.bytes.encodeBase64()}"]).data

// output
[name:default, exception:null, version:5.7, resourceUrl:jar:file:/Users/juanvazquez/Documents/activiti-5.7/apps/apache-tomcat-6.0.32/webapps/activiti-rest/WEB-INF/lib/activiti-cfg.jar!/activiti.cfg.xml]


// POST request
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0' )
import static groovyx.net.http.ContentType.JSON

def jsonObj = new groovy.json.JsonBuilder()
jsonObj{
  userId 'kermit'
  password 'kermit'
}
def client = new groovyx.net.http.RESTClient('http://localhost:8080/activiti-rest/service/login')
def response = client.post(contentType: JSON, body:jsonObj.toString() )

println response.data           

//output
[success:true]

POP3 Gmail access with Clojure and JavaMail

I recently had the need to access gmail using Clojure. I used JavaMail to accomplish this via pop3. Below is some code that I wrote to help me get emails. Hope you find it useful Enjoy ūüôā


(use '[clojure.contrib.duck-streams])
(def props (System/getProperties))
; Get the default Session object.
(def session (javax.mail.Session/getDefaultInstance props))

; Get a Store object that implements the specified protocol.
(def store (.getStore session "pop3s"))

;Connect to the current host using the specified username and password.
(.connect store "pop.gmail.com" "username@gmail.com" "password")

;Create a Folder object corresponding to the given name.
(def folder (. store getFolder "inbox"))

; Open the Folder.
(.open folder (javax.mail.Folder/READ_ONLY ))
; Get the messages from the server
(def messages (.getMessages folder))

(defn getFrom [message](javax.mail.internet.InternetAddress/toString (.getFrom message)))
(defn getReplyTo [message] (javax.mail.internet.InternetAddress/toString (.getReplyTo message)) )
(defn getSubject [message] (.getSubject message))

;print out the body of the message
(for [m messages] (read-lines(.getInputStream m)) )

;;;;;code for sending an email

(def props (System/getProperties))
(. props put "mail.smtp.host", "smtp.gmail.com")
(. props put "mail.smtp.port", "465")
(. props put "mail.smtp.auth", "true")
(. props put "mail.transport.protocol", "smtps")

(def session (javax.mail.Session/getDefaultInstance props nil))
(def msg (javax.mail.internet.MimeMessage. session))
(. msg setFrom (javax.mail.internet.InternetAddress. "sender@gmail.com"))
(. msg addRecipients javax.mail.Message$RecipientType/TO
"receiver@gmail.com")

(. msg setSubject "i am the subject")
(. msg setText "I am the body!!!")

(. msg setHeader "X-Mailer", "msgsend")
(. msg setSentDate (java.util.Date.))

; send the email
(def transport (. session getTransport))
(. transport connect "smtp.gmail.com" 465 "sender@gmail.com" "password")
(. transport sendMessage msg (. msg getRecipients javax.mail.Message$RecipientType/TO))
(. transport close)

Writing a PayPal SOAP client with Java 6

I have always been mystified on the inner workings of SOAP. That was until I learned about the “wsimport” utility that comes with Java 6. It makes the entire process very easy. Below is an example of writing a SOAP client for PayPal’s Sandbox. This code will execute the SetExpressCheckout API call.

Just enter the following on your command line to generate the com.javazquez package

wsimport -keep -XadditionalHeaders -Xnocompile -p com.javazquez http://www.sandbox.paypal.com/wsdl/PayPalSvc.wsdl

open your favorite java editor(I used eclipse) and add the package(‚Äúcom.javazquez‚ÄĚ..created in the above command) to your new project

next, write some code to test out the APIs

package com.javazquez;

import javax.xml.ws.Holder;
public class TestEC {

public static void main(String[] args) {
SetExpressCheckoutReq req = new SetExpressCheckoutReq();
SetExpressCheckoutRequestType reqType = new SetExpressCheckoutRequestType();
SetExpressCheckoutRequestDetailsType details = new SetExpressCheckoutRequestDetailsType();
AddressType addr = new AddressType();
addr.cityName = "omaha";
addr.street1 = "123 main";
addr.country = CountryCodeType.US;
addr.name = "joe tester";

details.address = addr;
details.orderTotal = new BasicAmountType();
details.orderTotal.currencyID = CurrencyCodeType.USD;
details.orderTotal.value = "1.00";
details.cancelURL = "http://javazquez.com/cancel";
details.returnURL = "http://javazquez.com/return";

reqType.setVersion("2.10");

reqType.setExpressCheckoutRequestDetails = details;
req.setSetExpressCheckoutRequest(reqType);

UserIdPasswordType user = new UserIdPasswordType();
user.username = "XXX";
user.password = "XXXX";
user.signature = "XXXX";

PayPalAPIInterfaceService pp = new PayPalAPIInterfaceService();
PayPalAPIAAInterface pinterface = pp.getPayPalAPIAA();
Holder security = new Holder(new CustomSecurityHeaderType());
security.value.setCredentials(user);
try{
SetExpressCheckoutResponseType resp = pinterface.setExpressCheckout(req, security);
System.out.println(resp.token);
System.out.println(resp.correlationID);
for(ErrorType msg: resp.errors){
System.out.println(msg.longMessage);
}
}
catch(Exception ex){
System.out.println(ex.getMessage());

}
}

}