Friday, December 13, 2013

How to authenticate to grails from R using RCurl

The trick is to open the login page before actually trying to post the login data. I assume something is stored in the cookie, but I only know this is how it works. See also my question on stackoverflow: http://stackoverflow.com/questions/20355874/how-to-use-rcurl-to-authenticate-to-spring-security-grails-app/20370222#20370222

My simple approach to security tokens

I'm a big fan of the spring-security plug-in series for grails. It allows me to do almost anything concerning security issues while giving me faith that this works properly. One scenario I encountered now, however, was not covered. In order to secure mit JSON web-service actions I couldn't rely on spring security. Instead I wanted to create unique links just like in google's picasa, when you wanted to share an image with a 3rd party that does not necessarily have a picasa account like you do. I decided to implement this in a very simple fashion: I added a field String uuid to the domain class in question. Then I added a service class: Then all I needed to do is to modify controller actions like this: I would like to hear what you think about this approach. Is it save enough? Is there a more elegant solution?

Pitfall with grails projections

With grails projections one should keep in mind that when one of the associated values turns out to be null, the entire entry is ignored in the result list. I found numerous posts, where this problem is addressed by alternating the join mode from inner join to left outer join:

http://www.intelligrape.com/blog/2011/11/01/criteria-query-with-left-outer-join/
http://stackoverflow.com/questions/17083204/criteria-uses-inner-join-instead-left-join-approach-by-default-making-my-que
http://stackoverflow.com/questions/19390720/grails-2-x-createcriteria-or-doesnt-work-for-nested-associations/19391255#19391255

Now, I do something like this:

Speeding up JSON export in Grails

In one of my grails apps, I export a large quantity of data in JSON format. This process was incredibly slow (~30s) and somehow I was convinced that this was a database issue since numerous joins were involved as well. As it turns out, however, the database query took ~1s to complete so that I got curious what the other 29s were spent on. Very unexpectedly, I had to blame grails' native JSON converter. A google search revealed that this was already known:

http://sysgears.com/articles/speedup-json-parsing-grails/

I decided to follow the suggested solution and started playing around with Jackson. Pitfalls are that some Java objects seem to cause Jackson some trouble, e.g. if objects can't be serialized. I therefore decided to use projections to efficiently recover all the database fields I was interested in (probably speeding up the database access as well). Since all the fields were then strings, ints or doubles, conversion worked like a charm. The exact same conversion now takes only ~1s, adding up to ~2s in total.

This helped me to understand how to use Jackson:
http://wiki.fasterxml.com/JacksonInFiveMinutes

There even seems to be a grails plug-in available to replace the default JSON converter with Jackson:
https://github.com/sjhorn/grails-jackson

However, I didn't try it out since development does not appear to be active.

One last important thing to note is that if you want to use jackson to render JSON content you might get into conflict with the regular JSON converter if you try to do something along the lines of render AS JSON. What you need to do instead is:

Friday, March 15, 2013

Deploying a gails app with the cloud-foundry plug-in

I recently discovered that CloudFoundry accounts are free (at least for now) and this offers a perfect opportunity to install a demo instance of grails applications. As with most things in grails, this is pretty straight-forward if you forget about the one or two issues you need to solve beforehand. I basically followed Burt Beckwith's excellent guide here: http://blog.springsource.org/2011/04/12/one-step-deployment-with-grails-and-cloud-foundry/ There were only two things giving me a headache. One issue was that the application did not get enough memory (PermGen of course). This was solved easily by reading the documentation of the plug-in:
//increase memory size to 1G (2G is max), don't start immediately since we need to change JAVA_OPTS
grails cf-push --memory=1G --no-start
grails cf-env-add JAVA_OPTS "-Xmx1024m -XX:MaxPermSize=512m -XX:+CMSClassUnloadingEnabled"
grails cf-start
The second issue became apparent from the exception: liquibase.exception.ServiceNotFoundException: Could not find implementation of liquibase.logging.Logger Thinking about this for a second I remembered that Liquibase was used by the database migration plug-in. I just removed it from the runtime in BuildConfig.groovy and tadah everything was running fine! I also raised a jira for this: http://jira.grails.org/browse/GPCLOUDFOUNDRY-52