Java Reference
In-Depth Information
high probability that it will be. A database application might use an incremented version
column to calculate the ETag value.
The CustomerResource class is expanded to support conditional GETs and PUTs. Let's take
a look at the relevant pieces of code:
src/main/java/com/restfully/shop/services/CustomerResource.java
@Path ( "/customers" )
public
public class
class CustomerResource
CustomerResource
{
...
@GET
@Path ( "{id}" )
@Produces ( "application/xml" )
public
public Response getCustomer ( @PathParam ( "id" ) int
int id ,
@Context Request request ) {
Customer cust = customerDB . get ( id );
iif ( cust == null
null )
{
throw
throw new
new WebApplicationException ( Response . Status . NOT_FOUND );
}
iif ( sent == null
null ) System . out . println ( "No ETag sent by client" );
EntityTag tag = new
new EntityTag ( Integer . toString ( cust . hashCode ()));
CacheControl cc = new
new CacheControl ();
cc . setMaxAge ( 5 );
The getCustomer() method first starts out by retrieving the current Customer object identi-
fied by the id parameter. A current ETag value is created from the hash code of the Cus-
tomer object. A new Cache-Control header is instantiated as well.
Response . ResponseBuilder builder =
request . evaluatePreconditions ( tag );
iif ( builder != null
null ) {
System . out . println (
"** revalidation on the server was successful" );
builder . cacheControl ( cc );
return
return builder . build ();
}
Next, Request.evaluatePreconditions() is called to perform a conditional GET. If the
client has sent an If-None-Match header that matches the calculated current ETag , the meth-
Search WWH ::




Custom Search