Java Reference
In-Depth Information
od returns immediately with an empty response body. In this case, a new
Cache-Control
header is sent back to refresh the
max-age
the client will use.
// Preconditions not met!
cust
.
setLastViewed
(
new
new
Date
().
toString
());
builder
=
Response
.
ok
(
cust
,
"application/xml"
);
builder
.
cacheControl
(
cc
);
builder
.
tag
(
tag
);
return
return
builder
.
build
();
}
}
If no
If-None-Match
header was sent or the preconditions were not met, the
Customer
is
sent back to the client with an updated
Cache-Control
header.
@Path
(
"{id}"
)
@PUT
@Consumes
(
"application/xml"
)
public
public
Response
updateCustomer
(
@PathParam
(
"id"
)
int
int
id
,
@Context
Request request
,
Customer update
) {
Customer cust
=
customerDB
.
get
(
id
);
iif
(
cust
==
null
null
)
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
EntityTag tag
=
new
throw
throw new
new
EntityTag
(
Integer
.
toString
(
cust
.
hashCode
()));
The
updateCustomer()
method is responsible for updating a customer. It first starts off by
finding the current
Customer
with the given
id
. From this queried customer, it generates the
up-to-date value of the
ETag
header.
Response
.
ResponseBuilder
builder
=
request
.
evaluatePreconditions
(
tag
);
iif
(
builder
!=
null
null
) {
// Preconditions not met!
return
return
builder
.
build
();
}
The current
ETag
header is compared against any
If-Match
header sent by the client. If it
does match, the update can be performed:
// Preconditions met, perform update
cust
.
setFirstName
(
update
.
getFirstName
());
cust
.
setLastName
(
update
.
getLastName
());