Saturday, May 3, 2008

Groovy 1.6-beta-1: c'est de la bombe :-)


>groovy -v
Groovy Version: 1.5.6 JVM: 1.5.0_13-119
>date; groovy Mandelbrot.groovy ; date
Sat May 3 22:03:53 SGT 2008
Groovy Elapsed 25.721
Groovy Elapsed 24.552
Groovy Elapsed 26.606
Sat May 3 22:05:12 SGT 2008


>/Applications/groovy-1.6-beta-1/bin/groovy -v
Groovy Version: 1.6-beta-1 JVM: 1.5.0_13
>date; /Applications/groovy-1.6-beta-1/bin/groovy Mandelbrot.groovy; date
Sat May 3 22:31:16 SGT 2008
Groovy Elapsed 5.198
Groovy Elapsed 5.174
Groovy Elapsed 5.273
Sat May 3 22:31:32 SGT 2008


x5 !
Congrats to the team !

Friday, May 2, 2008

Groovy and Twitter (Part 1)

Looks like Twitter will be under the spotlight at JavaOne this year. Posts on this topic are popping everywhere. I already talked about the "Script Bowl" session but if you are attending JavaOne you should probably have a look to this post.

But back to Groovy, I propose to write a simple class to interface to Twitter and then build mashup. But first, let's draft some specs. All communication with the server goes through http and sticks to the REST approach. You can find most of what I used here and there. Before going further, you must be familiar with some of the Twitter's terminology:
  • Tweet
    Every person has a status. Updating the status with a new one is the same as sending a tweet. Tweets have to be smaller than 140 characters.
  • Friends
    people you are subscribed to (following)
  • Followers
    people who are subscribed to your tweets
Some other concepts are:
  • Direct message
    a private message sent between two (or more) users.
  • Replies
    you can reply to another users’ tweets using the @username prefix in your own tweets.
In order to start, I would like my class to be able to:
  • authenticate to the server using a given login (and associated password)
  • retrieve the friends of that user
  • update it's status
So let's go ...

1. Authentication. This is fairly easy since Twitter uses Basic Authentication. I propose to set an authenticator using a Groovy syntax ;-)

Authenticator.setDefault(
[getPasswordAuthentication : { return new PasswordAuthentication(name, password as char[]) } ] as Authenticator
)


2.Retrieve friends. Fairly easy as well, we just need to send a GET request to http://twitter.com/statuses/friends/${user.id}.xml and yes you have to add ?page=$page if you have plenty of them. In this request, user is a Twitter user returned by getUser().
3.Update your status. Last by not least, we need to update our status. This is done by sending a POST request to http://twitter.com/statuses/update.xml using a RESTful request.

An example is worth thousands of words:

twitter = new Twitter("guillaume.alleon@gmail.com", "****")
twitter.postUpdate("Finalizing my Groovy Twitter API")

def u = twitter.getUser("glaforge")
println "Id of Groovy grand master is ${u.id}"

def f = twitter.friends
f.each{println it.name}


Obviously this API is still in its infancy, I have been playing with it few hours. You will be able to get it here: http://code.google.com/p/groovy-twitter/

Thursday, May 1, 2008

Still alive ...

Well you could wonder if this blog is still alive ... Alas it has not been very active these last few months but I will try to resurrect it a bit. A lot has happen recently:
  • Apache CXF has graduated,
  • G2One and the Groovy team have released version 1.5.6
  • Gradle has been out
It then time for a new version of GroovyWS !

JavaOne 08 is next week. "Au Menu" will be a "Script Bowl" session. I will try to post some examples on the proposed apps (twitter & the world web app) before the conference starts (as I am sure you will get better solution there ;-))

So see you soon !

Saturday, January 5, 2008

Using GroovyWS with the TerraService (Part 1)

I got recently a post on the difficulty of retrieving complex elements by some webservices, I will try to clarify some issues on this in this post using the Microsoft Terra webservice.
The TerraService is an WebService API which allows you to query the Microsoft TerraServer database of high resolution aerial imagery. Among other things, you can:
  • Find Geographic and Image Coordinates by Place Name
  • Convert coordinates from one projection system to another
  • Fetch tile meta-data and image-data
Today, I will just concentrate on using the search API to show how to deal with arrays with GroovyWS.

If you look at the TerraService WSDL you will notice that some functions return arrays. This is clear from the following part of the WSDL:


<s:element name="GetPlaceList">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="placeName" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="MaxItems" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="imagePresence" type="s:boolean"/>
</s:sequence>
</s:complexType>
</s:element>

