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.mastfrog.acteur.server.EventImpl.java

License:Open Source License

@Override
public ByteBuf getContent() {
    return req instanceof FullHttpRequest ? ((FullHttpRequest) req).content() : Unpooled.EMPTY_BUFFER;
}

From source file:com.mastfrog.acteur.server.HttpObjectAggregator.java

License:Open Source License

private static FullHttpMessage toFullMessage(HttpMessage msg) {
    if (msg instanceof FullHttpMessage) {
        return ((FullHttpMessage) msg).retain();
    }/*from ww  w .j a v  a 2s  .  com*/

    FullHttpMessage fullMsg;
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        fullMsg = new DefaultFullHttpRequest(req.getProtocolVersion(), req.getMethod(), req.getUri(),
                Unpooled.EMPTY_BUFFER, false);
    } else if (msg instanceof HttpResponse) {
        HttpResponse res = (HttpResponse) msg;
        fullMsg = new DefaultFullHttpResponse(res.getProtocolVersion(), res.getStatus(), Unpooled.EMPTY_BUFFER,
                false);
    } else {
        throw new IllegalStateException();
    }

    return fullMsg;
}

From source file:com.mpush.core.handler.HttpProxyHandler.java

License:Apache License

private ByteBuf getBody(HttpRequestMessage message) {
    return message.body == null ? Unpooled.EMPTY_BUFFER : Unpooled.wrappedBuffer(message.body);
}

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//*from w w w. j  a  va  2 s  . c  om*/
static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        //            System.out.println("----------------------------------------------");
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.ning.http.client.providers.netty_4.NettyResponse.java

License:Apache License

public ByteBuf getResponseBodyAsByteBuf() throws IOException {
    ByteBuf b = null;/*w  ww  . j a  v  a2 s.com*/
    switch (bodyParts.size()) {
    case 0:
        b = Unpooled.EMPTY_BUFFER;
        break;
    case 1:
        b = ResponseBodyPart.class.cast(bodyParts.get(0)).getChannelBuffer();
        break;
    default:
        ByteBuf[] channelBuffers = new ByteBuf[bodyParts.size()];
        for (int i = 0; i < bodyParts.size(); i++) {
            channelBuffers[i] = ResponseBodyPart.class.cast(bodyParts.get(i)).getChannelBuffer();
        }
        b = Unpooled.wrappedBuffer(channelBuffers);
    }

    return b;
}

From source file:com.otcdlink.chiron.downend.Http11ProxyHandler.java

License:Apache License

@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    String rhost;// ww  w.j av a2 s. c  o  m
    if (raddr.isUnresolved()) {
        rhost = raddr.getHostString();
    } else {
        rhost = raddr.getAddress().getHostAddress();
    }

    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT,
            rhost + ':' + raddr.getPort(), Unpooled.EMPTY_BUFFER,
            new DefaultHttpHeaders().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE).add(
                    HttpHeaderNames.PROXY_CONNECTION, HttpHeaderValues.KEEP_ALIVE),
            EmptyHttpHeaders.INSTANCE);

    SocketAddress proxyAddress = proxyAddress();
    if (proxyAddress instanceof InetSocketAddress) {
        InetSocketAddress hostAddr = (InetSocketAddress) proxyAddress;
        //            req.headers().set(HttpHeaderNames.HOST, hostAddr.getHostString() + ':' + hostAddr.getPort());
        req.headers().set(HttpHeaderNames.HOST, rhost + ':' + raddr.getPort());
    }

    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }

    return req;
}

From source file:com.phei.netty.nio.http.snoop.HttpSnoopServerHandler.java

License:Apache License

@Override
protected void messageReceived(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;

        if (HttpHeaderUtil.is100ContinueExpected(request)) {
            send100Continue(ctx);//w  w w .j av  a2  s .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.protocolVersion()).append("\r\n");
        buf.append("HOSTNAME: ").append(request.headers().get(HOST, "unknown")).append("\r\n");
        buf.append("REQUEST_URI: ").append(request.uri()).append("\r\n\r\n");

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

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
        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 (CharSequence name : trailer.trailingHeaders().names()) {
                    for (CharSequence 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.rackspacecloud.blueflood.http.HttpResponder.java

License:Apache License

public static void respond(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {

    // set response headers
    if (CORS_ENABLED) {
        res.headers().add("Access-Control-Allow-Origin", CORS_ALLOWED_ORIGINS);
    }//from  w ww . ja  v a 2s.  com

    if (res.content() != null) {
        setContentLength(res, res.content().readableBytes());
    }

    if (isKeepAlive(req)) {
        res.headers().add(CONNECTION, KEEP_ALIVE);
    }

    // Send the response and close the connection if necessary.
    ctx.channel().write(res);
    if (req == null || !isKeepAlive(req)) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.sangupta.swift.netty.proxy.ProxyBackendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext channelHandlerContext) {
    channelHandlerContext.read();
    channelHandlerContext.write(Unpooled.EMPTY_BUFFER);
}

From source file:com.sangupta.swift.netty.proxy.ProxyFrontendHandler.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//*from   w ww . j  a v a  2 s  .c om*/
static void closeOnFlush(Channel channel) {
    if (channel.isActive()) {
        channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}