Java Reference
In-Depth Information
builder = Response . noContent ();
return
return builder . build ();
}
The updateCustomer() method obtains a customer ID and an instance of
javax.ws.rs.core.Request from the injected parameters. It then locates an instance of a
Customer object in some application-specific way (for example, from a database). From this
current instance of Customer , it creates an EntityTag from the hash code of the object. It
also finds the current timestamp of the Customer instance in some application-specific way.
The Request.evaluatePreconditions() method is then called with timestamp and tag
variables. If these values do not match the values within the If-Match and If-Unmodified-
Since headers sent with the request, evaluatePreconditions() sends back an instance of
a ResponseBuilder initialized with the error code 412, “Precondition Failed.” A Response
object is built and sent back to the client. If the preconditions are met, the service performs
the update and sends back a success code of 204, “No Content.”
With this code in place, we can now worry less about concurrent updates of our resources.
One interesting thought is that we did not have to come up with this scheme ourselves. It is
already defined within the HTTP specification. This is one of the beauties of REST, in that it
fully leverages the HTTP protocol.
Wrapping Up
In this chapter, you learned that HTTP has built-in facilities to help scale the performance of
our distributed systems. HTTP caching is a rich protocol that gives us a lot of control over
browser, proxy, and client caches. It helps tremendously in reducing network traffic and
speeding up response times for applications. Besides caching, distributed systems also have
the problem of multiple clients trying to update the same resource. The HTTP protocol again
comes to the rescue with well-defined semantics for handling concurrent updates. For both
caching and concurrent updates, JAX-RS provides some helper classes to make it easier to
enable these features in your Java applications. Chapter 25 contains some code you can use
to test-drive many of the concepts in this chapter.
Search WWH ::




Custom Search