Doing RPC with REST
RPC doesn't really rhyme with REST. REST revolves around the concept of resources, and the basic methods offered by the HTTP protocol (being GET, PUT, POST, DELETE and some others). Now, sometimes you're confronted with something outside the scope of those methods. Say, for example, you want to be able to send a client's data to the ERP system by simply doing 1 REST call.
Option 1 is to create an URL template like this: http://host/client/{id}/sendToERP. But that isn't REST. sendToERP isn't a resource, it's an action. So that one's gone.
Option 2 is to create an URL template like this: http://host/client/{id}?action=sendToERP. Better, but to which HTTP method will you bind this. Is it a GET? A PUT? Not really sure. You're not really altering the client, but you're not asking for the data either...
So that brings us to option 3: the command pattern. Assume we have a URL template like this: http://host/client/{id}/command. Doing a get on this will return all the possible commands for that client. In our case this might be:
<commands>
<command id="1">
<type>sendToERP</type>
</command>
<command id="2">
<type>updateFromERP</type>
</command>
</commands>
So now we can do this to execute the sendToERP command: GET http://host/client/{id}/command/1. What does this return? Well, it can for example return the result of the command, like this:
<commandResult>
<result>
OK
</result>
</commandResult>
Code snippet: replacing java.util.Logger by SLF4J
This simple piece of code adapts all JUL statement to be forwarded to SLF4J:
java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
for (Handler handler : handlers) {
rootLogger.removeHandler(handler);
}
SLF4JBridgeHandler.install();
The first 5 lines are needed because otherwise JUL will still log to System.err.
Creating a custom binder for Spring RCP
Introduction
For those not familiar with RCP, a quick heads-up. Spring RCP is a Swing application framework, based on the Spring Framework. It utilizes many of Spring's available utilities, the Spring IOC container being the most important one.
Bindings and binders
Spring RCP gives you the possibility to create controls that are bound to certain properties of a given object. These bound controls are called bindings. A binder is a class that creates bindings. A binding factory, if you will.
Standard Spring RCP has some basic binders, but often you will encounter user requirements that require you to build a custom binder. For example, a binder to show a list of objects in a JTable, a binder to show a String in a formatted text field or even a binding to show and select an image. These are not standard in RCP, and you'll need to make those yourself.
Adding security to CXF web services
In my previous post, I demonstrated how easy it is to create REST services using Spring and CXF. Now I'll show you something even easier: adding security.
For this example, I'll start where I left off, which is a working REST service showing the time, and add basic HTTP authentication. I'll be using Spring Security 3 to add the security features. Spring Security 3 is relatively new and those used to version 2 will need a wee bit of adjustment (you'll need more jars, as they split them up).
Creating a REST service with CXF and Spring in 10 minutes
I love REST. It's easy, straightforward and above all: easily testable. When you're developing with REST, you have a lot of options to choose from. There's Jersey, the reference Sun implementation, then you have RestEasy, the JBoss choice, and there is CXF, the Apache choice. I chose CXF for this example simply because it's so easy to create REST services with it, without a lot of configuration.