Thursday Oct 16, 2008

Jersey samples

The samples supplied with Jersey are on the java.net maven repository as a single zip file or individually (browse from here).

All the of samples are capable of being executed from the command line or from NetBeans 6.5 beta/dev build (with the maven plug-in configured to an external maven distribution version 2.0.9 or greater). Maven makes it easy to distribute the samples without redistributing third party dependencies, such as Grizzly, GlassFish embedded, Spring, and Scala, some of which can be rather large.

Some samples have unit tests that avail of the Jersey client API. This serves a number of purposes:

  • How to use the Jersey client API;
  • How to deploy in embedded scenarios using Grizzly and GlassFish;
  • How to develop functionality tests for resources using JUnit; and
  • Limit or remove manual testing of the samples.

We plan to automate the testing of all samples and from this experience we plan to create a useful JAX-RS/Jersey testing framework that is capable of abstracting from the actual HTTP container used to deploy resource classes. The ultimate goals are to make it easier to test and to automate everything so that we can test continuously via Hudson. Potentially we can then spin stable releases as fast as Hudson slaves can test.

A sample i particularly like and i recently added is Sparklines, which was inspired by Joe Gregorio's Sparklines service and python implementation. Sparklines, "as defined by Tufte, are intense, simple, word-sized graphics".

Here is a snippet of the Sparklines resource class:

@Path("/")
@Produces("image/png")
public class SparklinesResource {
    List<Integer> data;

    @DefaultValue("20") @QueryParam("height") int imageHeight;

    Interval limits;

    EntityTag tag;

    public SparklinesResource(
            @QueryParam("d") IntegerList data,
            @DefaultValue("0,100") @QueryParam("limits") Interval limits,
            @Context Request request,
            @Context UriInfo ui) {
        if (data == null)
            throw new WebApplicationException(400);
        
        this.data = data;
        
        this.limits = limits;

        if (!limits.contains(data))
            throw new WebApplicationException(400);

        this.tag = computeEntityTag(ui.getRequestUri());
        if (request.getMethod().equals("GET")) {
            Response.ResponseBuilder rb = request.evaluatePreconditions(tag);
            if (rb != null)
                throw new WebApplicationException(rb.build());
        }
    }

Besides being a fun application with a practical use it uses a whole bunch of JAX-RS features in one Java class: query parameter with default values, application-specific classes for query parameters, parameters on fields, throwing exceptions in constructors, and preconditions where entity tags are generated from the information in the request URI.

For those wanting to move beyond 1.0 i have just added a simple Groovy sample to the 1.0.1-SNAPSHOT. It needs to be improved and I ain't that familiar with Groovy so if any one wants to contribute something more substantial to show off Groovy, JAX-RS and Jersey let me know.

And... on a tangential topic related to languages i have been taking a little look at Clojure. There is a very thought provoking presentation on line. For those of us not that familiar Lisp the syntactical "explosion" of braces of Clojure can be a bit overwhelming compared to Java at first, but, if one looks at Clojure's Java interoperation and then compares Clojure code with Java code with the following consideration, think of curly braces as just braces, then the Clojure syntax is OK and can be very conscise yet still readable, and it is often just the order of where "." and braces occur that changes. I also took a little look at the code, for what Clojure does the set Java code is rather small, often strange (as Rich Hickey says he tries to apply functional idioms to Java code), and has some very interesting implementatons of persistent (in the functional sense) data structures.

About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today