Package io.netty.buffer

Examples of io.netty.buffer.ByteBuf


        this.remoteSocketAddress = remoteSocketAddress;
    }

    @Override
    public ByteBuf intercept(ChannelHandlerContext ctx, ByteBuf channelBuffer, Logger logger) throws Exception {
        ByteBuf channelBufferCopy = Unpooled.copiedBuffer(channelBuffer);
        logger.debug("INTERCEPTING - REQUEST: " + channelBuffer.toString(Charsets.UTF_8));
        try {
            List<ByteBuf> allRequestRawChunks = new ArrayList<ByteBuf>();
            List<Object> requestHttpFormattedChunks = new ArrayList<Object>();
            httpRequestDecoder.callDecode(ctx, channelBufferCopy, requestHttpFormattedChunks);

            for (Object httpChunk : requestHttpFormattedChunks) {
                if (httpChunk instanceof HttpRequest) {
                    HttpRequest httpRequest = (HttpRequest) httpChunk;
                    httpRequest.headers().remove(HttpHeaders.Names.ACCEPT_ENCODING);
                    if (remoteSocketAddress != null) {
                        httpRequest.headers().set(HttpHeaders.Names.HOST, remoteSocketAddress.getHostName() + ":" + remoteSocketAddress.getPort());
                    }
                    httpRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
                }
                logger.debug("HTTP-FORMATTED -REQUEST- " + httpChunk.getClass().getSimpleName() + " -- " + httpChunk);
                if (!(httpChunk instanceof LastHttpContent)) {
                    List<Object> requestRawChunks = new ArrayList<Object>();
                    httpRequestEncoder.encode(ctx, httpChunk, requestRawChunks);
                    for (Object rawChunk : requestRawChunks) {
                        if (rawChunk instanceof ByteBuf) {
                            allRequestRawChunks.add((ByteBuf) rawChunk);
                        }
                    }
                }
            }

            return Unpooled.copiedBuffer(allRequestRawChunks.toArray(new ByteBuf[allRequestRawChunks.size()]));
        } finally {
            channelBufferCopy.release();
        }
    }
View Full Code Here


    @Override
    public ByteBuf intercept(ChannelHandlerContext ctx, ByteBuf channelBuffer, Logger logger) throws Exception {
        if (true) {
            return channelBuffer;
        }
        ByteBuf channelBufferCopy = Unpooled.copiedBuffer(channelBuffer);
        logger.debug("INTERCEPTING - RESPONSE: " + channelBuffer.toString(Charsets.UTF_8));
        try {
            List<ByteBuf> allResponseRawChunks = new ArrayList<ByteBuf>();
            List<Object> responseHttpFormattedChunks = new ArrayList<Object>();
            httpResponseDecoder.callDecode(ctx, channelBufferCopy, responseHttpFormattedChunks);

            for (Object httpChunk : responseHttpFormattedChunks) {
                if (httpChunk instanceof HttpResponse) {
                    HttpResponse httpResponse = (HttpResponse) httpChunk;
                    httpResponse.headers().remove(HttpHeaders.Names.ACCEPT_ENCODING);
                    httpResponse.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
                }
                logger.debug("HTTP-FORMATTED -RESPONSE- " + httpChunk.getClass().getSimpleName() + " -- " + httpChunk);
                if (!(httpChunk instanceof LastHttpContent)) {
                    List<Object> responseRawChunks = new ArrayList<Object>();
//                    if(httpChunk instanceof DefaultHttpContent) {
//                        httpChunk = ((DefaultHttpContent)httpChunk).content();
//                    }
                    httpResponseEncoder.encode(ctx, httpChunk, responseRawChunks);
                    for (Object rawChunk : responseRawChunks) {
                        if (rawChunk instanceof ByteBuf) {
                            allResponseRawChunks.add((ByteBuf) rawChunk);
                        }
                    }
                }
            }

            return Unpooled.copiedBuffer(allResponseRawChunks.toArray(new ByteBuf[allResponseRawChunks.size()]));
        } finally {
            channelBufferCopy.release();
        }
    }
View Full Code Here

    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            final ByteBuf chunk = (ByteBuf) msg;
            if (flushedBuffer) {
                bufferedMode = false;
            }
            if (bufferedMode) {

                flushContent = false;

                if (contentLength != null) {
                    contentSoFar += chunk.readableBytes();
                } else {
                    // find content length
                    BasicHttpDecoder basicHttpDecoder = new BasicHttpDecoder(Unpooled.copiedBuffer(chunk));
                    contentLength = basicHttpDecoder.getContentLength();
                    contentSoFar = (chunk.readableBytes() - basicHttpDecoder.getContentStart());
                }

                if (logger.isTraceEnabled()) {
                    logger.trace("CHUNK:                     ---\n-" + System.getProperty("line.separator") + Unpooled.copiedBuffer(chunk).toString(Charsets.UTF_8) + "\n-" + System.getProperty("line.separator"));
                    logger.trace("CONTENT-SO-FAR-PRE-CHUNK:  --- " + (contentSoFar - Unpooled.copiedBuffer(chunk).toString(Charsets.UTF_8).length()));
                    logger.trace("CHUNK-SIZE:                --- " + chunk.readableBytes());
                    logger.trace("CONTENT-SO-FAR-PRE-CHUNK:  --- " + contentSoFar);
                    if (contentLength != null) {
                        logger.trace("CONTENT-REMAINING:         --- " + (contentLength - contentSoFar));
                        logger.trace("CONTENT-LENGTH:            --- " + contentLength);
                    }
                }

                if (contentLength != null) {
                    logger.trace("Flushing buffer as all content received");
                    flushContent = (contentSoFar >= contentLength) || (chunk.readableBytes() == 0);
                }
                try {
                    channelBuffer.writeBytes(chunk);
                    ctx.channel().read();
                } catch (IndexOutOfBoundsException iobe) {
                    logger.trace("Flushing buffer upstream and switching to chunked mode as downstream response too large");
                    bufferedMode = false;
                    // write and flush buffer upstream
                    if (outboundChannel.isActive() && channelBuffer.isReadable()) {
                        logger.debug("CHANNEL READ EX: " + chunk.toString(Charsets.UTF_8));
                        outboundChannel.writeAndFlush(channelBuffer).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (future.isSuccess()) {
                                    // write and flush this chunk upstream in case this single chunk is too large for buffer
                                    channelRead(ctx, chunk);
                                } else {
                                    logger.warn("Failed to write to: " + remoteSocketAddress, future.cause());
                                    future.channel().close();
                                }
                            }
                        });
                    }
                }
            } else {
                bufferedMode = false;
                if (outboundChannel.isActive()) {
                    logger.debug("CHANNEL READ NOT-BUFFERING: " + chunk.toString(Charsets.UTF_8));
                    outboundChannel.writeAndFlush(chunk).addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (future.isSuccess()) {
                                // was able to flush out data, start to read the next chunk
View Full Code Here

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof FullHttpMessage && ((HttpObject) msg).getDecoderResult().isSuccess()) {
            HttpRequest request = (HttpRequest) msg;

            ByteBuf requestContent = Unpooled.copiedBuffer(((HttpContent) msg).content());

            DefaultFullHttpRequest mockServerHttpRequest = new DefaultFullHttpRequest(request.getProtocolVersion(), request.getMethod(), request.getUri(), requestContent);
            mockServerHttpRequest.headers().add(request.headers());

            LastHttpContent trailer = (LastHttpContent) msg;
View Full Code Here

            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
        }
    }

    private ByteBuf getBody(HttpResponse httpResponse) {
        ByteBuf content = Unpooled.buffer(0);
        if (httpResponse.getBodyAsString() != null) {
            if (httpResponse.getBody() instanceof BinaryBody) {
                content = Unpooled.copiedBuffer(Base64Converter.base64StringToBytes(httpResponse.getBodyAsString()));
            } else {
                content = Unpooled.copiedBuffer(httpResponse.getBodyAsString().getBytes());
View Full Code Here

    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            final ByteBuf chunk = (ByteBuf) msg;
            if (flushedBuffer) {
                bufferedMode = false;
            }
            if (bufferedMode) {

                flushContent = false;

                if (contentLength != null) {
                    contentSoFar += chunk.readableBytes();
                } else {
                    // find content length
                    BasicHttpDecoder basicHttpDecoder = new BasicHttpDecoder(Unpooled.copiedBuffer(chunk));
                    contentLength = basicHttpDecoder.getContentLength();
                    contentSoFar = (chunk.readableBytes() - basicHttpDecoder.getContentStart());
                }

                if (logger.isTraceEnabled()) {
                    logger.trace("CHUNK:                     ---\n-" + System.getProperty("line.separator") + Unpooled.copiedBuffer(chunk).toString(Charsets.UTF_8) + "\n-" + System.getProperty("line.separator"));
                    logger.trace("CONTENT-SO-FAR-PRE-CHUNK:  --- " + (contentSoFar - Unpooled.copiedBuffer(chunk).toString(Charsets.UTF_8).length()));
                    logger.trace("CHUNK-SIZE:                --- " + chunk.readableBytes());
                    logger.trace("CONTENT-SO-FAR-PRE-CHUNK:  --- " + contentSoFar);
                    if (contentLength != null) {
                        logger.trace("CONTENT-REMAINING:         --- " + (contentLength - contentSoFar));
                        logger.trace("CONTENT-LENGTH:            --- " + contentLength);
                    }
                }

                if (contentLength != null) {
                    logger.trace("Flushing buffer as all content received");
                    flushContent = (contentSoFar >= contentLength) || (chunk.readableBytes() == 0);
                }
                try {
                    channelBuffer.writeBytes(chunk);
                    ctx.channel().read();
                } catch (IndexOutOfBoundsException iobe) {
                    logger.trace("Flushing buffer and switching to chunked mode as buffer full");
                    flushContent = true;
                }
                if (flushContent) {
                    flushedBuffer = true;
                    if (relayChannel.isActive() && channelBuffer.isReadable()) {
                        logger.debug("CHANNEL READ EX: " + chunk.toString(Charsets.UTF_8));
                        relayChannel.writeAndFlush(channelBuffer).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (future.isSuccess()) {
                                    channelBuffer.clear();
                                    // write and flush this chunk upstream in case this single chunk is too large for buffer
                                    channelRead(ctx, chunk);
                                } else {
                                    logger.warn("Failed to send flush channel buffer [" + channelBuffer + "]", future.cause());
                                    future.channel().close();
                                }
                            }
                        });
                    }
                }
            } else {
                bufferedMode = false;
                if (relayChannel.isActive()) {
                    logger.debug("CHANNEL READ NOT-BUFFERING: " + chunk.toString(Charsets.UTF_8));
                    relayChannel.writeAndFlush(chunk).addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (future.isSuccess()) {
                                // was able to flush out data, start to read the next chunk
View Full Code Here

        roundTrip( "80FFFF" );
    }

    protected void roundTrip(String hex) {
        BigInteger i = new BigInteger( hex, 16 );
        ByteBuf buf = BigIntegerUtils.toBuf( i );
        assertEquals( hex.length() / 2, buf.readableBytes() );
        BigInteger i2 = BigIntegerUtils.fromBuf( buf );
        assertEquals( i, i2 );
    }
View Full Code Here

*/
public class OpenSSLKDFTest {

    @Test
    public void testGeneration() {
        ByteBuf password = Unpooled.buffer();
        password.writeBytes( "tacos".getBytes() );
        OpenSSLKDF kdf = new OpenSSLKDF(password, 64, 8);

        ByteBuf key = kdf.getKey();

        assertEquals(key.getByte(0) & 0xff, 0xda);
        assertEquals(key.getByte(1) & 0xff, 0xce);
        assertEquals(key.getByte(2) & 0xff, 0xdf);
        assertEquals(key.getByte(3) & 0xff, 0x41);
        assertEquals(key.getByte(4) & 0xff, 0x21);
        assertEquals(key.getByte(5) & 0xff, 0x04);
        assertEquals(key.getByte(6) & 0xff, 0x44);
        assertEquals(key.getByte(7) & 0xff, 0xfe);

        ByteBuf iv = kdf.getIv();

        assertEquals(iv.getByte(0) & 0xff, 0x85);
        assertEquals(iv.getByte(1) & 0xff, 0x47);
        assertEquals(iv.getByte(2) & 0xff, 0xf5);
        assertEquals(iv.getByte(3) & 0xff, 0xb1);
        assertEquals(iv.getByte(4) & 0xff, 0xcf);
        assertEquals(iv.getByte(5) & 0xff, 0x08);
        assertEquals(iv.getByte(6) & 0xff, 0x5a);
        assertEquals(iv.getByte(7) & 0xff, 0x6c);


    }
View Full Code Here

    public void write(ByteBuf buf) throws IOException {
        this.channelFuture.channel().writeAndFlush(buf.retain());
    }

    public void writeUtf8String(String str) throws IOException {
        ByteBuf buf = this.channelFuture.channel().alloc().buffer();
        buf.writeBytes(str.getBytes("utf8"));
        write(buf);
    }
View Full Code Here

        buf.writeBytes(str.getBytes("utf8"));
        write(buf);
    }

    public void writeAsciiString(String str) throws IOException {
        ByteBuf buf = this.channelFuture.channel().alloc().buffer();
        buf.writeBytes(str.getBytes("us-ascii"));
        write(buf);
    }
View Full Code Here

TOP

Related Classes of io.netty.buffer.ByteBuf

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.