This project has moved

This project has been moved to jpoxy. Please check the jpoxy Google code page for latest updates.

Preamble

Explanation of standard formats and protocols:

  • JSON (JavaScript Object Notation) is a lightweight1 data-interchange format with language bindings for C, C++, C#, Java, JavaScript, Perl, TCL and many others.
  • JSON-RPC is a simple remote procedure call protocol similar to XML-RPC although it uses the lightweight JSON format instead of XML.

What is it?

I love the simplicity of JSON-RPC when it comes to rapidly devloping cutting-edge web applications.

I recently decided to start writing more server-side code in Java servlets in order to take advantage of cloud-based infrastructures such as the Google App Engine. Until now I have written my web applications using PHP as the server-side language. Specifically using frameworks such as Symfony or Kohana, which makes writing simple JSON-RPC services relatively trivial.

So I started looking for a simple JSON-RPC system I could use in Java to abstract my business logic classes from the mundane tasks associated with handling web requests. I found several packages which all claimed to implement the JSON-RPC protocol via Java servlets, however they seemed to require far more by way of setup than I wanted and they all seemed to require the developer to write applications to their specific implementation’s standards.2

So I decided to write yet another Java implementation of the JSON-RPC specification myself with the following goals in mind:

  • Easy to implement. Setup for this package should be kept at a minimum. This includes both development as well as production setup.
  • Easy to code in. Application developers using this class should not need to know much about JSON-RPC beyond exposing methods in their code that can be called remotely from other applications via the web.
  • Non-invasive. Developers using this implementation should be able to reuse plain old Java object (POJO) classes as much as possible, making the transport layer of JSON-RPC as transparent as possible.

With these goals in mind I set off to develop the package com.werxltd.jsonrpc to be a simple wrapper designed to be used inside of a standard .war project.

Setting it up

You can either download the .jar file here to include in your project manually or (and this is my preferred method) you can import the com.werxltd.jsonrpc package as a dependency in your Maven-managed project by specifying the following in your project’s pom.xml configuration file:

<repositories>
    <repository>
        <id>werxltd</id>
        <url>http://maven.werxltd.com </url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
           <enabled>true</enabled>
       </releases>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.werxltd</groupId>
        <artifactId>jsonrpc</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

Next, you’ll need to specify endpoints in your .war file’s web.xml configuration file. Here’s an example:

<web-app>
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>com.werxltd.jsonrpc.RPC</servlet-class>
        <init-param>
            <param-name>rpcclasses</param-name>
            <param-value>YourClass</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/example</url-pattern>
    </servlet-mapping>
</web-app>

That’s it! Now your project is configured to filter all requests sent to /example through the JSON-RPC class which examines the class name you passed in (in this case, YourClass) for public methods it can expose. An instance of your class is created internally if your class is not static and it will remain in memory throughout the life of the servlet. Any exceptions your class generates are gracefully wrapped inside a JSON-RPC error message for proper handling upstream.

Further configuration

Additional init-param options are available:

  • expose_methods – Whether to allow a user to list all available RPC methods. For public RPC services this makes sense whereas for private RPC implementations disabling this feature may provide additional security.
  • detailed_errors – Whether to print a full stack trace when displaying errors to the user. This needs to be disabled in production environments.
  • persist_class – Whether or not to create an instance of the class containing the desired method. Disabling this ensures a fresh copy of the object for each request. Enabling this option, however, will greatly improve performance but errors or memory leaks in the contained classes may cause problems in the application server.
  • use_full_classname – Whether to require the user to use the full class name plus a dot and then the method name
  • (ie: com.werxltd.jsonrpc.ClassB.test for the test method in the com.werxltd.jsonrpc.ClassB class)
  • . Enabling this option allows you to use multiple classes with the same public method names.

To enable any of these options, add an init-param line to your web.xml file. Ex:

<init-param>
	<param-name>use_full_classname</param-name>
	<param-value>true</param-value>
</init-param>

Using it

While the setup is pretty much straightforward, due to the loose typing found in JavaScript (and, as a result, JSON) there are some caveats in how methods are called. Specifically in how arguments are passed to those methods.

Scanned methods are stored internally with a signature consisting of the method name and how many arguements that method accepts. When a JSON-RPC request is made, the servlet determines how many parameters were included and attempts to match the method requested with a corresponding internal method which has the same number of parameters/arguements.

Because parameters are passed in via the web, only Java primitive data types along with three others, JSONObject, JSONArray and java.lang.String are accepted as valid parameter data types.

If a match of method name and number of parameters is found, an attempt is made to parse the passed-in data into the method’s required type. Any methods which accept as their first parameter a parameter of the JSONObject type, this method automatically takes prescience over all other parameters.

To use the JSON-RPC interface from another application, you must pass a valid JSON-RPC object to your final servlet as a parameter named “json” via either GET or POST. Here is an example of the valid JSON-RPC object you need to pass:

{
    "method":"add",
    "params":[1 2 3]
}

This JSON-RPC implementation accepts either named parameters or positional parameters like the ones shown above. Here is a named parameters example:

{
    "method":"echo",
    "params":{
        "text":"testing"
    }
}

You can either pass in a JSON object as the value of the ‘data’ or ‘json’ parameter or as component ‘method’ and ‘params’ parameters.

Events

This framework now supports simple event messaging, allowing your class to react to and even interact with servlet configuration, context, request, and response objects. To use this facility your class needs to extend the new JSONRPCEventListener interface and implement the messageReceived method, accepting one argument of the JSONRPCMessageEvent type.

Here is an example of how a class could implement a Simple JSON-RPC event listener:

public void messageReceived(JSONRPCMessageEvent me) {
	switch(me.message().getCode()) {
		case JSONRPCMessage.INIT:
			config = me.message().getServletConfig();
		break;
		case JSONRPCMessage.BEFOREREQUEST:
			request = me.message().getRequest();
			session = request.getSession(true);
		break;
		case JSONRPCMessage.BEFORERESPONSE:
			response = me.message().getHttpResponse();
			response.setContentType("text/html");
		break;
		case JSONRPCMessage.AFTERRESPONSE:
			lastresponse = me.message().getRPCResponse();
		break;
	}
}

The event facility also makes it easier to implement solutions that use sessions. It also make it possible for classes to react to the output of other classes, including errors.

License

We believe in open source, as such this project is licensed under the OSI approved MIT License.

The road ahead

Completed:

Planned:

  • figure out a way to allow passing a raw JSON object via regular servlet interfaces

Hope this helps someone else. I’m looking forward to using this class as a central component in many rich web projects I have planned.

  1. Lightweight in both size and resources required to process data encoded in JSON vs. XML. []
  2. I did find this project after finishing the first revision of my class. It looks great and like it would do much of what I wanted, however the code is proprietary. After I finish documenting my classes I plan on releasing them under an OSI-approved licence. If you are interested in helping me with this project, feel free to let me know! []