Jersey (1.19.1)-Using filters

Source: Internet
Author: User

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.

TheClientandWebResourceClasses 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 theClientHas a filters added,filter1andfilter2, in that order, and theWebResourcehas 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:

    1. 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 .
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.