Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The following example demonstrates how to implement input and output adapters.

Gzip Handler

TBDThe following code snippet is an example of adding a input and output adapters in the Gzip handler.

Code Block
xml
xml
public 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);
  }}

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() method and the addOutputStreamAdapter() method on the HandlerContext instance.

Gzip Input Stream Adapter

TBDThe following code snippet is an example of an implementation of a Gzip input stream adapter.

Code Block
xml
xml
class 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;
   }}

Explanation

The Gzip input stream adapter is responsible for wrapping the input stream with the Gzip input stream.

Gzip Output Stream Adapter

TBDThe following code snippet is an example of an implementation of a Gzip output stream adapter.

Code Block
xml
xml
class GzipOutputAdapter implements OutputStreamAdapter {
    public OutputStream adapt(OutputStream os,
                              ClientRequest request) {
        request.getHeaders().add("Content-Encoding", "gzip");
        return new GZIPOutputStream(os);
    }}

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 instanceoutput stream adapter is responsible for wrapping the output stream with the Gzip output stream.