...
Input
...
and
...
Output
...
Stream
...
Adapters
...
The
...
Apache
...
Wink
...
Client
...
provides
...
the
...
ability
...
to
...
manipulate
...
raw
...
Http
...
input
...
and
...
output
...
entity
...
streams
...
through
...
the
...
InputStreamAdapter
...
and
...
the
...
OutputStreamAdapter
...
interfaces.
...
This
...
is
...
useful
...
for
...
modifying
...
the
...
input
...
and
...
output
...
streams,
...
regardless
...
of
...
the
...
actual
...
entity,
...
for
...
example
...
when
...
adding
...
compression
...
capabilities.
...
The
...
adapt()
...
method
...
of
...
the
...
output
...
stream
...
adapter
...
is
...
called
...
before
...
the
...
request
...
headers
...
are
...
committed,
...
in
...
order
...
to
...
allow
...
the
...
adapter
...
to
...
manipulate
...
them.
...
The
...
adapt()
...
method
...
of
...
the
...
input
...
stream
...
adapter
...
is
...
called
...
after
...
the
...
response
...
status
...
code
...
and
...
the
...
headers
...
are
...
received
...
in
...
order
...
to
...
allow
...
the
...
adapter
...
to
...
behave
...
accordingly.
...
Stream
...
Adapters
...
Example
...
The
...
following
...
example
...
demonstrates
...
how
...
to
...
implement
...
input
...
and
...
output
...
adapters.
Gzip Handler
Code Block | ||||
---|---|---|---|---|
| ||||
public h4. Gzip Handlerpublic class GzipHandler implements ClientHandler { public ClientResponse handle(ClientRequest request, HandlerContext context) { request.getHeaders().add("Accept-Encoding", "gzip"); context.addInputStreamAdapter(new GzipInputAdapter()); context.addOutputStreamAdapter(new GzipOutputAdapter()); return context.doChain(request); }} h4. |
Gzip
...
Input Stream Adapter
Code Block | ||||
---|---|---|---|---|
| ||||
class Stream Adapterclass GzipInputAdapter implements InputStreamAdapter{ public InputStream adapt(InputStream is, ClientResponse response) { String header = response.getHeaders().getFirst("Content-Encoding"); if (header != null && header.equalsIgnoreCase("gzip")) { return new GZIPInputStream(is); } return is; }} h4. |
Gzip
...
Output Stream Adapter
Code Block | ||||
---|---|---|---|---|
| ||||
class Stream Adapterclass GzipOutputAdapter implements OutputStreamAdapter { public OutputStream adapt(OutputStream os, ClientRequest request) { request.getHeaders().add("Content-Encoding", "gzip"); return new GZIPOutputStream(os); }} h4. Explanation The Gzip handler creates instances of the GzipInputAdapter and the GzipOutputAdapter and adds them to the stream adapters of the current request by invoking the addInputStreamAdapter() and addOutputStreamAdapter() on the HandlerContext |
Explanation
The Gzip handler creates instances of the GzipInputAdapter and the GzipOutputAdapter and adds them to the stream adapters of the current request by invoking the addInputStreamAdapter() and addOutputStreamAdapter() on the HandlerContext instance.