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.z.core.test.HexDumpProxyBackendHandler.java

License:Apache License

public void channelActive(ChannelHandlerContext ctx) {
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);
}

From source file:ozy.server.ServerHandler.java

License:Open Source License

private void routeRequest(ChannelHandlerContext ctx) {
    if (router() == null) {
        sendErrorMessage(ctx, INTERNAL_SERVER_ERROR, new NullPointerException("router"));
    } else if (!router().isRouterFor(_serviceRequest)) {
        sendErrorMessage(ctx, BAD_REQUEST, "[ServerHandler] invalid request");
    } else {//from  w  w w . java  2  s .co  m
        try {
            _serverResponseFr = router().route(_serviceRequest);
            _serverResponseFr.onComplete((responseTry) -> {
                if (responseTry.isSuccess()) {
                    try {
                        sendResponse(ctx, responseTry.success());
                    } catch (Exception exc) {
                        sendErrorMessage(ctx, INTERNAL_SERVER_ERROR, exc);
                    }
                    if (!HttpHeaders.isKeepAlive(_httpRequest)) {
                        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
                    }
                } else {
                    sendErrorMessage(ctx, INTERNAL_SERVER_ERROR, responseTry.failure().toDebugString());
                }
            });
        } catch (Exception exc) {
            sendErrorMessage(ctx, INTERNAL_SERVER_ERROR, exc);
        }
    }
}

From source file:ratpack.rocker.internal.DefaultRockerRenderer.java

License:Apache License

@Override
public void render(Context context, RockerModel rockerModel) throws Exception {
    try {/* www  . j a v a 2s  .c om*/
        ArrayOfByteArraysOutput output = rockerModel.render(ArrayOfByteArraysOutput.FACTORY);

        List<byte[]> arrays = output.getArrays();
        ByteBuf byteBuf;
        int size = arrays.size();
        if (size == 0) {
            byteBuf = Unpooled.EMPTY_BUFFER;
        } else if (size == 1) {
            byteBuf = Unpooled.wrappedBuffer(arrays.get(0));
        } else {
            byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, size,
                    Iterables.transform(arrays, Unpooled::wrappedBuffer));
        }

        AsciiString contentType = output.getContentType() == ContentType.HTML ? HTML : TEXT;
        context.getResponse().contentTypeIfNotSet(contentType).send(byteBuf);

    } catch (Exception e) {
        // https://github.com/fizzed/rocker/issues/30
        // Ratpack will try to toString() the rockerModel object, to create an exception message for the RenderException
        // This will obscure the underlying exception. Log here so we can actually see it.
        // This can be removed when the above issue is rectified.
        throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e);
    }
}

From source file:ratpack.server.internal.RequestBody.java

License:Apache License

private void complete(Downstream<? super ByteBuf> downstream) {
    state = State.READ;// www . j  a va  2  s .c  o  m
    if (received.isEmpty()) {
        downstream.success(Unpooled.EMPTY_BUFFER);
    } else {
        downstream.success(composeReceived());
    }
}

From source file:ratpack.server.internal.RequestBody.java

License:Apache License

private ByteBuf composeReceived() {
    if (received.isEmpty()) {
        return Unpooled.EMPTY_BUFFER;
    } else if (received.size() == 1) {
        return new ByteBufRef(received.remove(0));
    } else {/*from w  w w .j  a  v  a  2  s.c  o m*/
        ByteBuf[] byteBufsArray = this.received.toArray(new ByteBuf[this.received.size()]);
        received.clear();
        return Unpooled.unmodifiableBuffer(byteBufsArray);
    }
}

From source file:ratpack.session.clientside.internal.ClientSideSessionStore.java

License:Apache License

@Override
public Promise<ByteBuf> load(AsciiString sessionId) {
    return Promise.sync(() -> {
        CookieStorage cookieStorage = getCookieStorage();
        if (!isValid(cookieStorage)) {
            return Unpooled.EMPTY_BUFFER;
        }/*from w  ww . j a  va2s  .c  o  m*/
        setLastAccessTime(cookieStorage);
        return deserialize(cookieStorage.data);
    });
}

From source file:ratpack.session.clientside.internal.ClientSideSessionStore.java

License:Apache License

private ByteBuf deserialize(ImmutableList<Cookie> sessionCookies) throws Exception {
    if (sessionCookies.isEmpty()) {
        return Unpooled.EMPTY_BUFFER;
    }/*from  ww  w  .jav a 2s .  c  om*/

    StringBuilder sessionCookie = new StringBuilder();
    for (Cookie cookie : sessionCookies) {
        sessionCookie.append(cookie.value());
    }
    String[] parts = sessionCookie.toString().split(SESSION_SEPARATOR);
    if (parts.length != 2) {
        return Unpooled.buffer(0, 0);
    }
    ByteBuf payload = null;
    ByteBuf digest = null;
    ByteBuf expectedDigest = null;
    ByteBuf decryptedPayload = null;
    try {
        payload = fromBase64(bufferAllocator, parts[0]);
        digest = fromBase64(bufferAllocator, parts[1]);
        expectedDigest = signer.sign(payload, bufferAllocator);
        if (ByteBufUtil.equals(digest, expectedDigest)) {
            decryptedPayload = crypto.decrypt(payload.resetReaderIndex(), bufferAllocator);
        } else {
            decryptedPayload = Unpooled.buffer(0, 0);
        }
    } finally {
        if (payload != null) {
            payload.touch().release();
        }
        if (digest != null) {
            digest.release();
        }
        if (expectedDigest != null) {
            expectedDigest.release();
        }
    }
    return decryptedPayload.touch();
}

From source file:ratpack.session.internal.LocalMemorySessionStore.java

License:Apache License

@Override
public Promise<ByteBuf> load(AsciiString sessionId) {
    return Promise.sync(() -> {
        maybeCleanup();/*from   www  .j  av  a 2 s  . c  o m*/
        ByteBuf value = cache.getIfPresent(sessionId);
        if (value != null) {
            return Unpooled.unreleasableBuffer(value.slice());
        } else {
            return Unpooled.EMPTY_BUFFER;
        }
    });
}

From source file:ratpack.session.store.internal.RedisSessionStore.java

License:Apache License

@Override
public Promise<ByteBuf> load(AsciiString sessionId) {
    return Promise.<ByteBuf>async(downstream -> {
        downstream.accept(connection.get(sessionId));
    }).map(byteBuf -> {/* ww  w  . java 2s .c  o  m*/
        if (byteBuf == null) {
            //Must return an empty buffer never null
            return Unpooled.EMPTY_BUFFER;
        }
        return byteBuf;
    });
}

From source file:reactor.io.net.impl.netty.http.NettyHttpClientHandler.java

License:Open Source License

@Override
protected void doOnTerminate(ChannelHandlerContext ctx, ChannelFuture last, final ChannelPromise promise) {
    if (request.method() == Method.WS) {
        return;/*  w  w  w  .  j ava 2  s  . c  om*/
    }

    ByteBuffer byteBuffer = body.flip().byteBuffer();

    if (request.checkHeader()) {
        HttpRequest req = new DefaultFullHttpRequest(request.getNettyRequest().getProtocolVersion(),
                request.getNettyRequest().getMethod(), request.getNettyRequest().getUri(),
                byteBuffer != null ? Unpooled.wrappedBuffer(byteBuffer) : Unpooled.EMPTY_BUFFER);

        req.headers().add(request.headers().delegate());

        if (byteBuffer != null) {
            HttpHeaders.setContentLength(req, body.limit());
        }

        ctx.writeAndFlush(req).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    promise.trySuccess();
                } else {
                    promise.tryFailure(future.cause());
                }
            }
        });
    } else {
        ctx.write(new DefaultHttpContent(
                byteBuffer != null ? Unpooled.wrappedBuffer(byteBuffer) : Unpooled.EMPTY_BUFFER));
    }
    body.reset();
}