Fork me on GitHub

Saturday, February 2, 2013

Quick bug fixing in Scala with SBT and Vim

UPDATE: Dave Cleaver created a SBT plugin do generate quickfix!

Until now if you wanted to have vim quickfix integration with SBT, you had two solution:
Unfortunately none of them are in my opinion usable for day to day development:
  • Launching SBT from Vim create a new instance of SBT at each call, and starting SBT takes time... lot of time.
  • VimSIDE is still very experimental (wasn't able to make it work outside of the demo project) and heavy, too heavy for me... one of the reason I love using Vim it's because it's light, fast and he don't try to change the code I write because he pretend to be smarter than me (like IntelliJ).
So, I like to use SBT in his own shell, running in interactive mode and enjoying the power of '~' for continuous compilation/testing.
The only thing is that each time I'm fixing compilation errors/warnings I have to navigate manually to the file and enter the line number to jump in the right place.

This time is over!
The solution is very simple (but very hacky!), just a single bash script which:
  • Append warn/error SBT messages into a temporary file
  • Monitor certain action done in the interactive session and delete the file when necessary
Let's see how the script looks like:
That's all! if you want to use it:
  • Download and make it executable
  • Add it to your path with a fancy name (hint: qsbt)
  • Edit your .vimrc to add the SBT error format pattern and a key-binding (check comments in the script for an example).
And now enjoy continuous quick-fixing by starting sbt with the qsbt script!

Monday, May 16, 2011

A simple (REST) web service client in Scala

For the purpose of creating a nice API for my new project Caligo (more on this in a future blog post), I was looking for a simple solution to access REST web service in Scala.

My requirement was simple: access an HTTP web service, and exchange data with him using the JSON format.

During my experiment I came across theses two nice Scala libraries:

Dispatch seems to be the only available HTTP client for Scala today, and since it's based on the Apache's one no need to worry about his reliability or compatibility.

SJSON was choose against Lift-JSON due to it's better handling of reflection on beans, both of them did a great job on case classes, but only SJSON was effective on beans (was mandatory for me since I must do polymorphism on my model and case classes do not support this).

Lift-JSON support of beans (thru reflection) must be improved on the version targeting Scala 2.9

Here is a sample usage of the frameworks, both of them are easily integrated, and using the power of the Scala syntax a full client request doesn't take that much code to be implemented (in this case 4 lines of code, model included):

Tuesday, June 22, 2010

Vim is everywhere

I'm now sure that vim is the perfect tool to write *clean* code !

And for vim fanatics, you can even get vim inside NetBeans (integration work much better than in Eclipse):

Friday, April 30, 2010

Spring Batch integration module for GridGain

For the purpose of using Spring Batch in a scalable and distributed manner to process huge amount of data, I am actually developing some components to make integration of Spring Batch with compute/data grid easier.

Different solutions is offered by Spring Batch to provide scalability, the one that best suit my needs is remote chunking.

As I already done some investigation before using GridGain I chose this framework to implement a distributed remote chunking system that can be easily integrated into any existing Spring Batch systems.

Using GridGain is really straightforward, and setting up a grid on a development machine doesn't need so much configuration.

The only issue I faced is due to the fact that GridGain use serialization to deploy tasks on nodes, in order to be able to deploy a remote ChunkProcessor, it must contains serializable ItemProcessor and ItemWriter, which unfortunately is not the case by default.

So instead of creating new interfaces, I made a SerializableChunkProcessor which only accept serializable ItemProcessor and ItemWriter. It's surely not the smarter solution, but since I can't modify default interfaces in Spring Batch and I don't want to create my own interfaces, this workaround will suffice.

Here is the job application context used for the integration test, as you can see the 'real' ItemProcessor / ItemWriter are injected into the GridGain chunk writer:

You can download the spring-batch-integration-gridgain module here:

If you want to see a full working sample, take a look at the integration test. The full project sources can be downloaded here:

Thursday, March 25, 2010

Integrating failover strategy for Spring Security NTLM

During implementation of the NTLM authentication into our application, I wanted to achieve failover to standard login page (html form) if NTLM authentication fail.

In order to obtain the wanted behavior I extended some Spring Security classes and hack them a little bit, so I created:
  • An AuthenticationEntryPoint strategy to switch between NTLM or standard login
  • A custom AuthenticationProvider to prevent password checking when user authenticate with NTLM
  • A custom NtlmProcessingFilter to disable NTLM authentication if a remember-me cookie is present
The thing I was unable to do is detecting if the user client is NTLM compliant before starting the NTLM challenge. Due to the way the protocol is build (and certainly for security reasons) it's impossible to know if the client is NTLM compliant before launching the challenge.

So let's took the scenario of a Google Chrome user:
  1. A popup (HTTP realm) authentication window is shown to the user
  2. He hit enter with invalid credentials (i.e. empty username/password)
  3. The browser is redirected to the standard html login form
By using 'remember me' the NTLM authentication won't be shown next time the user access the site due to the presence of the cookie.

If the user client is Internet Explorer no authentication window is shown because the navigator use directly the information of the logged user. If the authentication fail, then the standard html login form is displayed letting him login with other credentials.

This implementation was made using Spring Security 2.0.5, but can be easly converted to Spring Security 3.0 using the Spring Security NTLM 3 module I recently created. Don't hesitate to ask me if you want that I publish an updated version.

You can download a sample application including hacked class here:

Sunday, March 21, 2010

Spring Security NTLM 3

As of version 3.X Spring Security doesn't include the NTLM extension anymore.

For the purpose of using NTLM authentication on Spring 3 projects, I migrated the 2.0.5 NTLM extension to the new version of Spring Security.

The full project sources are available here:

I haven't took time fixing the following bugs found on version 2.0.5:

If your are interested to help correcting this defects don't hesitate to get in touch.

You can download the spring-security-ntlm snapshot here:

Due to the fact that NTLM isn't integrated in new version of spring-security-core, you need to add a custom filter.
Here is a snippet describing briefly how to do that:
Don't hesitate to ask if you need help, this extension work exactly as version 2.0.5. You can use an example from version 2.0.5 if you are starting from scratch.

Saturday, December 19, 2009

Integrating Spring Security with NTLM + Crowd

For the purpose of implementing a SSO (Single Sign On) system into our application, I investigated the use of spring-security and successfully implemented it using NTLM and Crowd.

Download Sample Project

As most of our users are using Internet Explorer, NTLM seems to be the best solution. By the way, as developer we are using Firefox and fortunately Firefox can provide too NTLM credentials (see this to enableNTLM inside Firefox).
NTLM is used for authentication only (giving windows credentials to the web application) then Crowd is used to store and retrieve user's authorizations.
Atlassian Crowd provide full spring-security integration, and a bunch of connector to communicate with most of directory services available.
I first configured Crowd to use an internal directory to avoid being disrupted by communication with external directories. Then I added connection to our Microsoft Active Directory... but this go beyond the scope of this blog entry.

  • Add spring-security and spring-security-ntlm and theirs dependency in your project
  • Add crowd-integration-client and his dependency in your project 
  • Integrate Crowd integration client by following this documentation from step 1 to 3.0 
First of all, create a new spring application context (applicationContext-security.xml) with the following content :

Then add it in your web application descriptor (web.xml) with the spring security filter, you must end with something like this :

And finally, create the custom UserDetailsAuthenticationProvider (unfortunally I was suprised to didn't find a provider that suit my needs...):

You're done !

Creating test pages
As you may have notified, the file applicationContext-security.xml defining three different level of security:

  • Anonymous access authorized by default to all page
  • Standard users can access the 'secure.jsp' page
  • Administrators can access the 'admin.jsp' page
So create the pages index.jsp, secure.jsp and admin.jsp with the content you want or change the security context to suit your needs.

Crowd configuration
Open the Crowd administrator console, and create user corresponding to your Active Directory account name, add it to the application you created when you integrated Crowd (more informations).
Create two groups (myapp-user and myapp-administrators, you can name it differently but you must then update your spring security context correspondingly) and add them to the user created before.
Don't hesitate to read the Crowd documentation for further information. Specially if you want add a connector for your directory, but I strongly recommend you to first test your application using a Crowd integrated directory.

Integrating spring-security into an existing web-application is relatively straight-forward, specially if you already using spring. Some specific configuration is needed to enable NTLM and Crowd support but documentation can be easily find online.
And best of all, if you want to change for Kerberos in future you can do it simply and without changing the way your application works... thanks to Spring IoC :-D
And thanks to Crowd you can change connection to different directory services without loosing user authorizations (of course user-name must be the same...).