uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

24.4.3 javax.ws.rs.core.Request

The javax.ws.rs.core.Request provides the functionality required for the Content Negotiation laid down in RFC 2616.

The following example describes the usage of javax.ws.rs.core.Request that is injected into the field of a root resource class:

package com.sample.resources;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
 
//Root resource class
@Path("/root")
public class Resource {
  //field in which the Request is injected by using the Context annotation
  private @Context Request request;
  //HTTP Entity Tag to be used in the content negotiation
  private EntityTag eTag = new EntityTag("a-resource-status-specific-tag");
 
  //Resource method
  @GET
  public Response getData() {
ResponseBuilder rb = null;
    //perform the content negotiation (evaluation of preconditions)
    //  null if matched with the precondition, if not matched, 
    //  the ResponseBuilder object is acquired in which 
    //  a suitable ETag HTTP header or a status code (412: Precondition Failed) is set
    rb = request.evaluatePreconditions(this.eTag);
    if (rb != null) {
      //if not matched with the precondition, generate an HTTP response 
      // from the ResponseBuilder object and return the response as it is
      return rb.build();
    } else {
      //if matched with the precondition, return the request data
      String data = "Some Information";
      return Response.ok().entity(data).build();
    }
  }
}

Consider the context root of the Web application (WAR file) containing the root resource class com.sample.resources.Resource to be example, and that the Web application is published on a host named sample.com. In the above example, with the HTTP GET request corresponding to the URL http://sample.com/example/root in which a-resource-status-specific-tag is specified in the If-Match HTTP header, first, the javax.ws.rs.core.Request context is injected into the request field and then the getData() method that can process the HTTP GET request is called. For this, content negotiation (in the above example, comparison between HTTP EntityTag of the resource and the If-Match HTTP header of the HTTP request) is performed with the getData() method and the value Some Information is acquired.