Thursday October 02, 2008

Java RESTful clients and JAX-RS

Filed Under: Java — Solomon @ Oct 02 2008, 05:39:01 PM EDT

RESTful architecture and JAX-RS lead to distributed architectures. Roy Fielding's dissertation talks about a layered, client-server architecture. The JAX-RS Spec provides a great way to develop the server-side components, but doesn't describe how to develop the client-side components needed for a layered architecture. Note, I'm not talking about the browser as a client; rather, I'm describing Java-to-Remote-Resource communication. JAX-RS doesn't specify how a client API should be built. However, the various JAX-RS implementations does provide a client API. The client also seems to be the #1 requested feature for the next JAX-RS spec.

Before I get into the specifics of each client, I'll take the liberty to categorize different types/levels of RESTful clients I've seen that come bundled with JAX-RS projects:

Raw Data Communication - There are existing client APIs such as java.net.URL and URLConnection and the more versatile HTTPClient. The java.net infrastructure allows you to make connections and get/post data. HTTPClient is more powerful than URLConnection, and provides lots of ways to manage the communication process but doesn't address how you should interpret the results of a GET into a Java object or convert a Java object into data for a POST.

DTO Binding - You usually need a Java Object to encapsulate the raw data of RESTful communication. In a browser, you can easily convert JSon text into a JavaScript object using "eval()." RESTful Java clients can do something similar by using a Representation to Object transformation framework, such as JAXB2 -- which is favored by JAX-RS. If you're already using JAXB2 to provide a service (object-to-representation), you might as well re-use JAXB2 on the client side; that allows for seamless client-to-server communication, at least from a development standpoint. Write your transformation objecct once, use everywhere.

Theoretically, clients with JAXB2 can be used to read data from any RESTful service. Write your service in Rails, Grails, or DJango and use that service with a "JAX-RS" client.

Service Proxy - JAX-RS provides a nice abstraction on how you map a URL to a service layer method and of course how you transform XML into an object (and vice versa). Some JAX-RS clients promote creating a Java interface on which you put your JAX-RS annotations. Those JAX-RS implementations then allow you to create a proxy to the remote services using that same Java interface coupled with a root URL. Those client proxies reduce boiler plate code considerably.

Theoretically, the service proxy approach can be flexible enough to perform a whole bunch of custom modifications to the communication process to perform "cross-cutting" functionality like security management and custom business logic related to the communication. One of my favorite custom cross-cutting business logic scenarios is "time-machine functionality" which involves adding some common URL parameters (&date=...). Additionally, this approach can theoretically be used when communicating with non-JAX-RS implemented services (Rails...).

This entry is part of my ongoing comparisons of JAX-RS frameworks. My next entry will include snippets collected from blogs and sample applications on how each of the four JAX-RS frameworks implements much needed RESTful client. I'll throw in some implementations of URLConnection and HTTPClient code as well.

Friday September 26, 2008

JAX-RS Spring Integration

Filed Under: Java — Solomon @ Sep 26 2008, 01:42:10 PM EDT

The four current JAX-RS implementations provide Spring integration, even JBoss's RESTEasy. Each JAX-RS implementation has its own Servlet adapter implementation. The spring integration comes in the form of extending the default Servlet and plugging in a Spring Application Context. Beans are then created with some XML and potentially Spring component scanning.

Jersey - Here's a tutorial in building a Jersey Web App. Switch the Servlet to SpringServlet. Here's a Spring integration tutorial from the Jersey Team.

RESTEasy - The RESTEasy Spring integratino works similarly to the Jersey implementation, but does seem to take advantage of a few more Spring features, and does seem to do some component scanning

Restlet - The Restlet integration with Spring is pretty slick and highly configurable.

CXF - The CXF integration with Spring has a deeper integration with Spring. There are jaxrs and jaxws namespaces. CXF/Spring allows you to configure two instance of the same Resource under different root URLs. It also has a client API that's exposed via Spring.

There are really fantastic Spring/JAX-RS capabilities through out the four implementations, but I think that CXF takes my vote for "Best Spring integration in a JAX-RS product." The one request I'd have is to integrate with Spring MVC's Handler Mapping (find a Spring bean that corresponds with to the URL) and HandlerAdapter (given the current instance - controller/@controller/service/resource/servlet/action, figure out how to send the request and response to it). I'm not sure how hard it would be to implement, but if it were simple to do, it would add some pretty interesting functionality.

If I'm missing something, please let me know! I'm in the process of choosing a JAX-RS vendor, and Spring integration is at the top of my list.

Friday September 26, 2008

JAX-RS Vendor Comparisons - Part I

Filed Under: Java — Solomon @ Sep 26 2008, 10:20:47 AM EDT

JAX-RS Vendor Comparison

I'd like to evaluate the four products that I know of that support JAX-RS:

CXF - this Apache hosted project is a merger between XFire (a SOAP Framework) and Celtix (an Open Source ESB, sponsored by IONA)

Jersey - the production ready, reference implementation from Sun

RESTEasy - JBoss's JAX-RS project, from the mind of Angry Bill Burke. (I say that with great respect)

Restlet - This project has been around for a long time and implemented REST before REST was popular. JAX-RS was a natural extension

I'm interested in comparing the following aspects of the JAX-RS implementations that extend beyond "pure" JAX-RS:

Maturity of the product - We need mature features and a mature and active user community are pretty important. We don't want to encounter too many problems, and when we do, we want to make sure that we can solve those problems quickly. JAX-RS is a standard API which is implemented using existing infrastructures. There will be plenty of "infrastructure" type hurdles along the way, so we'll be a lot more secure knowing that we're using a mature infrastructure

Server-side Integration Strategies - I have an existing Spring/Hibernate infrastructure and a strong familiarity developing on that infrastructure stack. Each product integrates with Spring, but there are different degrees of integration. There are also built-in integration with other development stacks, for example EJB.

Java Client API - There is no JAX-RS Client API, so each JAX-RS implementation has it's own take on how to perform Java-to-Remote-RESTful-Resource calls. There are pros and cons to each approach. Theoretically, we can have a client from one product call a service from another product. I need the ability to change all client calls by adding some query parameters (security and "time-machine" parameters to test different date scenarios). I also

Product Purpose - Jersey is meant as a reference implementation. RESTEasy is a playground for new ideas. CXF has an "enterprise version" supported by IONA. The Restlet project grew out of a desire to have a RESTful API alternative.

Of course there's more to it than those brief introductions, but the original purpose and current status of a projecct will play into vendor selection decisions.

Other Service Providing Models - The JAX-RS is a common API that can wrap an existing infrastructures, but a couple of the produts have APIs that predate JAX-RS and can be used in combination with JAX-RS development. For example, CXF (an ESB) provides plenty of transport mechanisms and exposes SOAP services via JAX-WS. Restlet's main RESTful API is not annotation based, and .

Configurability - I need to solve problems like cross-domain issues and browser performance. I'd like the flexibility to wrap JSon in a method, return HTML fragments and return dynamically created JavaScript with document.writes. The method-wrapped JSon approach is used in combination with mapping extensions to mimeTypes. I can do more interesting things with extensible configuration options, as long as the extension points are exposed in a clear and simple way.

Security - "How do I secure my RESTful service?" is a pretty common question. JAX-RS does have a notion of a Security Context, but leaves a lot of the specifics to the implementation.

Performance - There are quite a few components to performance testing these containers, including representation-object binding speed, requests/some-time-frame metrics, memory consumption and the impact of adding a large number of services. I definitely won't have enough time to perform an exhaustive study on this... Perhaps this is a good topic for William Louth :) .

I'll give some background on what I need, to both expose my biases and my needs. We have a large website -- let's call it NationalPark.com. There is a large section devoted to functionality that's self contained and sufficiently different from the rest of the site -- let's call it RestArea. The functionality of RestArea is reused in a handful of deployments: a couple on NationalPark.com and a few other deployments on other sites. NationalPark.com and the other websites are all developed on the Spring/Hibernate stak. We are rethinking the underlying functionalty of RestArea, and want to enrich the user experience of RestArea. We also want to separate RestArea deployment from the rest of NationalPark.com so that problems in RestArea don't spill over into the rest of NationalPark.com. A SOA/SaaS model will give us all of those features, but will also let us have more deployment options across the rest of our websites... We can deploy RestArea into one manageable environment across all of our websites, or we can deploy RestArea once per unique function. Did I mention that there will be somewhere between a million and two million users of the RestArea across our websites?

I plan on doing more research on these aspects and I'll be posting some blog updates over the next few days. I posted an entry about Spring Integration. I'll follow it up with Client API comparisons next week. Eventually, all of this research can hopefully be used for a comprehensive comparison article.

Any input you have on comparing JAX-RS products would be greatly appreciated!

Monday September 22, 2008

JAX-RS and WebSphere 6.1 possible?

Filed Under: Java — Solomon @ Sep 22 2008, 03:12:56 PM EDT

Is it possible to somehow use JAX-RS in WebSphere?

Thursday September 11, 2008

What's REST Anyway? Distilling The Dissertation

Filed Under: Java — Solomon @ Sep 11 2008, 10:33:09 AM EDT

This introduction to REST explicitly simplifies REST as

a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do)... the five key principles are
Give every "thing" an ID Link things together Use standard methods Resources with multiple representations Communicate statelessly

It's definitely a pretty good description of describing REST principles to REST-less newbies, but I think I'll supplement them with the formal principles as dictated by Roy Fielding. As an aside, among his other accomplishments Roy Fielding has "co-founded the Apache HTTP Server Project and [is] on the board of directors of The Apache Software Foundation." He was also on the standards committee for URIs, HTTP and HTML. He literally wrote the book on REST in his PhD dissertation: Architectural Styles and the Design of Network-based Software Architectures

From Chapter 6.1: Standardizing the Web

The name "Representational State Transfer" is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through the application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.

Here's part of the introduction

REST provides a set of architectural constraints that, when applied as a whole, emphasizes scalability of component interactions, generality of interfaces, independent deployment of components, and intermediary components to reduce interaction latency, enforce security, and encapsulate legacy systems.

From the closing remarks of Chapter 5: Representational State Transfer (REST)

REST component interactions are structured in a layered client-server style, but the added constraints of the generic resource interface create the opportunity for substitutability and inspection by intermediaries. Requests and responses have the appearance of a remote invocation style, but REST messages are targeted at a conceptual resource rather than an implementation identifier.

REST comes down to the following principles (quoted from Chapter 5: Deriving REST):

Client Server - the separation allows the components to evolve independently, thus supporting the Internet-scale requirement of multiple organizational domains.

Stateless - each request from client to server must contain all of the information necessary to understand the request.

Cache - Cache constraints require that the data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable

Uniform Interface - REST is defined by four interface constraints: identification of resources; manipulation of resources through representations; self-descriptive messages; and, hypermedia as the engine of application state

Layered System - By restricting knowledge of the system to a single layer, we place a bound on the overall system complexity and promote substrate independence

Code-On-Demand - REST allows client functionality to be extended by downloading and executing code in the form of applets or scripts

I hope I didn't leave anything out important...

Wednesday September 10, 2008

RESTful Development

Filed Under: Java — Solomon @ Sep 10 2008, 10:23:48 AM EDT

Web-based systems have to perform increasingly complex functionality, a lot of which is client-side, but a hefty portion of it requires complex server-side development. That complex web development is becoming reliant on implementing RESTful architectural principles, similar to those found in Ruby on Rails. A new wave of RESTful development is aimed at the Entprise in the guise of RESTful Web Services. Development of RESTful Enterprise systems is becoming increasingly simpler for both the Enterprise Services and the Web camps through frameworks such as Grails, JAX-RS, WCF, ADO.Net Data Services and a new batch of statically-typed RESTful MVC technologies built on enterprisey technologies. The non-scripting frameworks definitely do have some catch up work, but they're all coming along nicely. The non-scripting REST environments have a disadvantage of not having language features that make development scripting environemts faster, but there are also plenty of examples of good engineering that has been ported over from the scriptng solutions to their non-scripting counterparts.

It doesn't seem like REST made it into the Enterprise in a big way yet, but the developments of the Big Boys' RESTful frameworks is likely to engender Enterprise interest over the coming months and years.

I've come across quite a few interesting features in various RESTful frameworks. I've also thought up some potentially interesting features in the development that may or may not exist yet that will probably prove useful in an enterprise; I may be playing the role of Astronaut Architect, so please feel free to flame any of my ideas that you find lame.

Centralized + Localized URL Mapping Mechanisms - Being able to describe a RESTful endpoint via an annotation (localized URL Mapping) is really fantastic. However, annotations are static and can limit reusability. For example, let's say I have a furniture e-commerce application. I may want separate URLs for sofas ("/catalog/sofas") and chairs ("/catalog/chairs") and still have a single back-end service/controller object servicing them. Grails can do this via it's URL Mapping mechanism. Spring MVC also has some built in mechanisms for this. I've worked with an awesome in-house regex-based, hierarchical URLMapping utility, that among other things, converts RESTful URLs ("/catalog/sofas/5921") to a format our MVC framework supported ("/catalogController.do?type=sofas&id=5921").

A centralized URLMapping/dispatching system also has a few more advantages, like a natural place for interception techniques and allowing for convention-over-configuration.

Multiple types of REpresentation - JAXB (JAXB 2.0) and XStream can both transform objects into XML, JSON or other formats. They are extensible and configurable. One of the two techonologies I mentioned can at the core of just about every single RESTful and SOA java technology.

Integration with text generating technologies - For example: JSP, FreeMarker, Velocity, SiteMesh, Tiles and others. Preferably, I'd be able to choose the technologies that best serve my needs. You'll likely need an HTML REpresentation in a RESTful environment. Even pure "RESTful Web Service" (such as JAX-RS) scenarios can benefit from this type of integration. Bill de hÓra uses StringTemplate to create relatively complicated XML in a JAX-RS environment, and Paul Sandoz of the JAX-RS and Jersey (the JAX-RS reference implementation) teams shows how Jersey integates with JSP. Scripting languages usually have complex String manipulation built into the language don't need this type of integration as much, even though they're likely to have such integrations. Groovy GString, Groovy XML and Scala's built first class XML are prime examples of scripting language features that make XML a breaze.

DOM Integration - I found that programmatic DOM manipulation is a pretty powerful complementary tool to domain object/POJOs. Bill de hÓra's example of converting of an object into a different format could have just as easily been performed in a non-template based solution. Groovy's DOM-like builder functionality is in fact one of the reasons that Groovy is so good at writting XML. A scripting environment's language features may be a bit more elegant, but a well designed DOM API can come pretty close in elegance.

My preference for this type of manipulation is Dom4J. I do wish that Dom4J would be updated for a JDK 5+ environment (specifically generics), but that would just be icing on the cake. I haven't played around with the Dom4J/XSteam integration, but it would be pretty slick to create a Dom4J document and write out JSon using XStream.

IMHO, DOM-to-REpresentational format, would be a great asset to an existing Object-to-REpresentational format for complex conversions between an internal domain model and an external one. Is "Object <-> DOM <-> REpresentation" possible with JAXB and XStream technologies? It may not be a commonly requested feature, but IMHO it would be a powerful addition.

Binding to REpresentation should allow POST/GET parameters in addition to XML/JSon - Servlets allow access to GET and POST data via a uniform API. AJAXy and Servicey systems use POST of JSon/XML data and GETs with parameters placed directly from the URL. I have plenty of legacy end-points (a.k.a. Controllers) that I'd love to use in a RESTful/Ajaxy way, but can't because of differences between the two POSTing techniques. (Note: I do have a solution for the URL parameters via the Centralized URLMapping). Shouldn't

order.customer=239852
be equivalent to
<order><customer>239852</customer></order>
? I did have a hack to bridge the two communication mechanisms, but I'd rather have had an integrated, well thought out solution.

Validation and Exception Handling - There are (at least) two forms of validation that are required to ensure proper behavior: 1) structural validation (the client passes along the right set of data, data sizes are correct) and 2) server-side business validation (For example: "you must have a unique user id," and "you're not authorized to access this data"). Communication of some of those errors can be performed through published interfaces (WADL), through status codes (401 = Not Authorized) and through messaging expressed in REpresentational formats (similar to Grails' validation).

Configurable REpresentational Negotiation Strategy - I should be able to tell the system that I'd like an extension (XML - .xml, JSON - .json, RSS - .rss, HTML - no extension) to be how the client and server negotiate content type (content_type=XML/JSon/YAML/RSS). I should also be able to use a GET/POST parameter or the "accept" header (text/html, application/rss). I should even be able to handle more than one strategy.

MVC RESTfully Smart View - AFAIK, developing a JAX-RS application, for the most part, consists of developing client-server "service" methods. You can apply annotations to describe how to REpresent the return value to the client. For example:

@Path("/customerservice/")
@ProduceMime("application/xml")
public class CustomerService {

  public CustomerService() {
  }

  @GET
  public Customers getCustomers() {
    ......
  }

  ...
}

Paul Sandoz of the JAX-RS and Jersey teams shows how Jersey implements MVC. While MVC may be possible in Jersey, it doesn't look nearly as robust as a full-blown MVC approach. I could be wrong, but AFAIK, If you want to expose your external domain model as both JSon or XML across the board in a JAX-RS, you'd have to create duplicate methods for each domain type/VERB combination to achieve that overall affect. I'm not sure if you can return more than one value to be rendered in JAX-RS. What happens if you need to render a Web Form with some collateral data, such as the ubiquitous State pull down? What happens if you new to show more than one type of value in either HTML form or XML form?

It's theoretically possible for an MVC system to have a "Smart View" that would be able to chose between rendering types based on context. This can work in conjunction with your REpresentational Negotiation Strategy. The "Smart View" can determine if the URL has a .xml extension or an XML "accept" header. Theoretically a "Smart View" can incorporate a custom data conversion strategy (either DOM manipulation or converting the result from one POJO to another POJO) and then render the result based on the REpresentational Negotiation Strategy.

There are definitely trade-offs between JAX-RS's client-server approach and this theoretical "Smart View" MVC. Client-server approaches are much simpler to understand. Full-blown MVC frameworks have a lot more capabilities and much more complexity. The popular Java based MVC frameworks, however, don't currently easily support the types of REpresentational functionality found in JAX-RS, but that's changing rapidly. Spring @MVC is going to get a RESTful upgrade in Spring 3.0, and frameworks such as Mattress (Spring based) and Fulworx (XWorks based, like Struts2) are paving the RESTful way with innovation. I assume that there will be more MVC-type capabilities built on top of JAX-RS. I'll definitely be watching this space.

Domain Addressability - This is a bit of a tough requirement. The domain model is controlled by some persistence mechanism (JPA for example), and may require numerical ids. The persistence mechanism is usually the "bottom" layer, meaning it's the farthest, execution-wise, from the origination call, and is usually an internal, abstracted implementation detail. External systems need to be able to identify the data through a URL that has different dynamics and access rules than the persistence layer. For example, take this fragment of a shopping cart application:

  <order total="...">
   <customer id="http://mycompany.com/customers/239852" />
   <items>
     <item id="http://mycompany.com/catalog/tables/49863"
       quantity="1"/>
     <item id="http://mycompany.com/catalog/chairs/28352"
       quantity="4"/>
   </items>
  </order>
  

If this data had to end up in a relational database, you'd have a whole bunch of tables that represent the entities and the relationships, probably with numeric ids and no URLs. It's also very likely that the "tables" and "chairs" URLs map to the same database table. Mapping between URLs and internal domain is non trivial. I haven't seen a consistent strategy for handling this mapping; that might be because I'm unaware of existing techniques...

We had some complex addressibility requirements on my last project because of SEO and Informational Architecture needs. We broke down our URL structure based on the meaning of the content to the end user; we didn't base our URL structures based on the internal representation of the content. We centralized the URL creation logic and exposed it via a Service object and a JSP tag. To add on to that, we also had guided navigation based on previous contexts. If you came to see a contemporary chair through the "style" catalog, we put some information into URL parameters so that we'd show other furniture of similar styles in the sidebar, instead of the default of other chairs.

The Domain Addressability strategy should also work well with the REpresentational Negotiation strategy. For example, if you had a URL of http://mycompany.com/order/1281.xml, the controller should return something like:

  <order url="..." total="...">
   <customer id="http://mycompany.com/customers/239852.xml" />
   <items>
    <item id="http://mycompany.com/catalog/tables/49863.xml"
      quantity="1"/>
    <item id="http://mycompany.com/catalog/chairs/28352.xml"
      quantity="4"/>
   </items>
  </order>
  

I haven't seen any existing implementations of this idea. Any help suggestions or existing implementations of this idea would be appreciated

Object REpresentation Mapping (ORM) - There should be easy ways to have end-to-end integration between the domain persistence technology, REpresentational transformer, and service end-point/URL. This idea has three aspects to it: 1) easily exposing data from a persistence store through REST, 2) easily finding granular data over REST 3) RESTful data "queries."

