Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 32 web apps control along with content negotiation parameters such as media types language and encoding Finally the Client helps obtain an instance of the Invocation object by using one of its buildXXX methods An instance of Invocation encapsulates an HTTP request and allows synchronous and asynchronous request submission via overloaded versions of the invoke and submit methods respectively Figure 1 illustrates this sequence Lets look at an example Client client ClientBuilder newClient WebTarget webTarget client target http service com user queryParam card 4275391126915480 Invocation Builder builder webTarget request text plain Invocation invocation builder header testheader testvalue buildGet Response response invocation invoke Now lets dissect this code snippet to gain a better understanding of whats going on An instance of Client obtained via the Client Builder class is used to specify the target URI An instance of WebTarget is created as a result it is further used to specify the expected response media type equivalent to an Accept HTTP header and associated URI path and query parameters This creates an instance of Invocation Builder which further builds a complete HTTP GET request The Invocation instance is used to deliver the request to the server The Configurable interface The Client ClientBuilder WebTarget and Invocation objects implement the javax ws rs core Configurable interface This allows them to define custom JAX RS components such as filters interceptors entity providers message readers and writers and so on This is made possible using the overloaded versions of the register method which Ill discuss shortly Note that this API is applicable to server side JAX RS components filters interceptors and so on as well Filters Filters and interceptors discussed later are another bigticket feature in JAX RS 20 They provide aspect oriented programming AOP like capabilities within JAX RS applications and enable developers Server side response filters are similar to their counterpart request filters in terms of their utility the read and mutate aspects of the response for example HTTP headers and the programming model for example being executed as a chain in a userdefined or default order to implement cross cutting application specific concerns which ideally should not be sprinkled all over the business logic These capabilities include authentication authorization request response validation logging and so forth The AOP based programming model involves interposing in methods of JAX RS resource classes and dealing with or mutating components of HTTP request response headers request URIs and the invoked HTTP method GET POST and the others Server side request filters Server side request filters act on incoming HTTP requests from the clients They are called prior to JAX RS resource method invocation thus providing an opportunity to act on certain characteristics of the incoming HTTP requests To implement a server side request filter implement the javax ws rs container ContainerRequestFilter interface which is an extension provided by JAX RS An instance of the javax ws rs container Container
You must have JavaScript enabled to view digital editions.