Example usage for io.netty.handler.codec.compression ZlibWrapper GZIP

List of usage examples for io.netty.handler.codec.compression ZlibWrapper GZIP

Introduction

In this page you can find the example usage for io.netty.handler.codec.compression ZlibWrapper GZIP.

Prototype

ZlibWrapper GZIP

To view the source code for io.netty.handler.codec.compression ZlibWrapper GZIP.

Click Source Link

Document

The GZIP wrapper as specified in <a href="http://tools.ietf.org/html/rfc1952">RFC 1952</a>.

Usage

From source file:com.ebay.jetstream.http.netty.client.HttpClient.java

License:MIT License

public void post(URI uri, Object content, Map<String, String> headers, ResponseFuture responsefuture)
        throws Exception {

    if (m_shutDown.get()) {
        throw new IOException("IO has been Shutdown = ");

    }/*w w w  . j a v a2  s .c o  m*/

    if (uri == null)
        throw new NullPointerException("uri is null or incorrect");

    if (!m_started.get()) {
        synchronized (this) {
            if (!m_started.get()) {
                start();
                m_started.set(true);
            }
        }
    }

    ByteBuf byteBuf = Unpooled.buffer(m_config.getInitialRequestBufferSize());
    ByteBufOutputStream outputStream = new ByteBufOutputStream(byteBuf);

    // transform to json
    try {
        m_mapper.writeValue(outputStream, content);
    } catch (JsonGenerationException e) {
        throw e;
    } catch (JsonMappingException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        outputStream.close();
    }

    EmbeddedChannel encoder;
    String contenteEncoding;
    if ("gzip".equals(m_config.getCompressEncoder())) {
        encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP, 1));
        contenteEncoding = "gzip";

    } else if ("deflate".equals(m_config.getCompressEncoder())) {
        encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB, 1));
        contenteEncoding = "deflate";
    } else {
        encoder = null;
        contenteEncoding = null;
    }

    if (encoder != null) {
        encoder.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
        encoder.writeOutbound(byteBuf);
        encoder.finish();
        byteBuf = (ByteBuf) encoder.readOutbound();
        encoder.close();
    }

    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            uri.toString(), byteBuf);
    if (contenteEncoding != null) {
        HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, contenteEncoding);
    }
    HttpHeaders.setHeader(request, HttpHeaders.Names.ACCEPT_ENCODING, "gzip, deflate");
    HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json");
    HttpHeaders.setContentLength(request, byteBuf.readableBytes());

    if (isKeepAlive())
        HttpHeaders.setHeader(request, HttpHeaders.Names.CONNECTION, "keep-alive");

    if (headers != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = headers.entrySet().iterator();
        while (it.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry pairs = (Map.Entry) it.next();
            HttpHeaders.setHeader(request, (String) pairs.getKey(), pairs.getValue());
        }
    }

    if (responsefuture != null) {
        RequestId reqid = RequestId.newRequestId();

        m_responseDispatcher.add(reqid, responsefuture);

        HttpHeaders.setHeader(request, "X_EBAY_REQ_ID", reqid.toString());
        // we expect this to be echoed in the response used for correlation.
    }

    if (m_dataQueue.size() < m_workQueueCapacity) {
        ProcessHttpWorkRequest workRequest = new ProcessHttpWorkRequest(this, uri, request);

        if (!m_dataQueue.offer(workRequest)) {
            if (responsefuture != null) {
                responsefuture.setFailure();
                m_responseDispatcher.remove(request.headers().get("X_EBAY_REQ_ID"));
            }
        }
    } else {
        throw new IOException("downstream Queue is full ");
    }

}

From source file:com.flysoloing.learning.network.netty.factorial.FactorialClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), FactorialClient.HOST, FactorialClient.PORT));
    }//  ww  w .  j a  v  a  2 s . co m

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    pipeline.addLast(new FactorialClientHandler());
}

From source file:com.flysoloing.learning.network.netty.factorial.FactorialServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }/*from  w  ww. j  av a  2s . c o  m*/

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    // Please note we create a handler for every new channel
    // because it has stateful properties.
    pipeline.addLast(new FactorialServerHandler());
}

From source file:com.flysoloing.learning.network.netty.portunification.PortUnificationServerHandler.java

License:Apache License

private void enableGzip(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();/*w  w  w  . ja  v a2s.c  o m*/
    p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    p.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    p.addLast("unificationB", new PortUnificationServerHandler(sslCtx, detectSsl, false));
    p.remove(this);
}

From source file:com.googlecode.protobuf.pro.duplex.client.DuplexTcpClientPipelineFactory.java

License:Apache License

/**
 * After RPC handshake has taken place, remove the RPC handshake
 * {@link ClientConnectResponseHandler} and add a {@link RpcClientHandler}
 * and {@link RpcServerHandler} to complete the Netty client side Pipeline.
 * //  w w  w .j a  v  a  2s  .c om
 * @param rpcClient
 * @return
 */
protected RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };
    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.CLIENT_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, rpcServiceRegistry, rpcServerCallExecutor, logger);
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, rpcClientRegistry);
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    return rpcClientHandler;
}

From source file:com.googlecode.protobuf.pro.duplex.server.DuplexTcpServerPipelineFactory.java

License:Apache License

public RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }//w w w. j  a v a  2  s.c  o m

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };

    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.SERVER_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, getRpcServiceRegistry(), getRpcServerCallExecutor(),
            getLogger());
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, getRpcClientRegistry());
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    if (log.isDebugEnabled()) {
        log.debug("completed Pipeline to " + rpcClient.getPeerInfo());
    }
    return rpcClientHandler;
}

From source file:com.heliosapm.streams.onramp.GZipDetector.java

License:Apache License

private void enableGzip(final ChannelHandlerContext ctx) {
    final ChannelPipeline p = ctx.pipeline();
    try {/*from   w ww.  j a v  a  2  s .  com*/
        //         p.addAfter("connmgr", "gzipdeflater", new JZlibEncoder(ZlibWrapper.GZIP){
        //            // TODO
        //         });            
        p.addAfter("gzipdetector", "gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
        p.remove(this);
    } catch (Exception ex) {
        log.error("Failed to add gzip handlers", ex);
    }
}

From source file:com.heliosapm.tsdblite.handlers.ProtocolSwitch.java

License:Apache License

private void enableGzip(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();/*from w  w  w . j a va  2 s.c om*/
    p.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    p.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    p.addLast("2ndPhaseSwitch", new ProtocolSwitch(false));
    p.remove(this);
    log.info("enabled gzip: [{}]", ctx.channel().id());
}

From source file:com.hop.hhxx.example.factorial.FactorialClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), io.netty.example.factorial.FactorialClient.HOST,
                io.netty.example.factorial.FactorialClient.PORT));
    }/*from   w ww  .j  a va  2 s  . c o m*/

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    pipeline.addLast(new FactorialClientHandler());
}

From source file:com.hop.hhxx.example.factorial.FactorialServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }/*  w w w.j a v  a 2s.  c o m*/

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast(new io.netty.example.factorial.BigIntegerDecoder());
    pipeline.addLast(new NumberEncoder());

    // and then business logic.
    // Please note we create a handler for every new channel
    // because it has stateful properties.
    pipeline.addLast(new FactorialServerHandler());
}