World Time Engine provides all kind of time related information for any given place on this planet. You can use their free web portal and enter GPS coordinates, city names or postal codes and retrieve timezone, daylight saving and more. Otherwise they provide an API to get the data via an http GET request. This service is not free, they charge you a 14 U$ for 50.000 requests.
API details you find here, but in short:
http://worldtimeengine.com/api/<apikey>/<latitude>/<longitude>
and you get something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://worldtimeengine.com/timezone.xsd">
<version>1.1</version>
<location>
<region>Australia/New South Wales</region>
<latitude>-33.8671</latitude>
<longitude>151.207</longitude>
</location>
<time>
<utc>2008-02-13 00:50:30</utc>
<local>2008-02-13 11:50:30</local>
<zone>
<hasDST>true</hasDST>
<current>
<abbreviation>EDT</abbreviation>
<description>Eastern Australia Daylight Savings Time</description>
<utcoffset>+11:00</utcoffset>
<isdst>true</isdst>
<effectiveUntil>2008-03-29 19:00:00</effectiveUntil>
</current>
<next>
<abbreviation>EST</abbreviation>
<description>Eastern Australia Standard Time</description>
<utcoffset>+10:00</utcoffset>
<isdst>false</isdst>
<effectiveUntil>2008-10-25 18:00:00</effectiveUntil>
</next>
</zone>
</time>
</timezone>
How can we use Groovy to request for the data and access its detail ?
- Get yourself a API Key from world time engine
- We use the Groovy Console (groovyConsole from the shell)
- We need the httpbuilder library
- Copy http-builder-0.5.0.jar and all dependencies into the ./groovy/lib folder in your home folder
- Execute this code:
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
def worldTime = new RESTClient("http://worldtimeengine.com/api/{yourkey}/35.7647018432617/140.386001586914")
def resp = worldTime.get( contentType : XML)
println resp.data.time.zone.current.abbreviation.text()
println resp.data

Groovy Console

XML Response
- You see it is fairly simple to access specific fields with the groovy notation resp.data.time.zone.current.abbreviation.text()
How do I use this ?
- I use Groovy classes in my java EE application. You can cut down on boilperplate code dramatically by using Groovy !
- The above service I use in an location aware application.
Comments Off
With Groovy and the XML-RPC library (groovy.codehaus.org/XMLRPC) the access to Trac gets extremely easy. The older version 0.4 and 0.5 of the library could not handle basic authorization but thanks to Tim fixing the missing feature and some other xml format error, you can now access any Trac Wikipage with as little as 3 lines of Groovy code.
def serverProxy
= new XMLRPCServerProxy
("https://user:password@sometracserver.com/login/xmlrpc") // or
// def serverProxy = new XMLRPCServerProxy("http://sometracserver.com/login/xmlrpc")
serverProxy.setBasicAuth("user","password")
println serverProxy.
wiki.
getPage("TracBackup")
Remark:
At the time of this entry version 0.6 is not released yet. Check for availability at repository.codehaus.org/org/codehaus/groovy/groovy-xmlrpc/ for the new version or update your svn copy and build by yourself (see blog entry)
Update 2010-02-01: The new version was released as 0.5.1 (link)
The Groovy XML-RPC library 0.5 allows me to do perform basic authorization, but my Trac Server now throws a error code 500 at me (stating XML declaration not well-formed). Since I dont know how to check the XML request the library posts, I choose to dissect the messages at the network level using Wireshark. Luckily I have a Python library that works fine, so I can compare the working version of the XML-RPC request. (Remark: Dont forget to start wireshark as root user/sudo)
2010-01-07 09:03:03,095 Trac[main] DEBUG: Dispatching <Request "POST u'/login/xmlrpc'">
2010-01-07 09:03:03,117 Trac[main] ERROR: Internal Server Error:
Traceback (most recent call last):
File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/trac/web/main.py", line 441, in _dispatch_request
dispatcher.dispatch(req)
File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/trac/web/main.py", line 205, in dispatch
resp = chosen_handler.process_request(req)
File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/TracXMLRPC-1.0.6-py2.5.egg/tracrpc/web_ui.py", line 163, in process_request
self.process_xml_request(req, content_type)
File "/usr/local/Python-2.5.2/lib/python2.5/site-packages/TracXMLRPC-1.0.6-py2.5.egg/tracrpc/web_ui.py", line 167, in process_xml_request
args, method = xmlrpclib.loads(req.read(int(req.get_header('Content-Length'))))
File "/usr/local/Python-2.5.2/lib/python2.5/xmlrpclib.py", line 1130, in loads
p.feed(data)
File "/usr/local/Python-2.5.2/lib/python2.5/xmlrpclib.py", line 547, in feed
self._parser.Parse(data, 0)
ExpatError: XML declaration not well-formed: line 1, column 30
2010-01-07 09:03:03,121 Trac[chrome] DEBUG: Prepare chrome data for request
Python Library/Call
import xmlrpclib
server = xmlrpclib.ServerProxy("http://user:password@sometracserver.com/login/xmlrpc")
print server.wiki.getPage("WikiStart")
Result in Wireshark

