Filtering requests and responses can provide useful functionality that's hidden from the application layer of building an D sending requests, and processing responses. Filters can read/modify the request URI, headers and entity or read/modify the response status, headers and entity.
TheClient
andWebResource
Classes extend fromfilterableAnd that enables the addition ofClientfilterInstances.A would WebResource
inherit filters from their creator, which can be a Client
or another WebResource
. Additional filters can is added to a after WebResource
it has been created. For requests, filters is applied in reverse order, starting with the filters and then moving to the WebResource
inheri Ted Filters. For responses, filters is applied in order, starting with inherited filters and followed by the filters added to The WebResource
.All filters is applied in the order in which they were added. For instance, the following example theClient
Has a filters added,filter1
andfilter2
, in that order, and theWebResource
has one filter added,filter3
:
Clientfilter filter1 == = = = C.resource (...); R.addfilter (FILTER3);
After a request have been built filter3
the request is filtered by, and in the filter2
filter1
order. After the response have been received the response is filtered by filter1
, and in that filter2
filter3
order, before the Response is returned.
Filters is implemented using the ' Russian doll ' stack-based pattern where a filter is responsible for calling th E Next filter in the ordered list of filters (or the next filter in the "Chain" of filters). The basic template for a filter is as follows:
class appclientfilter extends Span style= "color: #000000;" > Clientfilter { public Clientresponse Handle (Clientrequest cr) { // Modify the Request clientrequest MCR = ModifyRequest (CR); // Call the next filter CLIENTRESP Onse resp = GetNext (). handle (MCR); // Modify the response return ModifyResponse (resp); }}
The filter modifies the request (if required) by creating a new clientrequest or modifying the state of the Passe D Clientrequest before calling the next filter. The call to the next request would return the response, a clientresponse. The filter modifies the response (if required) by creating a new clientresponse or modifying the state of the returned Cli Entresponse. Then the filter returns the modified response. Filters is re-entrant and May is called by multiple threads performing requests and processing responses.
Supported filters
The Jersey Client API currently supports and filters:
- A GZIP content encoding filter, gzipcontentencodingfilter. If This filter was added then a request entity was compressed with The
content-encoding
&nb Sp;of gzip
, and a response entity if compressed with A of gzip
is decompressed. The filter declares An accept-encoding
of gzip
.
- A logging filter, loggingfilter. If This filter was added then the request and response headers as well as the entities be logged to a declared output stre Am if present, or To
system.out
if not. Often This filter would be placed at the end of the ordered list of filters to logs the request before it is sent and the RE Sponse after it is received.
The filters above is good examples that show how to modify or read request and response entities. Refer to the source code of the Jersey client for more details.
Jersey (1.19.1)-Using filters