I was trying to download IBM Development Package for Eclipse. I was using Chrome all these days since its launch, apart from Firefox, IE. I always had a good impression about the newbie Chrome so far. But, its inability to open a secure web site shattered my first impression with Chrome. But, this is still acceptable considering it is still 0.2 version 🙂
Chrome

How does Safari3 react to this secured site? Not so different. Safari has been in this market for a while. No excuse for Safari. But, I would still think this would be a basic requirement of a browser to support secured web sites. I never use Safari until I see a bizarre behavior with other browsers. 🙁
Safari

The users can easily fall prey to these bizarre messages. Firefox3 and IE7 were consistent in their behavior as they were proven stable for years.

Firefox Internet Explorer

Think before you decide to leave these trusted sites when accessing through these browsers.

Possibly Related Posts:


I was reading Paul Graham’s essay “A Fundraising Survival Guide” in which, he outlines some of the key factors which helps in keeping fund raising dreams for startups beyond stealth mode. I thought of highlighting these techniques in this post simply because they were very concise and demanding.

1. Have low expectations.
2. Keep working on your startup.
3. Be conservative.
4. Be flexible.
5. Be independent.
6. Don’t take rejection personally.
7. Be able to downshift into consulting.
8. Avoid inexperienced investors.
9. Know where you stand.

Sometimes it is easy to burn the cash and not beat the market expectations. So by all means, successful funding can not be considered a successful startup. Investors are really smart. Don’t you agree? 🙂

Possibly Related Posts:


Yahoo Search provides a RESTful interface for web search. Of course this service is been there for a while. I was just curious to use Jersey Client to play around with the Yahoo REST service. It opens up a world of possibilities for building applications around these services in Java.

Let us invoke the web search API using GET and context search API using POST as described in the following URLs.

http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=finances&format=pdf (GET)

http://search.yahooapis.com/WebSearchService/V1/contextSearch (POST)
appid=YahooDemo&query=madonna&context=Italian+sculptors+and+painters+of+the+renaissance+favored+the+Virgin+Mary+for+inspiration

The URL is constructed using the actual query parameters for accessing web resources which takes the URL encoded “argument=value” form.

The below code shows how we can quickly write a Jersey based REST client for invoking these RESTful web services. The APIs are fairly self-explanatory.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import restful.search.jaxb.ResultSet;
import restful.search.jaxb.ResultType;

import javax.ws.rs.core.MediaType;

public class YahooRestfulSearchClient {
    public static void main(String args[]) {
        Client client = Client.create();
        WebResource r = client.resource("http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=finances&format=pdf");
        ResultSet searchResults = r.accept(MediaType.APPLICATION_XML).get(ResultSet.class);
        for (ResultType result : searchResults.getResult()) {
            System.out.println("Title : " + result.getTitle());
            System.out.println("Summary : " + result.getSummary());
            System.out.println("Url : " + result.getUrl());
        }

        Form form = new Form();
        form.add("appid", "YahooDemo");
        form.add("query", "madonna");
        form.add("context", "Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration");

        r = client.resource("http://search.yahooapis.com/WebSearchService/V1/contextSearch");
        searchResults = r.accept(MediaType.APPLICATION_XML).post(ResultSet.class, form);        
        for (ResultType result : searchResults.getResult()) {
            System.out.println("Title : " + result.getTitle());
            System.out.println("Summary : " + result.getSummary());
            System.out.println("Url : " + result.getUrl());
        }        
    }
}

I updated my original approach for invoking the Context search based on the suggestions from Jersey developers to use Jersey form parameters encoded in the body of the request instead of directly constructing the URI as a request entity in the post call. Typically, POST is used to create web resources. But, Yahoo documentation suggests that POST be used for context searches just because the query params can sometimes be potentially lengthy, and hence the recommendation to send query parameters in the POST data block. The other option in Jersey is to use the UriBuilder to construct the URI and add query parameters to the URI. The code snip below shows this latter approach. Using form parameters is more elegant than constructing lengthy URIs 🙂


        URI uri = UriBuilder.fromPath("http://search.yahooapis.com/WebSearchService/V1/contextSearch").queryParam("appid", "YahooDemo").queryParam("query", "madonna").queryParam("context", "Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration").build();
        r = client.resource(uri);
        searchResults = r.accept(MediaType.APPLICATION_XML).post(ResultSet.class);

In both these approaches, the crux lies in the response type ‘ResultSet’ which is described in the below schema for the responses returned from the Yahoo REST service. You can access the schema here. Here is a quick look at the schema.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="urn:yahoo:srch"
  xmlns="urn:yahoo:srch"
  elementFormDefault="qualified">

	<xs:element name="ResultSet">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Result" type="ResultType" minOccurs="0" maxOccurs="100" />
			</xs:sequence>
			<xs:attribute name="totalResultsAvailable" type="xs:integer" />
			<xs:attribute name="totalResultsReturned" type="xs:integer" />

			<xs:attribute name="firstResultPosition" type="xs:integer" />
		</xs:complexType>
	</xs:element>

	<xs:complexType name="ResultType">
		<xs:sequence>
			<xs:element name="Title" type="xs:string" />
			<xs:element name="Summary" type="xs:string" />
			<xs:element name="Url" type="xs:string" />

			<xs:element name="ClickUrl" type="xs:string" />
			<xs:element name="ModificationDate" type="xs:string" minOccurs="0" />
			<xs:element name="MimeType" type="xs:string" minOccurs="0" />
			<xs:element name="Cache" type="CacheType" minOccurs="0" />
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="CacheType">
		<xs:sequence>
			<xs:element name="Url" type="xs:string" />

			<xs:element name="Size" type="xs:string" />
		</xs:sequence>
	</xs:complexType>

</xs:schema>

Use this schema to generate your JAXB types (ResultSet, ResultType, CacheType) and you are ready to consume the response within your REST client. The best part of this exercise was using IntelliJ IDEA 8.0M1. This latest release has support for REST client plugin where you can test your restful resources and it supports RESTful development using Jersey. More to explore on this front. Here is a screen shot of IDEA8 Rest Client in action.
IDEA8 Rest Client

Possibly Related Posts:


It has been a wonderful week with my ramblings with CXF and SSL. But, it all ended up in good mood. Sometimes, it is so hard to find how to use certain features with CXF. I am a great fan of CXF because of its simplicity and intuitive feature set. It provides an array of integration points with best of breed technologies, but sadly most of the crux is buried under its system tests or CXF user mailing list archive. The documentation front is really lacking its meat from the “source”. So, people end up debugging CXF which is the only weapon to invade your configuration issues.

The community around CXF is very healthy and gained substantial momentum in all fronts for the past year or so. CXF user mailing list is such an awful resource. Its the first resort to get your queries answered and if you don’t get a response, don’t wait, just debug yourself. The resources are very limited and their support is really appreciated.

I wanted to contribute a sample for programmatic setup of SSL with CXF using embedded Jetty, so people get it right at the first time. So, I ported the sample “wsdl_first_https” which is bundled with CXF 2.1.1 distribution to a “java_first_https” scenario. The “wsdl_first_https” sample uses Spring based configuration for SSL. Though this scenario seem to fit well where the application uses spring as its underlying DI framework. For people who wish not to care about Spring, why would they want to unnecessarily bundle Spring jars for achieving CXF/SSL support in their application. But, this all depends on what approach you chose to build your services with CXF (either using Spring config or Java APIs). This is just a simple case for building this demo in “no-Spring” way. It opens a whole world of possibilities for dynamically configuring security in your embedded Jetty container.

Lets take the simple Greeter service which greets the client with its name sent as an input parameter.

@WebService
public interface Greeter {

    String greetMe(String me);
}


@javax.jws.WebService(name = "Greeter", serviceName = "SOAPService",
                      targetNamespace = "http://apache.org/hello_world_soap_http")
public class GreeterImpl implements Greeter {
    private static final Logger LOG =
        Logger.getLogger(GreeterImpl.class.getPackage().getName());

    public String greetMe(String me) {
        LOG.info("Executing operation greetMe");
        System.out.println("Executing operation greetMe");
        System.out.println("Message received: " + me + "\n");
        return "Hello " + me;
    }
}

The demo uses the same client and server certificate stores shipped with “wsdl_first_https” demo in CXF 2.1.1. So, don’t forget to include them when running this demo.

The Server side SSL configuration using CXF Java APIs is shown below.

public class Server {

    protected Server() throws Exception {
        System.out.println("Starting Server");
        String address = "https://localhost:9001/SoapContext/SoapPort";
        JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
        sf.setServiceClass(Greeter.class);
        sf.setAddress(address);

        Greeter implementor = new GreeterImpl();
        sf.getServiceFactory().setInvoker(new BeanInvoker(implementor));

        sf = configureSSLOnTheServer(sf, 9001);
        org.apache.cxf.endpoint.Server server = sf.create();
        String endpoint = server.getEndpoint().getEndpointInfo().getAddress();
        
        System.out.println("Server started at " + endpoint);
    }