Wireshark
Groovy Call
def serverProxy
= new XMLRPCServerProxy
("http://sometracserver.com/project/login/xmlrpc") serverProxy.setBasicAuth("user","password")
serverProxy.wiki.getPage("WikiStart")
Result in Wireshark

Wireshark
There is the problem: encoding=”+ENCODING+”
The Trac XML reader most likely will throw up the complete call because of this. Actually the comparison was not really required because the error is very obvious.
Comments Off
The current release is 0.4 (groovy-xmlrpc-0.4.jar). The actual codebase also supports basic authorization (link), but this version 0.5 was not released yet due to the failing junit test.
In this tutorial we will build the library from the codebase. Using Netbeans we have all the tool we need to achieve this easily.
Tutorial:
- Start Netbeans. Team | Subversion | Checkout..
Enter http://svn.codehaus.org/groovy/trunk/groovy/modules/xmlrpc/ and a local directory

SVN

SVN
==[IDE]== Jan 2, 2010 8:55:32 PM Checking out...
co -r HEAD http://svn.codehaus.org/groovy/trunk/groovy/modules/xmlrpc /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc --config-dir /home/sven/.netbeans/6.8/config/svn/config --non-interactive
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1/smackx-3.0.1.jar.md5
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smackx/3.0.1/smackx-3.0.1.jar
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1/smack-3.0.1.jar
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/repository/jivesoftware/smack/3.0.1/smack-3.0.1.jar.md5
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/.classpath
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/.project
A /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/src
...
- Build the library (right click on the project and Build)
NetBeans: Executing 'mvn -Dnetbeans.execution=true install'
NetBeans: JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.16
Scanning for projects...
Building Groovy XML-RPC
task-segment: [install]...
Installing /media/NEXUSII/GroovyAndGrails/GroovyBuildNB/xmlrpc/target/groovy-xmlrpc-0.5.0-SNAPSHOT.jar to /home/sven/.m2/repository/org/codehaus/groovy/groovy-xmlrpc/0.5.0-SNAPSHOT/groovy-xmlrpc-0.5.0-SNAPSHOT.jar
BUILD SUCCESSFULTotal time: 9 seconds
Finished at: Sat Jan 02 21:34:59 CET 2010Final Memory: 25M/137M
- Download missing dependencies
Eventually some libraries are missing, Netbeans will remind you. (Just press download)

Dependency Problems
In part 2 we will use the library to access TRAC via XML-RPC.
In the previous post we used the Groovy XML-RPC library, in this snippet we will use the Apache XML-RPC library. Why ? There is no basic security implemented in the Groovy XML-RPC to access for example a
The prerequisites are still the same, just we change the library:
- Download the Apache library from here. You should download a file from the binaries folder (apache-xmlrpc-3.1.2-bin.zip), inside you will find a lib subfolder with a couple of jars we need to add to the classpath.
(commons-logging-1.1.jar, xmlrpc-client-3.1.2.jar, xmlrpc-server-3.1.2.jar, ws-commons-util-1.0.2.jar, xmlrpc-common-3.1.2.jar)
Snippet:
Remark: You can add more parameters for the server with
- config.setEncoding(“UTF-8″)
config.setBasicUserName(“[username]“);
config.setBasicPassword(“[password]“);
Comments Off
It is straight forward to access Snipplr via XML-RPC with Groovy. In this tutorial we use the Groovy Console to play with Snipplr snippets.
Requirements:
Tutorial: