Example usage for io.netty.buffer Unpooled EMPTY_BUFFER

List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled EMPTY_BUFFER.

Prototype

ByteBuf EMPTY_BUFFER

To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.

Click Source Link

Document

A buffer whose capacity is 0 .

Usage

From source file:com.tesora.dve.server.connectionmanager.loaddata.MSPLoadDataDecoder.java

License:Open Source License

private void resumeInput(ChannelHandlerContext ctx) {
    if (paused) { //make sure we don't recurse infinitely.
        paused = false;/*  ww  w  .  j  a v a2 s.  c  o m*/
        ctx.channel().config().setAutoRead(true);
        ctx.pipeline().fireChannelRead(Unpooled.EMPTY_BUFFER); //this flushes any partial packets held upstream (may cause recursion).
    }
}

From source file:com.topsec.bdc.platform.api.server.http.HttpSnoopServerHandler.java

License:Apache License

/**
 * send back response and close the connection in server side.
 * //from ww w. j  av a2 s.com
 * @param ctx
 * @param responseStatus
 * @param responseContent
 */
private void writeResponseAndClose(ChannelHandlerContext ctx, String message) {

    //response?????
    ByteBuf responseContentBuf = null;
    if (Assert.isEmptyString(message) == true) {
        responseContentBuf = Unpooled.copiedBuffer(_responseStatus.reasonPhrase(), CharsetUtil.UTF_8);
    } else {
        responseContentBuf = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8);
    }
    // Decide whether to close the connection or not.
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, _responseStatus, responseContentBuf);
    //???
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    //disallow keepAlive
    //boolean keepAlive = HttpHeaders.isKeepAlive(_request);
    //response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    // Add 'Content-Length' header only for a keep-alive connection.
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    // Add keep alive header as per:
    // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
    //???
    response.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
    // Encode the cookie.
    String cookieString = _request.headers().get(COOKIE);
    if (cookieString != null) {
        Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
        if (!cookies.isEmpty()) {
            // Reset the cookies if necessary.
            for (Cookie cookie : cookies) {
                response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
            }
        }
    } else {
        // Browser sent no cookie.  Add some.
        //response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode("key1", "value1"));
        //response.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode("key2", "value2"));
    }
    try {
        // Write the response.
        ctx.write(response);
        // close connection
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    } catch (Throwable t) {
        t.printStackTrace();
        ctx.close();
    }
}

From source file:com.topsec.bdc.platform.api.test.http.snoop.HttpSnoopServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {

    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;

        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);//from  w w w  .  ja va 2s .  c  om
        }

        buf.setLength(0);
        buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
        buf.append("===================================\r\n");

        buf.append("VERSION: ").append(request.getProtocolVersion()).append("\r\n");
        buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n");
        buf.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n");

        HttpHeaders headers = request.headers();
        if (!headers.isEmpty()) {
            for (Map.Entry<String, String> h : headers) {
                String key = h.getKey();
                String value = h.getValue();
                buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n");
            }
            buf.append("\r\n");
        }

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        Map<String, List<String>> params = queryStringDecoder.parameters();
        if (!params.isEmpty()) {
            for (Entry<String, List<String>> p : params.entrySet()) {
                String key = p.getKey();
                List<String> vals = p.getValue();
                for (String val : vals) {
                    buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n");
                }
            }
            buf.append("\r\n");
        }

        appendDecoderResult(buf, request);
    }

    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            buf.append("CONTENT: ");
            buf.append(content.toString(CharsetUtil.UTF_8));
            buf.append("\r\n");
            appendDecoderResult(buf, request);
        }

        if (msg instanceof LastHttpContent) {
            buf.append("END OF CONTENT\r\n");

            LastHttpContent trailer = (LastHttpContent) msg;
            if (!trailer.trailingHeaders().isEmpty()) {
                buf.append("\r\n");
                for (String name : trailer.trailingHeaders().names()) {
                    for (String value : trailer.trailingHeaders().getAll(name)) {
                        buf.append("TRAILING HEADER: ");
                        buf.append(name).append(" = ").append(value).append("\r\n");
                    }
                }
                buf.append("\r\n");
            }

            if (!writeResponse(trailer, ctx)) {
                // If keep-alive is off, close the connection once the content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
        }
    }
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testEmptyHttpDataFrame() throws Exception {
    int length = 0;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = dataFrame(length, flags, streamId);
    decoder.decode(frame);/*w  w w.  j av a2s.  co m*/

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER);
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testLastHttpDataFrame() throws Exception {
    int length = 0;
    byte flags = 0x01; // END_STREAM
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = dataFrame(length, flags, streamId);
    decoder.decode(frame);/*from   w ww  .  ja v  a 2  s .c o m*/

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFrame(streamId, true, false, Unpooled.EMPTY_BUFFER);
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testLastSegmentHttpDataFrame() throws Exception {
    int length = 0;
    byte flags = 0x02; // END_SEGMENT
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = dataFrame(length, flags, streamId);
    decoder.decode(frame);/*from  w  w  w  . ja v a2  s .  c  o m*/

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFrame(streamId, false, true, Unpooled.EMPTY_BUFFER);
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testPaddedHttpDataFrame() throws Exception {
    int length = RANDOM.nextInt() & 0x3FFF | 0x01;
    byte flags = 0x08; // PADDED
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int padding = Math.min(RANDOM.nextInt() & 0xFF, length - 1);

    ByteBuf frame = dataFrame(length, flags, streamId);
    frame.writeByte(padding);/*w w w.  ja va 2s.  com*/
    writeRandomData(frame, length - 1);
    decoder.decode(frame);

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFramePadding(streamId, false, padding + 1);
    int dataLength = length - 1 - padding;
    if (dataLength == 0) {
        inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER);
    } else {
        for (int i = 0; i < dataLength; i += 8192) {
            // data frames do not exceed maxChunkSize
            int off = HTTP_FRAME_HEADER_SIZE + 1 + i;
            int len = Math.min(dataLength - i, 8192);
            inOrder.verify(delegate).readDataFrame(streamId, false, false, frame.slice(off, len));
        }
    }
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testHttpDataFrameReservedBits() throws Exception {
    int length = 0;
    byte flags = (byte) 0xC4; // should ignore any unknown flags
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = dataFrame(length, flags, streamId);
    setReservedBits(frame);//from  www  . ja  va  2 s . c om
    decoder.decode(frame);

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER);
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testEmptyHttpDataFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf frame = releaseLater(ENCODER.encodeDataFrame(streamId, false, Unpooled.EMPTY_BUFFER));
    assertDataFrame(frame, streamId, false, Unpooled.EMPTY_BUFFER);
}

From source file:com.twitter.http2.HttpFrameEncoderTest.java

License:Apache License

@Test
public void testLastHttpDataFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf frame = releaseLater(ENCODER.encodeDataFrame(streamId, true, Unpooled.EMPTY_BUFFER));
    assertDataFrame(frame, streamId, true, Unpooled.EMPTY_BUFFER);
}