    private JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf, int port) {
        try {
            TLSServerParameters tlsParams = new TLSServerParameters();
            KeyStore keyStore = KeyStore.getInstance("JKS");
            String password = "password";
            File truststore = new File("certs\\cherry.jks");
            keyStore.load(new FileInputStream(truststore), password.toCharArray());
            KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyFactory.init(keyStore, password.toCharArray());
            KeyManager[] km = keyFactory.getKeyManagers();
            tlsParams.setKeyManagers(km);

            truststore = new File("certs\\truststore.jks");
            keyStore.load(new FileInputStream(truststore), password.toCharArray());
            TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustFactory.init(keyStore);
            TrustManager[] tm = trustFactory.getTrustManagers();
            tlsParams.setTrustManagers(tm);
            FiltersType filter = new FiltersType();
            filter.getInclude().add(".*_EXPORT_.*");
            filter.getInclude().add(".*_EXPORT1024_.*");
            filter.getInclude().add(".*_WITH_DES_.*");
            filter.getInclude().add(".*_WITH_NULL_.*");
            filter.getExclude().add(".*_DH_anon_.*");
            tlsParams.setCipherSuitesFilter(filter);
            ClientAuthentication ca = new ClientAuthentication();
            ca.setRequired(true);
            ca.setWant(true);
            tlsParams.setClientAuthentication(ca);
            JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory();
            factory.setTLSServerParametersForPort(port, tlsParams);
        } catch (KeyStoreException kse) {
            System.out.println("Security configuration failed with the following: " + kse.getCause());
        } catch (NoSuchAlgorithmException nsa) {
            System.out.println("Security configuration failed with the following: " + nsa.getCause());
        } catch (FileNotFoundException fnfe) {
            System.out.println("Security configuration failed with the following: " + fnfe.getCause());
        } catch (UnrecoverableKeyException uke) {
            System.out.println("Security configuration failed with the following: " + uke.getCause());
        } catch (CertificateException ce) {
            System.out.println("Security configuration failed with the following: " + ce.getCause());
        } catch (GeneralSecurityException gse) {
            System.out.println("Security configuration failed with the following: " + gse.getCause());
        } catch (IOException ioe) {
            System.out.println("Security configuration failed with the following: " + ioe.getCause());
        }

        return sf;
    }

    public static void main(String args[]) throws Exception {
        System.out.println("The server's security configuration will be done programatically.");
        System.out.println();
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

The SSL server configuration using Java APIs exactly maps to spring config “CherryServer.cxf” used in the distribution.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  ** This file configures the Cherry Server.
 -->

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:sec="http://cxf.apache.org/configuration/security"
  xmlns:http="http://cxf.apache.org/transports/http/configuration"
  xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration"
  xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
  xsi:schemaLocation="
       http://cxf.apache.org/configuration/security  		      http://cxf.apache.org/schemas/configuration/security.xsd
            http://cxf.apache.org/transports/http/configuration
            http://cxf.apache.org/schemas/configuration/http-conf.xsd
            http://cxf.apache.org/transports/http-jetty/configuration
            http://cxf.apache.org/schemas/configuration/http-jetty.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <http:destination name="{http://apache.org/hello_world_soap_http}GreeterPort.http-destination"> 
  </http:destination>

  <httpj:engine-factory>
   <httpj:engine port="9001">
    <httpj:tlsServerParameters>
      <sec:keyManagers keyPassword="password">
           <sec:keyStore type="JKS" password="password" 
                file="certs/cherry.jks"/>
      </sec:keyManagers>
      <sec:trustManagers>
          <sec:keyStore type="JKS" password="password"
               file="certs/truststore.jks"/>
      </sec:trustManagers>
      <sec:cipherSuitesFilter>
        <!-- these filters ensure that a ciphersuite with
          export-suitable or null encryption is used,
          but exclude anonymous Diffie-Hellman key change as
          this is vulnerable to man-in-the-middle attacks -->
        <sec:include>.*_EXPORT_.*</sec:include>
        <sec:include>.*_EXPORT1024_.*</sec:include>
        <sec:include>.*_WITH_DES_.*</sec:include>
        <sec:include>.*_WITH_NULL_.*</sec:include>
        <sec:exclude>.*_DH_anon_.*</sec:exclude>
      </sec:cipherSuitesFilter>
      <sec:clientAuthentication want="true" required="true"/>
    </httpj:tlsServerParameters>
   </httpj:engine>
  </httpj:engine-factory>
</beans>

The Client side SSL configuration using CXF Java APIs is shown below.

public class Client {

    private static void configureSSLOnTheClient(Object c) {
        org.apache.cxf.endpoint.Client client = ClientProxy.getClient(c);
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();

        try {
            TLSClientParameters tlsParams = new TLSClientParameters();
            tlsParams.setDisableCNCheck(true);

            KeyStore keyStore = KeyStore.getInstance("JKS");
            String trustpass = "password";

            File truststore = new File("certs\\truststore.jks");
            keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
            TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustFactory.init(keyStore);
            TrustManager[] tm = trustFactory.getTrustManagers();
            tlsParams.setTrustManagers(tm);

            truststore = new File("certs\\wibble.jks");
            keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
            KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyFactory.init(keyStore, trustpass.toCharArray());
            KeyManager[] km = keyFactory.getKeyManagers();
            tlsParams.setKeyManagers(km);

            FiltersType filter = new FiltersType();
            filter.getInclude().add(".*_EXPORT_.*");
            filter.getInclude().add(".*_EXPORT1024_.*");
            filter.getInclude().add(".*_WITH_DES_.*");
            filter.getInclude().add(".*_WITH_NULL_.*");
            filter.getExclude().add(".*_DH_anon_.*");
            tlsParams.setCipherSuitesFilter(filter);

            httpConduit.setTlsClientParameters(tlsParams);
        } catch (KeyStoreException kse) {
            System.out.println("Security configuration failed with the following: " + kse.getCause());
        } catch (NoSuchAlgorithmException nsa) {
            System.out.println("Security configuration failed with the following: " + nsa.getCause());
        } catch (FileNotFoundException fnfe) {
            System.out.println("Security configuration failed with the following: " + fnfe.getCause());
        } catch (UnrecoverableKeyException uke) {
            System.out.println("Security configuration failed with the following: " + uke.getCause());
        } catch (CertificateException ce) {
            System.out.println("Security configuration failed with the following: " + ce.getCause());
        } catch (GeneralSecurityException gse) {
            System.out.println("Security configuration failed with the following: " + gse.getCause());
        } catch (IOException ioe) {
            System.out.println("Security configuration failed with the following: " + ioe.getCause());
        }
    }

    public static void main(String args[]) {
        System.out.println("The client's security configuration will be done programatically.");
        System.out.println();
        String address = "https://localhost:9001/SoapContext/SoapPort";
        JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
        proxyFactory.setServiceClass(Greeter.class);
        proxyFactory.setAddress(address);

        Greeter client = (Greeter) proxyFactory.create();
        configureSSLOnTheClient(client);

        System.out.println("Invoking greetMe...");
        try {
            String resp = client.greetMe(System.getProperty("user.name"));
            System.out.println("Server responded with: " + resp);
            System.out.println();

        } catch (Exception e) {
            System.out.println("Invocation failed with the following: " + e.getCause());
            System.out.println();
        }

    }
}

The SSL client configuration using Java APIs exactly maps to spring config “WibbleClient.cxf” used in the distribution.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  ** This file configures the Wibble Client
  -->

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:sec="http://cxf.apache.org/configuration/security"
  xmlns:http="http://cxf.apache.org/transports/http/configuration"
  xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
  xsi:schemaLocation="
           http://cxf.apache.org/configuration/security
           http://cxf.apache.org/schemas/configuration/security.xsd
           http://cxf.apache.org/transports/http/configuration
           http://cxf.apache.org/schemas/configuration/http-conf.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

  <http:conduit name="{http://apache.org/hello_world_soap_http}SoapPort.http-conduit">
    <http:tlsClientParameters disableCNCheck="true">
      <sec:trustManagers>
          <sec:keyStore type="JKS" password="password"
               file="certs/truststore.jks"/>
      </sec:trustManagers>
      <sec:keyManagers keyPassword="password">
           <sec:keyStore type="JKS" password="password" 
                file="certs/wibble.jks"/>
      </sec:keyManagers>
      <sec:cipherSuitesFilter>
        <!-- these filters ensure that a ciphersuite with
          export-suitable or null encryption is used,
          but exclude anonymous Diffie-Hellman key change as
          this is vulnerable to man-in-the-middle attacks -->
        <sec:include>.*_EXPORT_.*</sec:include>
        <sec:include>.*_EXPORT1024_.*</sec:include>
        <sec:include>.*_WITH_DES_.*</sec:include>
        <sec:include>.*_WITH_NULL_.*</sec:include>
        <sec:exclude>.*_DH_anon_.*</sec:exclude>
      </sec:cipherSuitesFilter>
    </http:tlsClientParameters>
   </http:conduit>
</beans> 

You can download the NetBeans project here.

Possibly Related Posts:


For those who missed JavaOne this year, Sun has published the conference slides here a month later. I find these very useful especially for Java geeks as it provides an opportunity to learn some of the technical advancements around the Java platform and the momentum around the technology is healthy.

My top contenders this year were:
Building SOA Applications with JAX-WS, JAXRS, JAXB, and Ajax by Mark Hansen
Let’s Resync Concurrency Features in JDK™ 7 by Brian Goetz
Defective Java™ Code: Turning WTF code into a learning experience by William Pugh
More Effective Java by Joshua Bloch
Design Patterns Reconsidered by Alex Miller
JAX-RS: The Java™ API for RESTful Web Services by Marc Hadley and Paul Sandoz
Top 10 Patterns for Scaling Out Java™ Applications by Cameron Purdy
Groovy and Grails: Changing the Landscape of Java™ Platform, Enterprise Edition (Java EE Platform) Patterns by Graeme Rocher
Choosing a Java Web Framework: A Comparison by Richard Pack
Java™ Servlet 3.0 API: What’s new and exciting by Rajiv Mordani

Possibly Related Posts:


In my earlier blog entry on Jersey, I used HTTPClient API and curl command line utility as the clients. I had not mentioned about the Jersey Client API. It is part of the Jersey distribution. I would prefer using Jersey Client API as it is modeled around the concepts of JAX-RS spec. Let us quickly re-write our client using the Jersey API and see how easy is to write a client in Java.


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import restful.impl.jaxb.MovieCollection;
import restful.impl.jaxb.MovieDetails;

import static java.lang.System.out;

public class JerseyClient {
    public static void main(String args[]) {
        Client client = Client.create();
        WebResource r = client.resource("http://localhost:9090/boxoffice/movies");
        MovieCollection movies = r.accept("application/xml").get(MovieCollection.class);
        for (MovieDetails movie : movies.getMovie()) {
            out.println("Title : " + movie.getTitle());
            out.println("Genres : " + movie.getGenres());
            out.println("Directed By : " + movie.getDirectedBy());
            out.println("Rank : " + movie.getRank());
        }        
    }
}

This program will print all the top box office movie details in the console. I will be discussing more about this client API in future posts.

The Jersey user forum is the first place to check for any Jersey related issues. The Jersey team is amazingly helpful in resolving any issues. You can also visit Paul’s blog here, Marc’s blog here and Jakub’s blog here. You can bookmark these blogs as they are really informative. Grab the Jersey team’s JavaOne 2008 presentation slide deck here.

You can download the NetBeans project for this sample movie application here.

Possibly Related Posts:


Jersey Jersey is a JSR 311 reference implementation for the JAX-RS spec (The Java API for RESTful Web Services). The JSR development is nearing its completion, but for most part the RI is stable enough for developers to start playing with RESTful services. JAX-RS is an elegant API built around the powerful REST architecture and modeled using resource providers. These resources can be described using WADL (Web Application Description Language). Let us build a basic movie resource which lists top box office movies. Just download the latest distribution of Jersey. I am using 0.8 release for this illustration. NetBeans 6.1 does provide support a Jersey plugin for 0.7 release. Add the jars from the distribution to the project classpath. JAX-RS requires JDK 1.5 as it uses annotations for implementing web resources.

The below XSD defines a basic movie schema.


<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              elementFormDefault="unqualified"> 
<xsd:element name="movieCollection">     
   <xsd:complexType>
      <xsd:sequence>
         <xsd:element name="movie" type="movieDetails" minOccurs="1" 
                          maxOccurs="unbounded"/>         
      </xsd:sequence>
   </xsd:complexType>
</xsd:element>
   <xsd:complexType name="movieDetails">
      <xsd:sequence>
         <xsd:element name="title" type="xsd:string"/>
         <xsd:element name="genres" type="xsd:string"/>
         <xsd:element name="directedBy" type="xsd:string"/>
      </xsd:sequence>
      <xsd:attribute name="rank" type="xsd:int"/>
   </xsd:complexType>
</xsd:schema>

Let us use JAXB to generate the Java classes for this schema using the “xjc” tool. This will generate MovieCollection and MovieDetails types in addition to the ObjectFactory. These types will be used in building the response object for our movie collection.

We will implement the “TopBoxOffice” web resource using the JAX-RS APIs.


import com.sun.jersey.spi.resource.Singleton;
import restful.impl.jaxb.MovieCollection;
import restful.impl.jaxb.MovieDetails;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.Response;
import static java.lang.System.out;

@Singleton
@Path("/boxoffice")
public class TopBoxOffice {
    private MovieCollection collection = new MovieCollection();
    private final String HTTP_RESPONSE_DATE_HEADER = "EEE, dd MMM yyyy HH:mm:ss zzz";
    private final String DATE_ONE = (new SimpleDateFormat(HTTP_RESPONSE_DATE_HEADER, Locale.US)).format(new Date(1));
    private static int rank = 0;
    
    public TopBoxOffice() {        
        init();
    }    
    
    @GET    
    @Path("/movies")
    @ProduceMime({"application/xml", "application/json"})
    public Response getTopBoxOfficeMoviesThisWeek() {                                
        out.println("Invoking getTopBoxOfficeMoviesThisWeek() ...");
        //Pragma and Expires headers are set to disable caching in IE
        return Response.ok(collection).header("Pragma", "no-cache").header("Expires", DATE_ONE).build();
    }
    
    final void init() {        
        MovieDetails movie = constructMovieObject("Indiana Jones and the Kingdom of the Crystal Skull", "Steven Spielberg", "Action/Adventure and Sequel");
        collection.getMovie().add(movie);
        movie = constructMovieObject("The Chronicles of Narnia: Prince Caspian", "Andrew Adamson", "Action/Adventure, Science Fiction/Fantasy and Adaptation");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Iron Man", "Jon Favreau", "Action/Adventure, Science Fiction/Fantasy and Adaptation");
        collection.getMovie().add(movie);
        movie = constructMovieObject("What Happens in Vegas", "Tom Vaughan", "Comedy");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Speed Racer", "Larry Wachowski, Andy Wachowski", "Action/Adventure, Science Fiction/Fantasy and Adaptation");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Baby Mama", "Michael McCullers", "Comedy");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Made of Honor", "Paul Weiland", "Comedy");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Forgetting Sarah Marshall", "Nicholas Stoller", "Comedy");
        collection.getMovie().add(movie);
        movie = constructMovieObject("Harold and Kumar Escape From Guantanamo Bay", "Jon Hurwitz, Hayden Schlossberg", "Comedy and Sequel");
        collection.getMovie().add(movie);
        movie = constructMovieObject("The Visitor", "Tom McCarthy", "Comedy and Drama");
        collection.getMovie().add(movie);       
    }
    
    final MovieDetails constructMovieObject(String title, String direction, String genres) {        
        MovieDetails movie = new MovieDetails();
        movie.setTitle(title);
        movie.setRank(++rank);
        movie.setDirectedBy(direction);
        movie.setGenres(genres);
        return movie;
    }
}

The Singleton annotation ensures that only one instance of this class will be instantiated per web application. The movie collection resource will be populated when the first request to the movie list resource occurs. Remember this is RI specific feature and not part of JSR 311 API. The Path annotation defines the URI path to access the web resource. The resource is annotated using GET which populates top box office movies in the response. The response will be rendered using either XML or JSON. The default media type will be XML as it is the first one in the list provided to the ProduceMime annotation. If you need JSON response, you need to set the “Accept” header to “application/json” type. This will render JSON response. If you notice the response code, we are adding couple headers which basically disables caching in IE. You may set as many HTTP response headers on the response using the javax.ws.rs.core.Response class. This resource only implements the basic HTTP GET operation. Developing PUT, POST and DELETE operations on the resource can be quite easy too. The resource can be backed by a database where these HTTP operations can be easily translated into CRUD actions operating on the data.

The resource can now be deployed in a HTTP server. Let us use the Grizzly HTTP server to quickly deploy our resource and do some testing. Grizzly jars are bundled as part of the Jersey distribution. Add the required jars to the classpath. The below code starts the HTTP server at port 9090.


import com.sun.grizzly.http.SelectorThread;
import com.sun.jersey.api.container.grizzly.GrizzlyServerFactory;
import static java.lang.System.*;

public class Server {
    public static void main(String[] args) throws Exception {        
        SelectorThread server = GrizzlyServerFactory.create("http://localhost:9090/");
        out.println("Server running, hit return to stop...");
        in.read();
        out.println("Stopping server");
        server.stopEndpoint();
        exit(0);
        out.println("Server stopped");
    }    
}

If you want to use this within a servlet container, you can use the standard web.xml to describe a Jersey webapp.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Now you can access the URL “http://localhost:9090/boxoffice/movies” from a browser.
You should see the XML response as shown below.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<movieCollection>
	<movie rank="1">
		<title>Indiana Jones and the Kingdom of the Crystal Skull</title>
		<genres>Action/Adventure and Sequel</genres>
		<directedBy>Steven Spielberg</directedBy>
	</movie>
	<movie rank="2">
		<title>The Chronicles of Narnia: Prince Caspian</title>
		<genres>Action/Adventure, Science Fiction/Fantasy and Adaptation</genres>
		<directedBy>Andrew Adamson</directedBy>
	</movie>
	<movie rank="3">
		<title>Iron Man</title>
		<genres>Action/Adventure, Science Fiction/Fantasy and Adaptation</genres>
		<directedBy>Jon Favreau</directedBy>
	</movie>
	<movie rank="4">
		<title>What Happens in Vegas</title>
		<genres>Comedy</genres>
		<directedBy>Tom Vaughan</directedBy>
	</movie>
	<movie rank="5">
		<title>Speed Racer</title>
		<genres>Action/Adventure, Science Fiction/Fantasy and Adaptation</genres>
		<directedBy>Larry Wachowski, Andy Wachowski</directedBy>
	</movie>
	<movie rank="6">
		<title>Baby Mama</title>
		<genres>Comedy</genres>
		<directedBy>Michael McCullers</directedBy>
	</movie>
	<movie rank="7">
		<title>Made of Honor</title>
		<genres>Comedy</genres>
		<directedBy>Paul Weiland</directedBy>
	</movie>
	<movie rank="8">
		<title>Forgetting Sarah Marshall</title>
		<genres>Comedy</genres>
		<directedBy>Nicholas Stoller</directedBy>
	</movie>
	<movie rank="9">
		<title>Harold and Kumar Escape From Guantanamo Bay</title>
		<genres>Comedy and Sequel</genres>
		<directedBy>Jon Hurwitz, Hayden Schlossberg</directedBy>
	</movie>
	<movie rank="10">
		<title>The Visitor</title>
		<genres>Comedy and Drama</genres>
		<directedBy>Tom McCarthy</directedBy>
	</movie>
</movieCollection>

In order to test the JSON rendering, let us write a Client developed using HTTP Client library. You need to have commons-logging and commons-codec in addition to the commons-httpclient jars in the classpath.


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.Header;
import static java.lang.System.out;

public class Client {

  public static void main(String args[]) throws Exception {
    String url = "http://localhost:9090/boxoffice/movies";
    out.println("Sent HTTP GET request to the endpoint " + url);
    GetMethod get = new GetMethod(url);
    get.addRequestHeader("Accept", "application/json");
    HttpClient httpclient = new HttpClient();

    StringBuffer buffer = null;
    try {
      httpclient.executeMethod(get);

      if (get.getStatusCode() == HttpStatus.SC_OK) {
        InputStream is = get.getResponseBodyAsStream();
        Header[] headers = get.getResponseHeaders();
        for (Header header : headers) {
            out.println(header.getName() + " : " + header.getValue());
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        buffer = new StringBuffer();
        String line = null;
        while ((line = in.readLine()) != null) {
          buffer.append(line);
        }
      } else {
        out.println("GET action raised an error: " + get.getStatusLine());
      }

    } finally {
      // Release current connection to the connection pool once you are done
      get.releaseConnection();
    }

    out.println("JSON RESPONSE : " + buffer.toString());
  }
}

The output of this program is shown below.


Sent HTTP GET request to the endpoint http://localhost:9090/boxoffice/movies
Pragma : no-cache
Expires : Wed, 31 Dec 1969 17:00:00 MST
Content-Type : application/json
Transfer-Encoding : chunked
Date : Tue, 27 May 2008 04:13:28 GMT
JSON RESPONSE : 
{"movieCollection":
{"movie":[
{"@rank":"1","title":"Indiana Jones and the Kingdom of the Crystal Skull","genres":"Action/Adventure and Sequel","directedBy":"Steven Spielberg"},
{"@rank":"2","title":"The Chronicles of Narnia: Prince Caspian","genres":"Action/Adventure, Science Fiction/Fantasy and Adaptation","directedBy":"Andrew Adamson"},
{"@rank":"3","title":"Iron Man","genres":"Action/Adventure, Science Fiction/Fantasy and Adaptation","directedBy":"Jon Favreau"},
{"@rank":"4","title":"What Happens in Vegas","genres":"Comedy","directedBy":"Tom Vaughan"},
{"@rank":"5","title":"Speed Racer","genres":"Action/Adventure, Science Fiction/Fantasy and Adaptation","directedBy":"Larry Wachowski, Andy Wachowski"},
{"@rank":"6","title":"Baby Mama","genres":"Comedy","directedBy":"Michael McCullers"},
{"@rank":"7","title":"Made of Honor","genres":"Comedy","directedBy":"Paul Weiland"},
{"@rank":"8","title":"Forgetting Sarah Marshall","genres":"Comedy","directedBy":"Nicholas Stoller"},
{"@rank":"9","title":"Harold and Kumar Escape From Guantanamo Bay","genres":"Comedy and Sequel","directedBy":"Jon Hurwitz, Hayden Schlossberg"},
{"@rank":"10","title":"The Visitor","genres":"Comedy and Drama","directedBy":"Tom McCarthy"}
]}
}

You can also use curl to test the above behavior. This approach is easier than the earlier one, if you have access to curl.


curl -i -H "Accept: application/json" http://localhost:9090/boxoffice/movies

WADL can be accessed at http://localhost:9090/application.wadl. This provides basic resource description as shown below.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://research.sun.com/wadl/2006/10">
	<resources base="http://localhost:9090/">
		<resource path="/boxoffice">
			<resource path="/movies">
				<method name="GET">
					<response>
						<representation mediaType="application/xml"/>
						<representation mediaType="application/json"/>
					</response>
				</method>
			</resource>
		</resource>
	</resources>
</application>

RESTful web services has more potential use cases and Jersey provides extensions to the library so that developers can customize as per their needs.
The resource provider SPI allows pluggable custom resource providers. JAX-RS also supports pluggable type system for encoding and decoding of a Java type to/from an entity of an HTTP response/request. Some of these advanced concepts deserve more space and possibly another post.

Possibly Related Posts:


Message brokering markets were once dominated by heavy weights and required huge investments by enterprises for implementing such solutions. Vendors made huge bucks out of it by selling such solutions and support. Is this still considered a niche market? I personally don’t think so. I am definitely not against vendors offering such solutions, but the point I am making here is whether this market still deserves huge investments when mature open source alternatives are available.

You possibly have two options. Buy an OTS solution and forget about maintenance nightmare, typically argued by the so called “Enterprise Architects” because they assume it’s a safe bet. Such decisions make your enterprise tightly coupled with these kinds of solutions. You may never know sometimes you end up paying huge bucks for feature requests and lobbying for license costs and finally stuck with the custom solution for years. This choice comes at a huge price, but at some convenience as one need not blame you for lack of a feature or offering a buggy solution. Does this convenience worth anymore?

Historically, enterprises chose such solutions mainly because of the support and maintenance that comes bundled with the solution and they always awarded such contracts to market leaders. Finally, when the vendor decides to release newer versions of the product, customers had to upgrade and sometimes forcefully migrated to a newer version, just because earlier version does not scale or does not support a feature or possibly discontinued.

On the contrary, some enterprises (mostly small to mid-size) consider the option of implementing such messaging solution using open source alternatives and people who make this choice need to be really smart because of the liability that comes with their decision. In most situations, such decisions are made by experts who are aware of the complexity involved in such undertaking. For most part it will be straightforward to adopt such a solution and sometimes it could be painful when there is lack of technical expertise within the enterprise. But, it will definitely be a rewarding experience when working with smart people in such integrations. The open source community offers their best support in resolving any issues. This option will surely benefit enterprises in terms of licensing and support costs. But, the key assumption here is that you bet on your expertise on such engagements.

ActiveMQ 5.1Open source solutions are increasingly becoming a compelling choice for enterprises because of their mature feature set and wide adoption backed by strong user community. ActiveMQ is one such project in ASF where you see tons of features available to its users. I had experience working on similar commercial products, but ActiveMQ is absolutely mind blowing. Simplicity wins the heart of enterprise developers. Some commercial offerings take days to install and configure which requires special hardware (high-end servers) and sometimes on-site training from vendor. With projects like ActiveMQ, it is becoming more and more productive for teams to develop, test and roll out integration solutions in a shorter period of time. It just takes minutes to install and configure ActiveMQ in your desktop.

I recently downloaded ActiveMQ and played around with it. Let us analyze their startup messages and dissect some of them and see what they offer. This is just a tip of an iceberg.


 (1) D:\\apache-activemq-5.1.0\\bin>activemq.bat
 (2) ACTIVEMQ_HOME: D:\\apache-activemq-5.1.0\\bin\\..
 (3) ACTIVEMQ_BASE: D:\\apache-activemq-5.1.0\\bin\\..
 (4) Loading message broker from: xbean:activemq.xml
 (5) INFO  BrokerService                  - Using Persistence Adapter: AMQPersistenceAdapter(D:\\apache-activemq-5.1.0\\bin\\..\\data)
 (6) INFO  BrokerService                  - ActiveMQ 5.1.0 JMS Message Broker (localhost) is starting
 (7) INFO  BrokerService                  - For help or more information please see:http://activemq.apache.org/
 (8) INFO  AMQPersistenceAdapter          - AMQStore starting using directory: D:\\apache-activemq-5.1.0\\bin\\..\\data
 (9) INFO  KahaStore                      - Kaha Store using data directory D:\\apache-activemq-5.1.0\\bin\\..\\data\\kr-store\\state
(10) INFO  AMQPersistenceAdapter          - Active data files: []
(11) INFO  KahaStore                      - Kaha Store using data directory D:\\apache-activemq-5.1.0\\bin\\..\\data\\kr-store\\data
(12) INFO  TransportServerThreadSupport   - Listening for connections at: tcp://nandi:61616
(13) INFO  TransportConnector             - Connector openwire Started
(14) INFO  TransportServerThreadSupport   - Listening for connections at: ssl://nandi:61617
(15) INFO  TransportConnector             - Connector ssl Started
(16) INFO  TransportServerThreadSupport   - Listening for connections at: stomp://nandi:61613
(17) INFO  TransportConnector             - Connector stomp Started
(18) INFO  TransportServerThreadSupport   - Listening for connections at: xmpp://nandi:61222
(19) INFO  TransportConnector             - Connector xmpp Started
(20) INFO  NetworkConnector               - Network Connector default-nc Started
(21) INFO  BrokerService                  - ActiveMQ JMS Message Broker (localhost, ID:nandi-64041-1211065443740-0:0) started
(22) INFO  log                            - Logging to org.slf4j.impl.JCLLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
(23) INFO  log                            - jetty-6.1.9
(24) INFO  WebConsoleStarter              - ActiveMQ WebConsole initialized.
(25) INFO  /admin                         - Initializing Spring FrameworkServlet 'dispatcher'
(26) INFO  log                            - ActiveMQ Console at http://0.0.0.0:8161/admin
(27) INFO  log                            - ActiveMQ Web Demos at http://0.0.0.0:8161/demo
(28) INFO  log                            - RESTful file access application at http://0.0.0.0:8161/fileserver
(29) INFO  log                            - Started SelectChannelConnector@0.0.0.0:8161
(30) INFO  FailoverTransport              - Successfully connected to tcp://localhost:61616

The following features are enabled by default when an ActiveMQ broker is started.

Line 8 – AMQStore is the default storage for AcitveMQ 5 and above.
Line 11 – KahaStore is an optimal performance storage solution used for message persistance.
Line 12 – OpenWire is a cross language Wire Protocol which allows native access to ActiveMQ from a number of different languages and platforms.
Line 14 – SSL transport allows clients to connect to a remote ActiveMQ broker using SSL over a TCP socket.
Line 16 – Stomp is used by non-Java clients talk to ActiveMQ server and other message brokers.
Line 18 – XMPP is used to connect to the broker & send and receive messages (Jabber).
Line 26 – ActiveMQ Web Console for managing the broker services such as queues, topics and subscriptions.
Line 28 – REST-ful API to messaging (JMS). Browsing of queues implemented using pluggable views such as ATOM and RSS feeds.
Line 30 – Failover transport used for reconnection.

There are tons of other features which is beyond the scope of this discussion. Some of the notable features include embedded message broker which comes handy in unit testing. You can refer to its complete feature set here. There are sub-projects within ActiveMQ such as NMS (for .NET) and CMS (for C++) which provides unified access to ActiveMQ from other programming language environments. Camel is another sub-project which implements enterprise integration patterns. Its a topic of interest for another blog entry.

This is more than compelling to adopt this solution. Enterprises sometimes face real complexity in terms of cost of ownership issues when trying to adopt such open source solutions. But, this could be overcome by choosing support offerings from companies like IONA, Covalent (now part of SpringSource) and OpenLogic. This may still work out to be cost effective when compared to OTS solutions. The market should favor such healthy adoption.

Possibly Related Posts:


Effective Java second edition finally started shipping this week. I dreamed of this day in one of my earlier blog post 🙂 As predicted, it finally made it this year during JavaOne. Even this year, Joshua had a session on “More ‘Effective Java'” – third in a row since 2006.

The book was published on May 8, 2008 and I pre-ordered at Amazon thinking the book will hit the stores only on May 28 as per their website. But, I then decided to order it at informIT as it was available immediately (not to mention the best deal I could get on the Internet). I should be getting it next week. You can read sample chapters from here and here. I had gone through the Generics chapter and its pretty impressive.

As always, its definitely a programmer’s asset. This second edition is reloaded with 21 more best programming practice primarily from JDK 1.5. You can read Joshua Bloch’s recent interviews at InfoQ and java.sun.com.

You can even buy an autographed version from craigslist. It comes at a premium of $50. Josh is always a rock star in the Java community.

Possibly Related Posts: