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.opendaylight.protocol.rsvp.parser.spi.subobjects.CUISubobjectParserTest.java

License:Open Source License

@Test(expected = IllegalArgumentException.class)
public void testException1() {
    this.parser.serializeUnnumeredInterface(this.unnumbered1, Unpooled.EMPTY_BUFFER);
}

From source file:org.pidome.server.system.network.http.HttpClientHandler.java

License:Apache License

/**
 * Writes the response to the output//from  w ww  .ja  v  a  2s.  co  m
 * @param ctx The channel context
 * @param status The response status
 * @param buf The buffer containing the data to send.
 * @param fileType The file type.
 * @param streamId The Stream id (only used in http2)
 * @param cache (if cache headers should be send).
 */
@Override
public final void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, byte[] buf,
        String fileType, String streamId, boolean cache) {

    ByteBuf content = ctx.alloc().buffer(buf.length);
    content.writeBytes(buf);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);

    HttpUtil.setContentLength(response, response.content().readableBytes());

    // In case of SPDY protocol used.
    if (spdyId != null) {
        response.headers().set(SPDY_STREAM_ID, spdyId);
        response.headers().set(SPDY_STREAM_PRIO, 0);
        response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 SPDY");
    } else {
        response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 HTTP1.1");
    }

    response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpRequestHandler.getContentTypeHeader(fileType));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN,
            "http" + ((ssl == true) ? "s" : "") + "://" + plainIp + ((port != 80) ? ":" + port : ""));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");

    if (cache == true) {
        DateTime dt = new DateTime();
        HttpHeaderDateFormat dateFormat = HttpHeaderDateFormat.get();
        response.headers().set(HttpHeaderNames.CACHE_CONTROL, "public, max-age=3153600");
        response.headers().set(HttpHeaderNames.EXPIRES, dateFormat.format(dt.plusMonths(12).toDate()));
    } else {
        response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, must-revalidate");
        response.headers().set(HttpHeaderNames.EXPIRES, "Sat, 26 Jul 1997 05:00:00 GMT");
    }

    if (keepAlive) {
        // 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(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        ctx.write(response);
    } else {
        // If keep-alive is off, close the connection once the content is fully written.
        ctx.write(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:org.restexpress.serialization.json.GsonJsonProcessorTest.java

License:Apache License

@Test
public void shouldDeserializeEmptyChannelBuffer() {
    ByteBuf buf = Unpooled.EMPTY_BUFFER;
    Object o = processor.deserialize(buf, KnownObject.class);
    assertNull(o);
}

From source file:org.restlet.engine.netty.NettyServerCall.java

License:Open Source License

protected void writeResponseTail(Response response) {
    if (!isKeepAlive()) {
        // Close the connection once the content is fully written.
        getNettyChannel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }//  w w  w  . j  ava 2 s . c  om
}

From source file:org.restlet.ext.netty.internal.NettyServerCall.java

License:Open Source License

@Override
protected void writeResponseTail(Response response) {
    if (!isKeepAlive()) {
        // Close the connection once the content is fully written.
        getNettyContext().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }/*from w ww.j  a  v a2 s. co  m*/
}

From source file:org.restnext.server.ServerHandler.java

License:Apache License

private void write(ChannelHandlerContext ctx, Response response, boolean keepAlive) {
    HttpVersion version = fromVersion(response.getVersion());
    HttpResponseStatus status = fromStatus(response.getStatus());
    ByteBuf content = response.hasContent() ? Unpooled.wrappedBuffer(response.getContent())
            : Unpooled.EMPTY_BUFFER;

    boolean chunked = response.isChunked();

    // create netty response
    HttpResponse resp;/*from  w  ww. ja v a  2 s  .com*/
    HttpChunkedInput chunkedResp = chunked
            ? new HttpChunkedInput(new ChunkedStream(new ByteBufInputStream(content), response.getChunkSize()))
            : null;

    if (chunked) {
        resp = new DefaultHttpResponse(version, status);
        HttpUtil.setTransferEncodingChunked(resp, true);
        createOutboutHeaders(resp, response, keepAlive);
        // Write the initial line and the header.
        ctx.write(resp);
    } else {
        resp = new DefaultFullHttpResponse(version, status, content);
        createOutboutHeaders(resp, response, keepAlive);
    }

    if (keepAlive) {
        if (chunked) {
            ctx.write(chunkedResp);
        } else {
            HttpUtil.setContentLength(resp, content.readableBytes());
            ctx.write(resp);
        }
    } else {
        ChannelFuture channelFuture;
        if (chunked) {
            channelFuture = ctx.writeAndFlush(chunkedResp);
        } else {
            channelFuture = ctx.writeAndFlush(resp);
        }
        // Close the connection after the write operation is done if necessary.
        channelFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:org.royaldev.enterprise.proxy.ProxyFrontendHandler.java

License:Apache License

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

From source file:org.spout.vanilla.protocol.codec.server.ServerListPingCodec.java

License:Open Source License

@Override
public ByteBuf encode(ServerListPingMessage message) throws IOException {
    return Unpooled.EMPTY_BUFFER;
}

From source file:org.springframework.boot.context.embedded.netty.HttpResponseOutputStream.java

License:Apache License

private void flushBuffer(boolean lastContent) {
    if (count > 0) {
        ByteBuf content = ctx.alloc().buffer(count);
        content.writeBytes(buf, 0, count);
        count = 0;//from w  ww  .j a  v  a 2s.  com
        writeContent(content, lastContent);
    } else if (lastContent) {
        writeContent(Unpooled.EMPTY_BUFFER, true);
    }
}

From source file:org.springframework.cloud.gateway.rsocket.core.GatewayRSocketTests.java

License:Apache License

@Before
public void init() {
    registry = mock(Registry.class);
    incomingPayload = DefaultPayload.create(Unpooled.EMPTY_BUFFER,
            Metadata.from("mock").with("id", "mock1").encode());

    RSocket rSocket = mock(RSocket.class);
    LoadBalancedRSocket loadBalancedRSocket = mock(LoadBalancedRSocket.class);
    when(registry.getRegistered(any(Metadata.class))).thenReturn(loadBalancedRSocket);

    Mono<EnrichedRSocket> mono = Mono.just(new EnrichedRSocket(rSocket, getMetadata()));
    when(loadBalancedRSocket.choose()).thenReturn(mono);

    when(rSocket.requestResponse(any(Payload.class))).thenReturn(Mono.just(DefaultPayload.create("response")));
}