Java Reference
In-Depth Information
ResponseBuilder
evaluatePreconditions
(
Date lastModified
,
EntityTag eTag
);
}
The overloaded
evaluatePreconditions()
methods take a
javax.ws.rs.core.EntityTag
, a
java.util.Date
that represents the last modified
timestamp, or both. These values should be current, as they will be compared with the values
of the
If-Modified-Since
,
If-Unmodified-Since
, or
If-None-Match
headers sent with
the request. If these headers don't exist or if the request header values don't pass revalida-
tion, this method returns null and you should send back a 200, “OK,” response with the new
representation of the resource. If the method does not return null, it returns a preinitialized
instance of a
ResponseBuilder
with the response code preset to 304. For example:
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
@Path
(
"{id}"
)
@GET
@Produces
(
"application/xml"
)
public
public
Response
getCustomer
(
@PathParam
(
"id"
)
int
int
id
,
@Context
Request request
) {
Customer cust
=
findCustomer
(
id
);
EntityTag tag
=
new
new
EntityTag
(
Integer
.
toString
(
cust
.
hashCode
()));
CacheControl cc
=
new
new
CacheControl
();
cc
.
setMaxAge
(
1000
);
ResponseBuilder builder
=
request
.
evaluatePreconditions
(
tag
);
iif
(
builder
!=
null
null
) {
builder
.
cacheControl
(
cc
);
return
return
builder
.
build
();
}
// Preconditions not met!
builder
=
Response
.
ok
(
cust
,
"application/xml"
);
builder
.
cacheControl
(
cc
);
builder
.
tag
(
tag
);
return
return
builder
.
build
();
}
In this example, we have a
getCustomer()
method that handles GET requests for the
/cus-
tomers/\{id}
URI pattern. An instance of
javax.ws.rs.core.Request
is injected into the
method using the
@Context
annotation. We then find a
Customer
instance and create a cur-