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:org.waarp.openr66.protocol.utils.FileUtils.java

License:Open Source License

/**
 * /*  ww  w . j a v a 2 s  .  c  o m*/
 * @param buffer
 * @return the hash from the given Buffer
 */
public final static ByteBuf getHash(ByteBuf buffer, DigestAlgo algo) {
    byte[] newkey;
    try {
        newkey = FilesystemBasedDigest.getHash(buffer, algo);
    } catch (IOException e) {
        return Unpooled.EMPTY_BUFFER;
    }
    return Unpooled.wrappedBuffer(newkey);
}

From source file:org.wso2.carbon.mss.internal.router.InternalHttpResponder.java

License:Open Source License

@Override
public ChunkResponder sendChunkStart(HttpResponseStatus status, @Nullable Multimap<String, String> headers) {
    statusCode = status.code();/*from  www.  ja v  a 2s .com*/
    return new ChunkResponder() {

        private ByteBuf contentChunks = Unpooled.EMPTY_BUFFER;
        private boolean closed;

        @Override
        public void sendChunk(ByteBuffer chunk) throws IOException {
            sendChunk(Unpooled.wrappedBuffer(chunk));
        }

        @Override
        public synchronized void sendChunk(ByteBuf chunk) throws IOException {
            if (closed) {
                throw new IOException("ChunkResponder already closed.");
            }
            contentChunks = Unpooled.wrappedBuffer(contentChunks, chunk);
        }

        @Override
        public synchronized void close() throws IOException {
            if (closed) {
                return;
            }
            closed = true;
            inputSupplier = createContentSupplier(contentChunks);
        }
    };
}

From source file:org.wso2.carbon.mss.internal.router.InternalHttpResponder.java

License:Open Source License

@Override
public void sendContent(HttpResponseStatus status, @Nullable ByteBuf content, String contentType,
        @Nullable Multimap<String, String> headers) {
    statusCode = status.code();//from w w  w. ja  v  a  2 s . c o m
    inputSupplier = createContentSupplier(content == null ? Unpooled.EMPTY_BUFFER : content);
}

From source file:org.wso2.carbon.transport.http.netty.listener.http2.HTTP2ResponseCallback.java

License:Open Source License

public void done(CarbonMessage cMsg) {
    handleResponsesWithoutContentLength(cMsg);
    if (HTTPTransportContextHolder.getInstance().getHandlerExecutor() != null) {
        HTTPTransportContextHolder.getInstance().getHandlerExecutor().executeAtSourceResponseReceiving(cMsg);
    }//  w  ww  .j  a  v  a  2 s  .com
    Http2Headers http2Headers = createHttp2Headers(cMsg);

    if (ctx.handler() instanceof HTTP2SourceHandler) {
        HTTP2SourceHandler http2SourceHandler = (HTTP2SourceHandler) ctx.handler();
        http2SourceHandler.encoder().writeHeaders(ctx, streamId, http2Headers, 0, false, ctx.newPromise());
        try {
            if (cMsg instanceof HTTPCarbonMessage) {
                HTTPCarbonMessage nettyCMsg = (HTTPCarbonMessage) cMsg;
                while (true) {
                    if (nettyCMsg.isEndOfMsgAdded() && nettyCMsg.isEmpty()) {
                        http2SourceHandler.encoder().writeData(ctx, streamId, Unpooled.EMPTY_BUFFER, 0, true,
                                ctx.newPromise());
                        http2SourceHandler.flush(ctx);
                        break;
                    }
                    HttpContent httpContent = nettyCMsg.getHttpContent();
                    if (httpContent instanceof LastHttpContent) {
                        http2SourceHandler.encoder().writeData(ctx, streamId, httpContent.content(), 0, true,
                                ctx.newPromise());
                        http2SourceHandler.flush(ctx);
                        if (HTTPTransportContextHolder.getInstance().getHandlerExecutor() != null) {
                            HTTPTransportContextHolder.getInstance().getHandlerExecutor()
                                    .executeAtSourceResponseSending(cMsg);
                        }
                        break;
                    }
                    http2SourceHandler.encoder().writeData(ctx, streamId, httpContent.content(), 0, false,
                            ctx.newPromise());
                }
            } else if (cMsg instanceof DefaultCarbonMessage) {
                DefaultCarbonMessage defaultCMsg = (DefaultCarbonMessage) cMsg;
                if (defaultCMsg.isEndOfMsgAdded() && defaultCMsg.isEmpty()) {
                    http2SourceHandler.encoder().writeData(ctx, streamId, Unpooled.EMPTY_BUFFER, 0, true,
                            ctx.newPromise());
                    http2SourceHandler.flush(ctx);
                    return;
                }
                while (true) {
                    ByteBuffer byteBuffer = defaultCMsg.getMessageBody();
                    ByteBuf bbuf = Unpooled.wrappedBuffer(byteBuffer);
                    http2SourceHandler.encoder().writeData(ctx, streamId, bbuf, 0, false, ctx.newPromise());
                    if (defaultCMsg.isEndOfMsgAdded() && defaultCMsg.isEmpty()) {
                        ChannelFuture future = http2SourceHandler.encoder().writeData(ctx, streamId,
                                Unpooled.EMPTY_BUFFER, 0, true, ctx.newPromise());
                        http2SourceHandler.flush(ctx);
                        if (HTTPTransportContextHolder.getInstance().getHandlerExecutor() != null) {
                            HTTPTransportContextHolder.getInstance().getHandlerExecutor()
                                    .executeAtSourceResponseSending(cMsg);
                        }
                        String connection = cMsg.getHeader(Constants.HTTP_CONNECTION);
                        if (connection != null
                                && Constants.HTTP_CONNECTION_CLOSE.equalsIgnoreCase(connection)) {
                            future.addListener(ChannelFutureListener.CLOSE);
                        }
                        break;
                    }
                }
            } else {

            }
        } catch (Http2Exception e) {
            logger.error("Error occurred while sending response to client", e);
        }
    }
}

From source file:org.wso2.carbon.transport.http.netty.listener.http2.HTTP2SourceHandler.java

License:Open Source License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (ctx != null && ctx.channel().isActive()) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }//www  .j  a  v a2  s .c o m
}

From source file:org.wso2.carbon.transport.http.netty.listener.SourceHandler.java

License:Open Source License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (ctx != null && ctx.channel().isActive()) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }/*from   w  w w.j a  va 2 s.  co m*/
    serverConnectorFuture.notifyErrorListener(cause);
}

From source file:org.wso2.carbon.transport.http.netty.redirect.HTTPClientRedirectTestCase.java

License:Open Source License

/**
 * Check whether, redirect request is written to the backend when a redirect response is received.
 *
 * @throws URISyntaxException//from w ww  .  j a  va2 s .com
 * @throws IOException
 */
@Test
public void unitTestForRedirectHandler() throws URISyntaxException, IOException {
    EmbeddedChannel embeddedChannel = new EmbeddedChannel();
    embeddedChannel.pipeline().addLast(new HttpResponseDecoder());
    embeddedChannel.pipeline().addLast(new HttpRequestEncoder());
    embeddedChannel.pipeline().addLast(new RedirectHandler(null, false, 5, false));
    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.TEMPORARY_REDIRECT, Unpooled.EMPTY_BUFFER);
    response.headers().set(HttpHeaders.Names.LOCATION, FINAL_DESTINATION);
    embeddedChannel.attr(Constants.ORIGINAL_REQUEST)
            .set(createHttpRequest(Constants.HTTP_GET_METHOD, FINAL_DESTINATION));
    embeddedChannel.writeInbound(response);
    embeddedChannel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    assertNotNull(embeddedChannel.readOutbound());
}

From source file:org.wso2.carbon.transport.http.netty.redirect.HTTPClientRedirectTestCase.java

License:Open Source License

/**
 * When the maximum redirect count reached, channel should not do any more redirects.
 *
 * @throws URISyntaxException//from w w w  .jav  a 2s. co  m
 * @throws IOException
 */
@Test
public void unitTestForRedirectLoop() throws URISyntaxException, IOException {
    EmbeddedChannel embeddedChannel = new EmbeddedChannel();
    embeddedChannel.pipeline().addLast(new HttpResponseDecoder());
    embeddedChannel.pipeline().addLast(new HttpRequestEncoder());
    embeddedChannel.pipeline().addLast(Constants.IDLE_STATE_HANDLER,
            new IdleStateHandler(50000, 50000, 0, TimeUnit.MILLISECONDS));
    embeddedChannel.pipeline().addLast(new RedirectHandler(null, false, 5, false, null, false));
    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.TEMPORARY_REDIRECT, Unpooled.EMPTY_BUFFER);
    response.headers().set(HttpHeaders.Names.LOCATION, FINAL_DESTINATION);
    embeddedChannel.attr(Constants.ORIGINAL_REQUEST)
            .set(createHttpRequest(Constants.HTTP_GET_METHOD, FINAL_DESTINATION));
    embeddedChannel.attr(Constants.RESPONSE_FUTURE_OF_ORIGINAL_CHANNEL).set(new HttpResponseFutureImpl());
    TargetChannel targetChannel = new TargetChannel(null, null);
    targetChannel.setChannel(embeddedChannel);
    embeddedChannel.attr(Constants.TARGET_CHANNEL_REFERENCE).set(targetChannel);
    embeddedChannel.attr(Constants.REDIRECT_COUNT).set(5);
    embeddedChannel.writeInbound(response);
    embeddedChannel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
    assertNull(embeddedChannel.readOutbound());
}

From source file:org.wso2.gw.emulator.http.consumer.HttpResponseProcessor.java

License:Open Source License

public void process(HttpRequestContext requestContext, ChannelHandlerContext ctx) {
    OutgoingMessage outgoing = getMatchResource(requestContext);
    if (outgoing == null) {
        if (!write404NotFoundResponse(requestContext, ctx)) {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }/* w w  w. j a  v  a  2  s . co  m*/
    } else {
        if (!writeResponse(requestContext, outgoing, ctx)) {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }
    }
}

From source file:org.wyb.sows.server.HexDumpProxyBackendHandler.java

License:Apache License

static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}