<s:element name="GetPlaceListResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetPlaceListResult" type="tns:ArrayOfPlaceFacts"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfPlaceFacts">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="PlaceFacts" type="tns:PlaceFacts"/>
</s:sequence>
</s:complexType>

<wsdl:message name="GetPlaceListSoapIn">
<wsdl:part name="parameters" element="tns:GetPlaceList"/>
</wsdl:message>
<wsdl:message name="GetPlaceListSoapOut">
<wsdl:part name="parameters" element="tns:GetPlaceListResponse"/>
</wsdl:message>

<wsdl:operation name="GetPlaceList">
<wsdl:input message="tns:GetPlaceListSoapIn"/>
<wsdl:output message="tns:GetPlaceListSoapOut">
</wsdl:operation>

Therefore, the following call to GetPlaceList will return an object embedding an array of PlaceFacts.

def proxy = new WSClient("http://terraservice.net/TerraService.asmx?WSDL", this.class.classLoader)
def plist = proxy.GetPlaceList("mountain view", 5, true)

From this object, one can call getPlaceFacts to get the array. Here we asked for its size:

println plist.placeFacts.size()

Then one call go through all the places returned by the call to the TerraService:

for (pfact in plist.placeFacts) {
println pfact.place.city
println pfact.place.state
}

All this might look a bit cryptic, and it is probably. The fact is that there is no magic. When you want to call a remote service, you have to know its signature. If you don't have any manual then you have to read and understand the WSDL which represents the contract you are offered.

If you have any idea on how thing could be improved by GroovyWS for helping you in writing your scripts then do not hesitate to give your feedback.

Tuesday, January 1, 2008

Happy new year - Bonne année 2008

Quelques petites nouvelles de début d'année: 2007 s'est finie pour nous par un séjour de quelques jours à Malacca (Malaisie). Nul doute que soph postera prochainement une description plus complète de nos visites. J'en profite pour vous souhaitez une très bonne année 2008; quelle vous apporte surtout la santé (le reste suivra, j'en suis sûr).

Dans le chapître des bonnes résolutions (car point de nouvelle année sans de nouveaux objectifs), je vous dirais bien que j'essayerai d'être plus présent sur ces pages, que je prendrai plus de vacances, que je ferai le marathon de Singapour, .... M'enfin n'y croyez pas trop !

A toutes fins utiles, et pour ceux qui voudraient se joindre à moi, le marathon de Singapour aura lieu cette année le 7 décembre.

Sur ces bonnes résolutions, je vous laisse et n'hésitez pas à me communiquer vos bonnes résolutions en commentaires.

Sunday, December 16, 2007

GroowyWS 0.2.0

It has been a long time since I talked about GroovyWS ... Groovy has been out for a week now and I have been working a bit on GroovyWS as well so I decided to push a new version. The changes brought by this new version are minor in term of workload but they should (hopefully) made GroovyWS more usuable in a production environment.
The first change deals with https support. Now you cab use the WSClient with a serving using secure http. This has been tested using Amazon S3 web services:

def proxy = new WSClient("https://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl",..)

You can now create buckets :-)

The other major change is the support for servers using basic authentication. So if your server requests to have an "Authorization: Basic bG9naW46cGFzc3dk" header in the request, you are able to set the correct properties using:

System.props["http.user"] = "login"
System.props["http.password"] = "passwd"

Do not forget that the token transmitted is just a base64 encoding of "login:passwd", so basically your are transmitting your credentials in clear text :-). You don't trust me ? Try this:

echo -n 'login:passwd' | openssl enc -a -e

or

echo 'bG9naW46cGFzc3dk' | openssl enc -a -d

Only use this over https !

The last minor change is to publish the SOAPAction property in the WSDL generated by the server. This makes odd commercial products work with WSServer !

What are the next steps ? I would like to add WS-Security support. This is something I already have partially, I am missing a public server to test it and would like to know how you would envisage its usage using groovy. So if you have any idea, do not hesitate to contact me ...

Sunday, August 26, 2007

GroovySOAP is now GroovyWS

It has been a long time without news. The fact is that I have been moving from France to Singapore and that keeps me busy with and away from the keyboard. Anyway I am back. You probably now that CXF has taken over XFire. Thanks to the work done by Dan & Al, the move has been pretty fast and we now have the best from XFire & Celtix.
I thought it was time to move also from GroovySOAP (relying on XFire) to GroovyWS (build on top of CXF). What are the difference so far ? If you choose GroovyWS you will have to use Java5 but you will gain an easiest way to build your clients (no worries about the complex types, they are automatically generated) and a proxy support.
In the future, I will probably start to work on basic authentication as this has been asked several times.
The build can be found here.