Exposing and reading a single granular entity (@Entity?) or a java.util.Collection of entities via a RESTful end-point should either come out of the box, or should be extremely easy to implement. In Grails/Rails, you can do this by pointing a script at a database. In MS ADO.NET Data Services, you can do that through some configuration and writing some ORM PONOs. How easy is that to do in JAX-RS? It looks pretty straight forward to me, if you're using NetBeans 6.1

It's the client side engine that really makes ADO.NET shine. A colleague at SunGard Consulting Services, Michael Heydt, said:

Having had my head buried in Silverlight for most of the year, I'm going to go out on a limb and make a bold statement that the real important part of REST in WCF / ADO.NET Data Services is really to support Silverlight based applications. Sure, AJAX is in place and it will support that, but it really seems to me that the future is Silverlight back to REST enabled data.

Having worked exclusively in HTML based technologies, I don't necessarily agree with the support focus of RIA, but I do agree with the general sentiment about the usefulness of the technology stack.

IMHO, It would be pretty interesting to have a Hibernate-like proxying client-side solution that could automatically retrieve lazily loaded data on request over REST. Qi4J RESTful implementation using Semantic Web technologies such as RDF and SPARQL is definitely a method worthy of exploration.

Integrated Security - Security is one of the top concerns about ADO.NET's RESTful services. REST doesn't say much about how you go about performing secure operations. Luckily, there definitely are plenty of methods a web-based (and there-fore inherently apply to REST over HTTP) applications can be secured. A RSTFul development framework should provide a way of integrating those security methods. Security concerns will definitely limit RESTful Enterprise deployment.

Configurable Client - RESTful environments can very likely be a collection of interlocking systems rather than a single server. A good client is indispensable. A good client knows a "logical name" of the RESTful service it needs. Bottom line is, environmental configuration is pretty darn important in a RESTful system.

JAX-RS doesn't have a standardized client API yet, but the three implementations of JAX-RS that I know of (CXF, Jersy and RESTEasy) all have client APIs.

Cachabilty - Caching is a big part of a RESTful architecture, and should be supported by both the server and the client of a RESTful environment.

A server-side development infrastructure should include the ability to set caching headers and ETag headers. Doing that via annotations would be pretty consistent with most of the newer frameworks.

