Friday, September 28, 2012

Using Spring Security with CXF in Grails

CXF plugin is super easy way to add SOAP web service to Grails application. It creates regular service class, with few specific parameters and voila - you have SOAP. But what if you want security?
REST web services are native citizens in Grails - they are just actions in controllers, so you can use Spring Security annotations to check permissions. But there is no annotations for services. There are two easy options.
First, you can restrict actions by defining static rules. This is very simple, but not very flexible as it does not allow to configure permissions on method level, you will have to create different services for different permissions. Also, old clients may not work if WSDL requires authentication.
Second method, is to check permissions manually. In practice this is similar to annotations, and can be converted to annotations easily. There is example:

  Secret secret(int id) {
    return Secret.get(id)

  void checkRights(String rights) {
    if (!SpringSecurityUtils.ifAllGranted(rights)) {
      throw new IllegalAccessException("You don't have permission")

Finding similar strings in Java

Finding equal strings in Java is very easy, as well as finding substrings. But what if you have strings that are almost similar (at least from human point of view). For example: "John Doe" and "Doo Jonh".
Recently, I had to do data migration for table with very poor data quality. There is a lot of theory and books on finding duplicates, but surprisingly, there is very little actual Java implementations that could help with such things. Fortunately, there is at least one good - Simmetrics, there is even example how to use it, but it is as simple as one can imagine:


AbstractStringMetric metric = new MongeElkan()
println ( ['John John', 'Doo Jonh', 'ddddddd', '7777777777'].max { metric.getSimilarity('John Doe', it) })

Doo Jonh

There is a bunch of different algorithms one can use, but I found MongeElkan to be best for my case.

Tuesday, September 18, 2012

Javamelody SQL monitoring with Grails plugin

Javamelody is amazing tool for live monitoring. It has great Grails plugin which makes it very easy to use. Only issue I had with it is SQL monitoring configuration.
According to documentation there are several ways to configure data sources:
  1. magic - you don't have to do anything, it just works
  2. javamelody.datasources property in GrailsMelodyConfig.groovy - you just specify datasources
  3. replacing JDBC driver with Javamelody proxy
  4. specifying java option - javamelody.datasources
Unfortunately, all internal configuration options (1, 2, 3) didn't worked. As I investigated (quickly) it looks like there is no dataSource bean available for BeanPostProcessor and it tries to proxy after all connections were already picked by application.
So only specifying java command line option worked for me. Otherwise - great tool. It is also possible to specify multiple data sources with command line like: