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:divconq.net.ssl.SslHandler.java

License:Apache License

private void finishWrap(ChannelHandlerContext ctx, ByteBuf out, ChannelPromise promise, boolean inUnwrap) {
    if (out == null) {
        out = Unpooled.EMPTY_BUFFER;
    } else if (!out.isReadable()) {
        out.release();/* w w w  .  jav a2s  . c o m*/
        out = Unpooled.EMPTY_BUFFER;
    }

    if (promise != null) {
        ctx.write(out, promise);
    } else {
        ctx.write(out);
    }

    if (inUnwrap) {
        needsFlush = true;
    }
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private void wrapNonAppData(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException {
    ByteBuf out = null;//  w  ww .  ja v  a 2 s .c  om
    try {
        for (;;) {
            if (out == null) {
                out = allocateOutNetBuf(ctx, 0);
            }
            SSLEngineResult result = wrap(engine, Unpooled.EMPTY_BUFFER, out);

            if (result.bytesProduced() > 0) {
                ctx.write(out);
                if (inUnwrap) {
                    needsFlush = true;
                }
                out = null;
            }

            switch (result.getHandshakeStatus()) {
            case FINISHED:
                setHandshakeSuccess();
                break;
            case NEED_TASK:
                runDelegatedTasks();
                break;
            case NEED_UNWRAP:
                if (!inUnwrap) {
                    unwrapNonAppData(ctx);
                }
                break;
            case NEED_WRAP:
                break;
            case NOT_HANDSHAKING:
                setHandshakeSuccessIfStillHandshaking();
                // Workaround for TLS False Start problem reported at:
                // https://github.com/netty/netty/issues/1108#issuecomment-14266970
                if (!inUnwrap) {
                    unwrapNonAppData(ctx);
                }
                break;
            default:
                throw new IllegalStateException("Unknown handshake status: " + result.getHandshakeStatus());
            }

            if (result.bytesProduced() == 0) {
                break;
            }
        }
    } catch (SSLException e) {
        setHandshakeFailure(e);
        throw e;
    } finally {
        if (out != null) {
            out.release();
        }
    }
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private void unwrapNonAppData(ChannelHandlerContext ctx) throws SSLException {
    unwrap(ctx, Unpooled.EMPTY_BUFFER.nioBuffer(), 0);
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private void closeOutboundAndChannel(final ChannelHandlerContext ctx, final ChannelPromise promise,
        boolean disconnect) throws Exception {
    if (!ctx.channel().isActive()) {
        if (disconnect) {
            ctx.disconnect(promise);//w  w  w  .  j  a  v  a 2 s .com
        } else {
            ctx.close(promise);
        }
        return;
    }

    engine.closeOutbound();

    ChannelPromise closeNotifyFuture = ctx.newPromise();
    write(ctx, Unpooled.EMPTY_BUFFER, closeNotifyFuture);
    flush(ctx);
    safeClose(ctx, closeNotifyFuture, promise);
}

From source file:gribbit.http.request.decoder.HttpRequestDecoder.java

License:Open Source License

/** Decode an HTTP message. */
@Override//  www .  ja v a  2  s  .  c  o m
public void messageReceived(ChannelHandlerContext ctx, Object msg) {
    try {
        Log.info("Got message of type " + msg.getClass().getName());
        if (msg instanceof HttpRequest) {
            // Got a new HTTP request -- decode HTTP headers
            HttpRequest httpReq = (HttpRequest) msg;
            if (!httpReq.decoderResult().isSuccess()) {
                throw new BadRequestException(null);
            }

            // Free resources to avoid DoS attack by sending repeated unterminated requests
            freeResources();

            // Parse the HttpRequest fields. 
            request = new Request(ctx, httpReq);

            // Handle expect-100-continue
            List<CharSequence> allExpectHeaders = httpReq.headers().getAll(EXPECT);
            for (int i = 0; i < allExpectHeaders.size(); i++) {
                String h = allExpectHeaders.get(i).toString();
                if (h.equalsIgnoreCase("100-continue")) {
                    ctx.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                            HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER));
                    return;
                }
            }

            if (httpReq.method() == HttpMethod.POST) {
                // Start decoding HttpContent chunks.
                freeResources();
                postRequestDecoder = new HttpPostRequestDecoder(httpDataFactory, httpReq);
            }

        }
        if (msg instanceof LastHttpContent || msg instanceof FullHttpRequest) {
            // Reached end of HTTP request

            if (request != null) {
                // Check for WebSocket upgrade request
                if (!tryWebSocketHandlers(ctx, request.getHttpRequest())) {
                    // This is a regular HTTP request -- find a handler for the request
                    tryHttpRequestHandlers(ctx);
                }
                // After the last content message has been processed, free resources
                freeResources();
            }

        } else if (msg instanceof HttpContent) {
            // Decode HTTP POST body
            HttpContent chunk = (HttpContent) msg;
            if (!chunk.decoderResult().isSuccess()) {
                throw new BadRequestException(null);
            }
            handlePOSTChunk(chunk);

        } else if (msg instanceof WebSocketFrame) {
            // Handle WebSocket frame
            if (webSocketHandler == null) {
                // Connection was never upgraded to websocket
                throw new BadRequestException();
            }
            WebSocketFrame frame = (WebSocketFrame) msg;
            handleWebSocketFrame(ctx, frame);
        }
    } catch (Exception e) {
        exceptionCaught(ctx, e);
    }
}

From source file:holon.internal.http.netty.NettyOutput.java

License:Open Source License

public NettyOutput initialize(Channel channel) {
    this.channel = channel;
    this.buffer = Unpooled.EMPTY_BUFFER;
    return this;
}

From source file:holon.internal.http.netty.NettyRequestContext.java

License:Open Source License

private ByteBuf renderContent(Content content, Object context) {
    try {//from   w ww  .  j  a va  2s  .  co  m
        if (content == null) {
            return Unpooled.EMPTY_BUFFER;
        }

        content.render(output.initialize(channel), context);
        return output.buffer();
    } catch (IOException e) {
        throw new HolonException("Failed to render response.", e);
    }
}

From source file:io.advantageous.conekt.http.impl.AssembledFullHttpResponse.java

License:Open Source License

public AssembledFullHttpResponse(HttpResponse response) {
    this(response, Unpooled.EMPTY_BUFFER);
}

From source file:io.advantageous.conekt.http.impl.AssembledFullHttpResponse.java

License:Open Source License

private static LastHttpContent toLastContent(ByteBuf buf, HttpHeaders trailingHeaders, DecoderResult result) {
    if (buf.isReadable()) {
        if (trailingHeaders == null) {
            return new DefaultLastHttpContent(buf);
        } else {//from   w ww. j av  a 2 s  .co  m
            return new AssembledLastHttpContent(buf, trailingHeaders, result);
        }
    } else {
        if (trailingHeaders == null) {
            return LastHttpContent.EMPTY_LAST_CONTENT;
        } else {
            return new AssembledLastHttpContent(Unpooled.EMPTY_BUFFER, trailingHeaders, result);
        }
    }
}

From source file:io.advantageous.conekt.http.impl.ConektHttpHandler.java

License:Open Source License

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        ByteBuf buf = content.content();
        if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) {
            ByteBuf newBuf = safeBuffer(content, allocator);
            if (msg instanceof LastHttpContent) {
                LastHttpContent last = (LastHttpContent) msg;
                return new AssembledLastHttpContent(newBuf, last.trailingHeaders(), last.getDecoderResult());
            } else {
                return new DefaultHttpContent(newBuf);
            }/*from ww w  .java 2s  .co  m*/
        }
    } else if (msg instanceof WebSocketFrame) {
        ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator);
        boolean isFinal = ((WebSocketFrame) msg).isFinalFragment();
        FrameType frameType;
        if (msg instanceof BinaryWebSocketFrame) {
            frameType = FrameType.BINARY;
        } else if (msg instanceof CloseWebSocketFrame) {
            frameType = FrameType.CLOSE;
        } else if (msg instanceof PingWebSocketFrame) {
            frameType = FrameType.PING;
        } else if (msg instanceof PongWebSocketFrame) {
            frameType = FrameType.PONG;
        } else if (msg instanceof TextWebSocketFrame) {
            frameType = FrameType.TEXT;
        } else if (msg instanceof ContinuationWebSocketFrame) {
            frameType = FrameType.CONTINUATION;
        } else {
            throw new IllegalStateException("Unsupported websocket msg " + msg);
        }
        return new WebSocketFrameImpl(frameType, payload, isFinal);
    }
    return msg;
}