A client-side development infrastructure could include the ability to cache results based on Caching headers and ETags. A browser does this. I'm not quite sure which existing caching technology could be used for RESETful client-side caching. I'm used to EHCache, usually with Hibernate, where the application server (the database's client) controls both the caching strategy (should an object be cached, how long to cache objects, versioning) and the caching implementation (performing the caching to disk/memory); RESTful caching requires the server to determine cache strategy, and the client to determine implementation. I don't know if that kind of functionality exists.

JS framework integration - DWR does some pretty amazing stuff with converting POJOs into JavaScript accessible services and JSon data. It also does some pretty slick performance enhancements by controlling aspects of both the client-side and server-side. I would imagine that it could theoretically integrate with JSR 311 compliant servers.

I've put up quite a few ideas in this article about what I'd like out of a RESTful development infrastructure. Some are ideas that have been implemented, and some may not even be easily implementable.

If you have any other ideas about a RESTful environment, or some other comment about the ideas here, please let me know. The ideas that I'm most interesting in fleshing out are the Domain Addressability and Object REpresentation Mapping (ORM) ideas. The Cachable Client idea isn't that far behind, and integrates with the ORM idea. Again, feel free to flame, I don't generally moderate the comments.

Tuesday September 02, 2008

Tidbits about JAX-RS

Filed Under: Java — Solomon @ Sep 02 2008, 11:35:22 AM EDT
James Strachan likes JAX-RS and Jersey. He calls JAX-RS "Railsish" and points to a blog entry about Explicit and Implicit MVC in Jersey, including support for JSP (written by Paul Sandoz -- the co-spec lead for JSR 311/JAX-RS). Paul also talks about integrating Spring and Jersey and integrating EJB and Jersey Paul also talks about Proxying/Chaining using the JAX-RS server with injected JAX-RS clients. This comment about working with different environments (dev vs. prod for example) struck a chord with me, since the products I develop usually work within a set of systems (webserver, blog-server, search server and etc) per environment. Paul's follow up cryptically talks about some solutions to the problem. I took a look at Paul's original entry, and was able to quickly see how I could extend Paul's @Provider example to support the way I would have wanted the system to behave. Bill de hÓra posted an interesting note about integrating Spring, Jersey and StringTemplate. Fulworx (can work with Spring and with Struts2/WebWorks MVC applications), and Mattress Framworks (Spring + REST = Mattress :) ) provide a different implementation approach to REST.
Wednesday August 27, 2008

REST tutorials and implementations

Filed Under: Java — Solomon @ Aug 27 2008, 10:14:15 PM EDT

I've had quite an interesting couple of weeks trying to wrap my head around REST, and the Java-sphere's various implementations of RESTful infrastructure. I'm going to use this entry to keep a bookmark of Resources I found useful in understanding not only Java's RESTful infrastructure but also the infrastructure of other ecosystems

Intro to Rest on InfoQ - By RESTafarian Stefan Tilkov, with lots of discussion from the InfoQ crowd

Rest Wiki

Ruby on Rails REST - RoR made the development community excited about REST. It implements REST as an extention of a Web framework

Grails REST - IMHO, this deserves another post. Web Framework + REST + WS-* + RSS all in one.

LiftWeb: SCALA REST Services - Thanks to jua for the tip. I can't say that I really fully understand what's going on there, but it seems neat.

MS WCF REST - Here is an entry on using WCF to expose RESTful services, and REST in Silverlight (webcasts). The most astounding thing to me was the amount of XML compared to the Java JAX-RS/JAX-WS approach. It combines both SOAP and REST with the same annotations, with some XML configuration changes.

JAX-RS - JEE's RESTful approach to remote Services. There are some really good ideas and allows for some great implementation strategies of remove "services". I still have some specific reservations about using it for my needs (large, scalable websites); I'll write more about that later. Both JAX-RS and JAX-WS integration with JAXB (Java XML Binding) which converts XML/JSon/YAML to and from Java Objects. Jersey is the reference implementation of JAX-RS, and it's sister project Metro is the JAX-WS reference implementation. JBoss's RESTEasy also deserves an honorable metnion.

CXF - formerly known as XFire, this is a WS-* implementation from the Thoughtworks guys. It supports both JAX-RS and JAX-WS, and uses any of JAXB 2.0, Aegis, XMLBeans (Castor and JiBX in the future) to perform XML/JSon conversions.

Mattress Framework - REST built ontop of Spring @MVC. It has an interesting Why not JSR 311 section.

EhCache Server - It uses JAX-RS under the covers, and also implements JAX-WS (WS-* Web Services). EhCache is a distributed (and/or local) cache mechanism. The EhCache server is a It's really the exemplary use case both architectural uses of JAX-RS, JAX-WS and REST in general. The code is clear, clean and expressive. Here are links to the JAX-RS EhCache Service code and JAX-WS EhCache Service. The origin of EhCache Server REST. EhCache Server's RESTful usage strategy. The EhCache Server definitely deserves a blog entry.

Solr Search Server - Solr is a server-ized version of the Lucene search engine. It uses XML to submit documents to be indexed/stored, and uses a custom-coded mechanism to expose search results in a user request format (XML/JSon/YAML).

One interesting note is that, AFAIK, the Solr URL strategy can't be easily implemented in JAX-RS. Solr uses a Request parameter to determine the result format (for example ...&format=json&...). I don't think JAX-RS easily allows for that strategy of client/server negotiation. Solr definitely deserves another blog entry.

REST over Spring MVC - There have been a few blogs about it with tidbits from various SpringSource talks, but not too much writen directly by the Spring guys. Juergen Hoeller says that Spring REST should be out in September.

I'm going to put some more thoughts about what I'd like to get out of a RESTful environment. I've been building large-scale content websites for the last three years, and I have quite a web development toolkit. Let's see which way my toolkit expands.

Monday August 25, 2008

Which development model does REST the "right way?"

Filed Under: Java — Solomon @ Aug 25 2008, 04:35:40 PM EDT

What's your opinion on who really "gets" REST from a development perspective (in the Java world)? Grails REST? JSR 311 - Jax-RS? Restlets?

JBosser Bill Burke (who's working on JSR 311) at one point said that:

Whoever can pick out the valuable pieces of Struts-like apps and combine it with the AJAX+REST approach will truly have something compelling.

I'm interested in REST from a perspective of building AJAXy Websites, and JSR 311 (Java API for RESTful Web Services) seems like the wrong paradigm for that type of development. Does anyone out there have any pointers for me in terms of a development platform that easily allows RESTful development?

Tuesday August 19, 2008

Spring 3.0 and REST

Filed Under: Java — Solomon @ Aug 19 2008, 04:13:52 PM EDT

I've heard a bit here and a bit there. I'm pretty interested in the RESTful changes that the Spring @MVC framework and even the cessation of Support for Java 1.4.

Unofficial Spring 3.0 announcement

I don't think that there has been any official communication on the subject of Spring 3.0. However, there was a pretty interesting Juergen Hoeller made comment on an InfoQ Article: Spring 2.5: New Features in Spring MVC. The comments on that thread heavily discusses the future direction of REST in @MVC:

Our core strategy is to bring REST to the Spring @MVC world in as natural a fashion as possible. This involves URI template support for Spring's @RequestMapping facility, a dedicated parameter-level @PathParam annotation for parameter extraction based on such a template, etc. This is supposed to be a natural next step after Spring MVC 2.5, preserving all common Spring MVC idioms; you could see it as "completing @MVC".

We'll also provide a RestTemplate class for client-side HTTP access to REST-style services. Currently, the first Spring Framework 3.0 milestone - including a first cut of the dedicated REST support - is scheduled for mid September. We are aiming for GA fairly soon, with 3.0 RC1 being scheduled for December already.

With respect to JAX-RS: The JSR 311 spec essentially defines a dedicated REST resource endpoint model there, not being a great fit for REST-enabling an existing MVC endpoint model. This is why JAX-RS is not the primary point of our REST strategy.

We're considering integration with JAX-RS on a separate basis - separate from Spring MVC's own endpoint model - possibly supporting the use of Jersey (the JAX-RS RI) with Spring-style beans in a Spring web application context. This might make Spring 3.0 as well, depending on the finalization of JSR 311 and Jersey in time for Spring 3.0 RC1. Otherwise it would be a candidate for Spring 3.1.

Juergen

I used the @MVC framework on my last project, which did have a lot asynchronous client-side work, could have benefited from this type of "@MVC completion." I'm pretty excited about this. I'm hoping to give a talk on this subject in October. I'm happy that the Spring Source guys have been so accommodating to my schedule by releasing the 3.0 GA in September :).

Unofficial Spring 3.0 REST Support

I'm not 100% sure of what the @MVC changes are going to be, but the one thing that keeps on coming up is the support for automated URI to parameter translation. For example:

@Controller 
public ContentController{
  @RequestMapping("/content/${id}/show")
  public String show(
    @UriParam("id") Number id) {
    ...
  )
}

@MVC know that a RESTful url like

/content/19350/show
should be handled by the ContentController.show() method with the appropriate portion of the URL gets translated and passed into the show() method.

this Jira defect titled "Comprehensive REST Support" gives a good overview of the potential enhancements:

...
* resource mapping: url templates etc.
* content negotiation: multiple representations of a resource including handling 415 "Unsupported Media Type"
* error conditions: handling responses correctly by sending different status codes e.g. 400 "Bad Request", 201 "Created", etc.
* etag support: similar to the getLastModified() interface
* http method support: templates for GET, PUT, DELETE, POST, HEAD, and OPTIONS (WedDav support too?) as well as handling 405 "Method Not Allowed".
...

Spring @MVC and JAX-RS/JSR 311

I learned a lot about JSR 311 from JBosser/Red Hatter Bill Burke's blogs on the subject and from comments on one of my blog entries that first covered @MVC. There definitely are a lot of similarities between the two approaches to REST through annotations (Hence the "@REST" title). I'm going to write up with a comparison in the near future. I think I'll add in Restlets and possibly even Qi4j's REST implementation

Wednesday July 02, 2008

@ViewPreparers - Spring 2.5 and Tiles 2

Filed Under: Java — Solomon @ Jul 02 2008, 12:31:56 PM EDT

I've been working quite a bit with Tiles 2 and Spring 2.5 lately for a pretty big website. The Tiles framework allows you to break up a page into smaller components (a.k.a a tile) each of which can have their own view (in our case JSPs) and their own "controller" called a ViewPreparer. The entry point from the web, in our case, is a Spring 2.5 annotation driven controller (@Controller for short). We've set up those @Controllers' to use "layout" tiles as views. Those "layout" tiles in turn use other tiles for componentized widgetry. You can read about some of the details of integrating @Controllers with Tiles (and the benefits therein) in Craig Walls' great blog entry on the subject.

We've set up some widgitized components to use ViewPreparers, so we decided to use component scanning magic to auto-magically import beans into our application context... At first, we made these ViewPreparers @Service objects, which created them in our "root" applicationContext. I like to keep DispatcherServlet related instances in the DispatcherServlet applicationContext, so I used the configurable context scanning to include the ViewPreparers only in the appropriate context:

 <context:component-scan base-package="com.mycomany">
   <context:include-filter type="regex" expression=".*ViewPreparer"/>
   <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Controller"/>

   <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Component"/>
   <context:exclude-filter type="annotation" 
     expression="org.springframework.stereotype.Service"/>
 </context:component-scan>

While that regex is pretty useful, it forced me to keep my class names to have a prefix of ViewPreparer. My next attempt to make it a bit nicer was to create my own annotation for defining ViewPreparers:

@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ViewPreparer
{
    /**
     * The value may indicate a suggestion for a logical component name, to be
     * turned into a Spring bean in case of an autodetected component.
     *
     * @return the suggested component name, if any
     */
    String value() default "";
}

Which I apply to my ViewPrepers:

@ViewPreparer
public class WidgetViewPreparer extends ViewPreparerSupport {

    private WidgetService widgetService;

    @Autowired
    public WidgetViewPreparer(WidgetService widgetService) {
        this.widgetService = widgetService;
    }

    @Override
    public void execute(TilesRequestContext tilesContext, 
       AttributeContext attributeContext) throws PreparerException {
         // perform fancy widgetry logic
    }
}

My auto-scanning now looks like

 <context:component-scan base-package="com.mycomany">
   <context:include-filter type="annotation" 
     expression="com.mycompany.stereotype.ViewPreparer"/>
   <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Controller"/>

   <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Component"/>
   <context:exclude-filter type="annotation" 
     expression="org.springframework.stereotype.Service"/>
 </context:component-scan>

My custom annotation definitely felt like it was a better approach than the regex... To take it a step further, wouldn't it be nice to have @ViewPreparers that had annotation symantics similar to Spring @Controllers?

Wednesday July 02, 2008

The Dangers of the bleeding edge

Filed Under: Java — Solomon @ Jul 02 2008, 12:04:20 PM EDT

I'm on a project that's using the latest and greatest of a specific technology, specifically Spring. As with any bleeding edge technology, most of the time things work out great, but things can go awry when you start getting into edge cases. In our case, we ran into a case where we had a slick new @Service object that had a @Transactional method... The combination of the processing of the two annotations simply didn't work. Everything works just fine if the @Service annotation is removed and the bean is set in XML. One of our developers even put in a Forum post about this (which I guess is still being researched by the spring guys).

Overall, new technologies provide a whole bunch of shinny new toys to play with... Just be careful not to cut yourself.

Wednesday April 16, 2008

Search for jobs by Salary

Filed Under: Java — Solomon @ Apr 16 2008, 11:26:09 AM EDT

Indeed.com added another cool job search tool: Search by Salary. You can just add in the dollar amount in the search term, and Indeed gives you the jobs that match. Here's a search for $120K jobs in NY.

Enjoy.

Tuesday April 15, 2008

Spring as an ETL tool

Filed Under: Java — Solomon @ Apr 15 2008, 08:08:50 AM EDT

ETL has three steps:

Extract - retrieve data from an existing source Transform - change the data around a bit so that you can use it in the "destination" environment Load - put the transformed data into the destination repository

I need to take a set of XML documents and save them to a database. I thought about using Groovy for my current requirements. I decided against it because of the complex requirements (multiple types of files/tables and complex transformations) and the team that will be maintaining this code (Java/Spring/Hibernate hard core developers).

I had access to complex Hibernate mappings, so the Load technology is a pretty straight forward choice. I had lots of success with Dom4J as XML Extract technology, and others seemed to be comfortable with that selection as well.

The only thing that's left is the Transform technology. I'm pretty comfortable with Spring core and Spring MVC. They both need to take a String representation of a potentially nested variable name and figure out how to convert that String to a setter. They both need to take a String representation of a value and transform it to the correct type. Spring uses the DataBinder class to do this.

The core of what I'm doing is:

  Document document = new XPPPacketReader().read(file);

  // this is more-or-less a Map
  MutablePropertyValues values = new MutablePropertyValues();

  // read in via XPath into the Map
  for (Map.Entry configuration : this.configurations) {
    String variableName = configuration.getKey();
    String xpath = configuration.getValue();
    values.addPropertyValue(variableName, document.valueOf(xpath));
  }

  // look at the document to figure out what kind of object we need
  // usually, it will involve some logic centered around 
  // document.getRootElement().getName()
  Object content = getObjectCreator.createObject(document); 

  // bind the data - 
     // convert String variable names to setters,
     // convert String values to the appropriate types.
  new DataBinder(content).bind(values);

BTW, I'm using XPP Packet Reader for Dom4J. It solves some character encoding issues that we've run across.

The code basically does the following:

reads in an XML file into a Dom4J Document iterates over a Spring configured map that has variableName to XPath mappings
get the value in the document expressed by the xpath add the value to a Map keyed by the name of the setter/variable
create a top level object that is mapped to the document bind the values extracted in step 2 to the document created in step 3

This approach handled the the most basic cases where there is a one-to-one mapping from xpath to nested variable that is a primitive or String value. Most of the transformations will likely be satisfied with this approach. The other, more complex situations have to be handled via application code and some customizations to DataBinder. DataBinder allows you to set custom PropertyEditors for complex conversion and dynamic collection management. I still have to see if this will work for my needs, but so far, so good.

Tuesday March 11, 2008

Java Calendar DST bug

Filed Under: Java — Solomon @ Mar 11 2008, 01:15:18 PM EDT

Try this code:

Calendar midnight = new GregorianCalendar(2008, 10, 2, 0, 0, 0);
Calendar c = new GregorianCalendar(2008, 10, 2, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
for (int i = 0; i < 25; ++i) {
  System.err.println(c.getTime() + ": " + c.get(Calendar.HOUR_OF_DAY));
  System.err.println("Hours since Midnight: "
      + (c.getTime().getTime() - midnight.getTime().getTime())
      / 1000.0 / 3600.0);

  c.set(Calendar.MINUTE, 0);
  System.err.println(c.getTime() + ": " + c.get(Calendar.HOUR_OF_DAY));
  System.err.println("Hours since Midnight: "
      + (c.getTime().getTime() - midnight.getTime().getTime())
      / 1000.0 / 3600.0);
  System.err.println();

  c.add(Calendar.HOUR_OF_DAY, 1);
}

November 2, 2008 is a cut off date for DST. c.set(Calendar.MINUTE, 0) is supposed to be a no-op, but it isn't... Run the code, and see the bug.

Credit goes to Panos Papamichael for finding this bug


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser