File name: twbs_jaxrs_contextobjects_request.html
Evaluating request preconditions using Request objects
Using Java API for
RESTful Web Services (JAX-RS), you can use the Request object to access
request headers. The Request object provides methods for evaluating
preconditions and for selecting the optimal response variant based
on the request headers.
About this task
By using an injected Request object with the JAX-RS runtime
environment, you can easily evaluate HTTP headers preconditions. The
@javax.ws.rs.core.Context annotation indicates that a context object
is injected. The javax.ws.rs.core.Request is the interface of the
object that you want to inject. The injected Request object is useful
in evaluating HTTP header preconditions with dates, such as the If-Modified-Since value
and entity tags such as If-Match.
You can
also use the Request object when implementing advanced content negotiation.
To learn more about content negotiation, read about using XML content
in JAX-RS application requests and responses.
Avoid trouble: The granularity of dates used in HTTP headers is
not as precise as some dates used in data sources. For example, the
precision for a date in a database row might be defined to the millisecond.
However, the date in an HTTP header field is only precise to seconds.
When evaluating HTTP preconditions, if you compare a java.util.Date
object to the date in an HTTP header, the difference in precision
might produce unexpected results. To avoid this problem, normalize
the java.util.Date object before comparing to the date value in the
HTTP header.
gotcha
Procedure
- If a resource method signature can be modified, add the
@javax.ws.rs.core.Context javax.ws.rs.core.Request parameter to the
method. When the resource method is invoked, the JAX-RS
runtime environment passes an object that implements the Request object;
for example:
@Path("/contextexample")
public class RootResource {
@GET
@Produces("text/plain")
public Response getResource(@Context Request requestObj) {
Date lastModified = /* Gets the last modified date of this resource from a data source. */;
ResponseBuilder possibleResponse = requestObj.evaluatePreconditions(lastModified);
/* If the preconditions are not met, then ResponseBuilder is not null. Return the automatically generated response. */
if (possibleResponse != null) {
return possibleResponse.build();
}
return Response.ok("a resource representation").lastModified(lastModified).build();
}
}
- If a resource method signature cannot be modified and the
class is a root resource, add the @javax.ws.rs.core.Context javax.ws.rs.core.Request
field. When the resource is instantiated for a request,
an object that implements Request is injected; for example:
@Path("/contextexample")
public class RootResource {
@Context
Request requestObj;
@GET
@Produces("text/plain")
public Response getResource() {
Date lastModified = /* Gets the last modified date of this resource from a data source */;
ResponseBuilder possibleResponse = requestObj.evaluatePreconditions(lastModified);
/* If the preconditions are not met, then ResponseBuilder is not null. Return the automatically generated response */
if (possibleResponse != null) {
return possibleResponse.build();
}
return Response.ok("a resource representation").lastModified(lastModified).build();
}
}
Results
You have used the javax.ws.rs.core.Request object to evaluate
the HTTP headers of an incoming request, and determine the proper
response to the request.
In this information ...
| IBM Redbooks, demos, education, and more(Index)
Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof.
|
|
