Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copiedBuffer.

Prototype

private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) 

Source Link

Usage

From source file:com.rackspacecloud.blueflood.http.DefaultHandler.java

License:Apache License

public static void sendResponse(ChannelHandlerContext channel, FullHttpRequest request, String messageBody,
        HttpResponseStatus status, Map<String, String> headers) {

    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
    if (headers != null && !headers.keySet().isEmpty()) {
        Iterator<String> itr = headers.keySet().iterator();
        while (itr.hasNext()) {
            String headerKey = itr.next();
            response.headers().add(headerKey, headers.get(headerKey));
        }/*from   w w  w .  java  2s.  com*/
    }

    final Timer.Context sendResponseTimerContext = sendResponseTimer.time();
    try {
        if (messageBody != null && !messageBody.isEmpty()) {
            response.content().writeBytes(Unpooled.copiedBuffer(messageBody, Constants.DEFAULT_CHARSET));
        }

        HttpResponder.respond(channel, request, response);
        Tracker.getInstance().trackResponse(request, response);
    } finally {
        sendResponseTimerContext.stop();
    }
}

From source file:com.rackspacecloud.blueflood.outputs.handlers.HttpMultiRollupsQueryHandler.java

License:Apache License

private void sendResponse(ChannelHandlerContext channel, FullHttpRequest request, String messageBody,
        HttpResponseStatus status) {/*from   w ww .j  a  v  a 2s . c  o m*/

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);

    if (messageBody != null && !messageBody.isEmpty()) {
        response.content().writeBytes(Unpooled.copiedBuffer(messageBody, Constants.DEFAULT_CHARSET));
    }

    HttpResponder.respond(channel, request, response);
    Tracker.getInstance().trackResponse(request, response);
}

From source file:com.sangupta.swift.netty.NettyUtils.java

License:Apache License

public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status,
            Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8");

    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.sangupta.swift.netty.NettyUtils.java

License:Apache License

public static void sendListing(ChannelHandlerContext ctx, File dir) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");

    StringBuilder buf = new StringBuilder();
    String dirPath = dir.getPath();

    buf.append("<!DOCTYPE html>\r\n");
    buf.append("<html><head><title>");
    buf.append("Listing of: ");
    buf.append(dirPath);//from   w  w w.  jav a  2s  .c  o m
    buf.append("</title></head><body>\r\n");

    buf.append("<h3>Listing of: ");
    buf.append(dirPath);
    buf.append("</h3>\r\n");

    buf.append("<ul>");
    buf.append("<li><a href=\"../\">..</a></li>\r\n");

    for (File f : dir.listFiles()) {
        if (f.isHidden() || !f.canRead()) {
            continue;
        }

        String name = f.getName();
        if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
            continue;
        }

        buf.append("<li><a href=\"");
        buf.append(name);
        buf.append("\">");
        buf.append(name);
        buf.append("</a></li>\r\n");
    }

    buf.append("</ul></body></html>\r\n");
    ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
    response.content().writeBytes(buffer);
    buffer.release();

    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.sangupta.swift.netty.NettyUtils.java

License:Apache License

public static void sendListing(final ChannelHandlerContext ctx, final File dir, final String spdyRequest) {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");

    if (AssertUtils.isNotEmpty(spdyRequest)) {
        response.headers().set(SPDY_STREAM_ID, spdyRequest);
        response.headers().set(SPDY_STREAM_PRIO, 0);
    }/*  ww w. j  ava2  s.  c o  m*/

    StringBuilder buf = new StringBuilder();
    String dirPath = dir.getPath();

    buf.append("<!DOCTYPE html>\r\n");
    buf.append("<html><head><title>");
    buf.append("Listing of: ");
    buf.append(dirPath);
    buf.append("</title></head><body>\r\n");

    buf.append("<h3>Listing of: ");
    buf.append(dirPath);
    buf.append("</h3>\r\n");

    buf.append("<ul>");
    buf.append("<li><a href=\"../\">..</a></li>\r\n");

    for (File f : dir.listFiles()) {
        if (f.isHidden() || !f.canRead()) {
            continue;
        }

        String name = f.getName();
        if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
            continue;
        }

        buf.append("<li><a href=\"");
        buf.append(name);
        buf.append("\">");
        buf.append(name);
        buf.append("</a></li>\r\n");
    }

    buf.append("</ul></body></html>\r\n");
    ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
    response.content().writeBytes(buffer);
    buffer.release();

    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.shouxun.server.netty.tcp.EchoServerHandler.java

License:Apache License

public void sendMsg(String msg, ChannelHandlerContext ctx) {
    //ByteBuf byteBuf = Unpooled.copiedBuffer(length+msg,CharsetUtil.UTF_8);
    if (null != msg && null != ctx) {
        ByteBuf byteBuf = Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8);
        ctx.writeAndFlush(byteBuf);/*from   w  w w .j  av  a 2 s.co  m*/
    }

}

From source file:com.sohu.jafka.http.HttpServerHandler.java

License:Apache License

private boolean writeResponse(ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
            Unpooled.copiedBuffer("OK", CharsetUtil.UTF_8));

    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // 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(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }/*from w w w. j  a va 2s  . co  m*/

    // Write the response.
    ctx.write(response);

    return keepAlive;
}

From source file:com.sohu.jafka.http.HttpServerHandler.java

License:Apache License

private static void sendStatusMessage(ChannelHandlerContext ctx, HttpResponseStatus status, String msg) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status,
            Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    ctx.writeAndFlush(response);/*from w ww  .  j a  va2 s.  co  m*/
    ctx.close();
}

From source file:com.spotify.netty.handler.codec.zmtp.ProtocolViolationTests.java

License:Apache License

private void testConnect(final int payloadSize) throws InterruptedException {
    final StringBuilder payload = new StringBuilder();
    for (int i = 0; i < payloadSize; i++) {
        payload.append('0');
    }//from   ww  w .j  a v  a 2 s .c o  m

    serverChannel.writeInbound(Unpooled.copiedBuffer(payload, Charsets.UTF_8));

    // TODO- verify it's ok to remove this
    //    verify(mockHandler, never())
    //        .channelActive(any(ChannelHandlerContext.class));
    Assert.assertNull(serverChannel.readInbound());
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessageDecoderTest.java

License:Apache License

@Test
public void testSingleFrame() throws Exception {
    final ZMTPMessageDecoder decoder = new ZMTPMessageDecoder();

    final ByteBuf content = Unpooled.copiedBuffer("hello", UTF_8);

    final List<Object> out = Lists.newArrayList();
    decoder.header(ctx, content.readableBytes(), false, out);
    decoder.content(ctx, content, out);//w ww . j  a  v  a 2s .  c o  m
    decoder.finish(ctx, out);

    final Object expected = ZMTPMessage.fromUTF8(ALLOC, "hello");
    assertThat(out, hasSize(1));
    assertThat(out, contains(expected));
}