Filtering requests and responses can provide useful functionality that is hidden from the application layer of building and sending requests, and processing responses. Filters can read/modify the request URI, headers and entity or read/modify the response status, headers and entity.

The Client and WebResource classes extend from Filterable and that enables the addition of ClientFilter instances. A WebResource will inherit filters from its creator, which can be a Client or another WebResource. Additional filters can be added to a WebResource after it has been created. For requests, filters are applied in reverse order, starting with the WebResource filters and then moving to the inherited filters. For responses, filters are applied in order, starting with inherited filters and followed by the filters added to the WebResource. All filters are applied in the order in which they were added. For instance, in the following example the Client has two filters added, filter1 and filter2, in that order, and the WebResource has one filter added, filter3:

ClientFilter filter1 = ...
ClientFilter filter2 = ...
Client c = Client.create();
c.addFilter(filter1);
c.addFilter(filter2); ClientFilter filter3 = ...
WebResource r = c.resource(...);
r.addFilter(filter3);

After a request has been built the request is filtered by filter3filter2 and filter1 in that order. After the response has been received the response is filtered by filter1filter2 and filter3 in that order, before the response is returned.

Filters are implemented using the “russian doll” stack-based pattern where a filter is responsible for calling the 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 ClientFilter {
public ClientResponse handle(ClientRequest cr) {
// Modify the request
ClientRequest mcr = modifyRequest(cr);
// Call the next filter
ClientResponse 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 passed ClientRequest before calling the next filter. The call to the next request will return the response, a ClientResponse. The filter modifies the response (if required) by creating a new ClientResponse or modifying the state of the returned ClientResponse. Then the filter returns the modified response. Filters are re-entrant and may be called by multiple threads performing requests and processing responses.

Supported filters

The Jersey Client API currently supports two filters:

  1. A GZIP content encoding filter, GZIPContentEncodingFilter. If this filter is added then a request entity is compressed with the Content-Encoding of gzip, and a response entity if compressed with a Content-Encoding of gzip is decompressed. The filter declares an Accept-Encoding of gzip.
  2. A logging filter, LoggingFilter. If this filter is added then the request and response headers as well as the entities are logged to a declared output stream if present, or to System.out if not. Often this filter will be placed at the end of the ordered list of filters to log the request before it is sent and the response after it is received.

The filters above are 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) - Client API, Using filters的更多相关文章

  1. Jersey(1.19.1) - Client API, Uniform Interface Constraint

    The Jersey client API is a high-level Java based API for interoperating with RESTful Web services. I ...

  2. Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts

    Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...

  3. Jersey(1.19.1) - Client API, Overview of the API

    To utilize the client API it is first necessary to create an instance of a Client, for example: Clie ...

  4. Jersey(1.19.1) - Client API, Testing services

    The Jersey client API was originally developed to aid the testing of the Jersey server-side, primari ...

  5. Jersey(1.19.1) - Client API, Security with Http(s)URLConnection

    With Http(s)URLConnection The support for security, specifically HTTP authentication and/or cookie m ...

  6. Jersey(1.19.1) - Client API, Proxy Configuration

    为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...

  7. docker报Error response from daemon: client is newer than server (client API version: 1.24, server API version: 1.19)

    docker version Client: Version: 17.05.0-ce API version: 1.24 (downgraded from 1.29) Go version: go1. ...

  8. Jersey(1.19.1) - JSON Support

    Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...

  9. Jersey(1.19.1) - XML Support

    As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incomin ...

随机推荐

  1. Unity3D之Mecanim动画系统学习笔记(三):Animation View

    动画组件之间的关系 我们先看一张图: 这里我们可以看到,我们在GameObject之上绑定的Animator组件是控制模型进行动画播放的. 而其属性Controller则对应一个Animator Co ...

  2. iOS中多控制器的使用

    通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...

  3. arcgis下载

    你懂的~ t.cn/RA4cc3k 密码ygdr 包含10.2全部,含有(亲测)字样表示测试过OK的,SP是从esri网站下载的几乎全部patch和sp,包括desktop.engine和sever: ...

  4. linux杂谈(十八):DNS服务器的配置(一)

    原文地址: http://blog.chinaunix.net/uid-29622064-id-4242123.html 1.DNS服务器简介 域名系统(英文:Domain Name System,縮 ...

  5. Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学

    H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...

  6. 一个不错的JavaScript解析浏览器路径方法(转)

    JavaScript中有时需要用到当前的请求路径等涉及到url的情况,正常情况下我们可以使用location对象来获取我们需要的信息,本文从另外一个途径来解决这个问题,而且更加巧妙 方法如下: fun ...

  7. HDU 5235 Friends (2015 Multi-University Training Contest 2 搜索+剪枝)

    题目链接:pid=5305">传送门 题意: n个人给定m个关系.每一个关系为x,y表示x,y是朋友.可是可能是online friends,也可能是offline friends. ...

  8. mysql 5.1 到 mysql 5.2的出现的索引BTREE问题 use near 'USING BTREE

    转自:http://hi.baidu.com/our_poll/item/669c5ce885b33ff1e0a5d4fc 我本机测试是安装的 mysql 5.1 , 但服务器上确是使用的 mysql ...

  9. iOS与日期相关的操作

    // Do any additional setup after loading the view, typically from a nib. //得到当前的日期 注意week1是星期天 NSDat ...

  10. codeigniter分页类传多个参数(转)

    http://example.com/index.php/控制器/方法名/页面的偏移值 页面的偏移值必须是方法名后第一个参数,否者分页类不能判断当前是哪一页,而用ci的分页类进行页面跳转时他是把偏移值 ...