Java Reference
In-Depth Information
PUT /customers/123
HTTP / 1.1
If-Match: "3141271342554322343200"
If-Unmodified-Since: Tue, 15 May 2013 09:56 EST
Content-Type: application/xml
/customers/123 HTTP
<customer id="123">...</customer>
You are not required to send both the If-Match and If-Unmodified-Since headers. One or
the other is sufficient to perform a conditional PUT or POST. When the server receives this
request, it checks to see if the current ETag of the resource matches the value of the If-
Match header and also to see if the timestamp on the resource matches the If-Unmodified-
Since header. If these conditions are not met, the server will return an error response code of
412, “Precondition Failed.” This tells the client that the representation it is updating was
modified concurrently and that it should retry. If the conditions are met, the service performs
the update and sends a success response code back to the client.
JAX-RS and Conditional Updates
To do conditional updates with JAX-RS, you use the Request.evaluatePreconditions()
method again. Let's look at how we can implement it within Java code:
@Path ( "/customers" )
public class
class CustomerResource
CustomerResource {
@Path ( "{id}" )
@Consumes ( "application/xml" )
public Response updateCustomer ( @PathParam ( "id" ) int
int id ,
@Context Request request ,
Customer update ) {
Customer cust = findCustomer ( id );
EntityTag tag = new
new EntityTag (
Integer . toString ( cust . hashCode ()));
Date timestamp = ...; // get the timestamp
ResponseBuilder builder =
request . evaluatePreconditions ( timestamp , tag );
iif ( builder != null
null ) {
// Preconditions not met!
return builder . build ();
... perform the update ...
Search WWH ::

Custom Search