Jersey(1.19.1) - Client API, Using filters
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 filter3
, filter2
and filter1
in that order. After the response has been received the response is filtered by filter1
, filter2
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:
- A GZIP content encoding filter, GZIPContentEncodingFilter. If this filter is added then a request entity is compressed with the
Content-Encoding
ofgzip
, and a response entity if compressed with aContent-Encoding
ofgzip
is decompressed. The filter declares anAccept-Encoding
ofgzip
. - 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Jersey(1.19.1) - Client API, Proxy Configuration
为 Jersey Client 设置代理,可以使用带有 ClientHandler 参数的构造方法创建 Client 实例. public static void main(String[] args ...
- 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. ...
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- Jersey(1.19.1) - XML Support
As you probably already know, Jersey uses MessageBodyWriters and MessageBodyReaders to parse incomin ...
随机推荐
- 做XH2.54杜邦线材料-导线
好多市场上买的杜邦线质量一般,今天选了一种别的线 线色:红 黑 黄 绿 白 棕 蓝 线规:正标UL1007-24AWG 产品说明: * UL1007电子连接线 * 线号:24 ...
- POJ 1088 滑雪 (记忆化搜索)
题目链接:http://poj.org/problem?id=1088 题意很好懂,就是让你求一个最长下降路线的长度. dp[i][j]记录的是i j这个位置的最优的长度,然后转移方程是dp[i][j ...
- easyUI之tree
转自:http://www.cnblogs.com/CoreCaiNiao/archive/2010/08/20/1804387.html http://blog.csdn.net/l27775918 ...
- mysql中utf8_bin、utf8_general_ci、utf8_general_cs编码区别
在mysql中存在着各种utf8编码格式,如下表: 1)utf8_bin 2)utf8_general_ci 3)utf8_general_cs utf8_bin将字符串中的每一个字符用二进制数据存储 ...
- 很值得学习的java 画图板源码
很值得学习的java 画图板源码下载地址:http://download.csdn.net/source/2371150 package minidrawpad; import java.awt.*; ...
- Codeforces Round #334 (Div. 2) D. Moodular Arithmetic 环的个数
D. Moodular Arithmetic Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/60 ...
- delphi Edit - TActionList
Edit TEditCut TEditCopy TEditpaste TEditSelectAll TEditUndo TEditDelete 编辑 ...
- Winform 水印TextBox
方法一: public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label() ...
- web开发技术点解析
一.控件篇 1.radio控件 在创建单选控件时,要做到多个radio有单选功能.必须把多个radio的name属性值设置为同样的,否则,多个radio之间是没有联系的. 二.样式篇 1.获取图片中的 ...
- 利用text插件和css插件优化web应用
JavaScript的模块化开发到如今,已经相当成熟了,当然,一个应用包含的不仅仅有js,还有html模板和css文件. 那么,如何将html和css也一起打包,来减少没必要的HTTP请求数呢? 本文 ...