Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

From source file:com.github.mrstampy.kitchensync.netty.channel.payload.KiSyMessageByteBufCreator.java

License:Open Source License

@Override
public <MSG> ByteBuf createByteBuf(MSG message, InetSocketAddress recipient) {
    try {//from  www  .  j  ava2  s.  c o m
        String s = mapper.writeValueAsString(message);
        return Unpooled.copiedBuffer(s, CharsetUtil.UTF_8);
    } catch (JsonProcessingException e) {
        log.error("Could not return KiSyMessage Json string for {}", message, e);
    }

    return null;
}

From source file:com.github.mrstampy.kitchensync.netty.channel.payload.StringByteBufCreator.java

License:Open Source License

@Override
public <MSG> ByteBuf createByteBuf(MSG message, InetSocketAddress recipient) {
    return Unpooled.copiedBuffer((String) message, CharsetUtil.UTF_8);
}

From source file:com.github.mrstampy.kitchensync.netty.handler.AbstractKiSyNettyHandler.java

License:Open Source License

/**
 * Content.//from  w w w  .  jav a2 s  .c  o m
 *
 * @param msg
 *          the msg
 * @return the string
 */
protected String content(DatagramPacket msg) {
    return msg.content().toString(CharsetUtil.UTF_8);
}

From source file:com.github.picto.network.exemple.HttpSnoopClientHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();//  w  w w .  j a  va 2 s.  c  o  m

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpHeaderUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
            shouldEnd.set(true);
        }
    }
}

From source file:com.github.rmannibucau.featuredmock.http.FeaturedHandler.java

License:Apache License

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

From source file:com.google.devtools.build.remote.worker.HttpCacheServerHandler.java

License:Open Source License

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

    if (!HttpUtil.isKeepAlive(request)) {
        future.addListener(ChannelFutureListener.CLOSE);
    }//from   w w w . ja  va2 s .c o  m
}

From source file:com.gw.services.client.HttpsClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;/* ww w.j a v a 2 s  .  c o m*/
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SSLContext sslCtx;
    if (ssl) {
        sslCtx = CLIENT_CONTEXT;
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpsClientInitializer(sslCtx));

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        String requestBody = "{\"productId\": 11,\"userName\": \"caiwei\",\"amount\": 1000}";
        ByteBuf content = Unpooled.copiedBuffer(requestBody.getBytes(CharsetUtil.UTF_8));
        // Prepare the HTTP request.
        HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
                uri.getRawPath(), content);
        request.headers().set(HttpHeaders.Names.HOST, host);
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
        request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
        // Set some example cookies.
        request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder
                .encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));
        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
    }
}

From source file:com.gw.services.client.HttpsClientHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.getStatus());
        System.err.println("VERSION: " + response.getProtocolVersion());
        System.err.println();/*from   w ww. j  a va2 s.  c  o m*/

        if (!response.headers().isEmpty()) {
            for (String name : response.headers().names()) {
                for (String value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }
        System.err.println("CONTENT {");
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}

From source file:com.hazelcast.simulator.protocol.handler.SimulatorProtocolDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (EMPTY_BUFFER.equals(buffer)) {
        return;/*from   w  w  w.ja va 2 s.  co m*/
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("SimulatorProtocolDecoder.decode() %s %s", addressLevel, localAddress));
    }

    if (isSimulatorMessage(buffer)) {
        decodeSimulatorMessage(ctx, buffer, out);
        return;
    }
    if (isResponse(buffer)) {
        decodeResponse(ctx, buffer, out);
        return;
    }

    String msg = format("%s %s Invalid magic bytes %s", addressLevel, localAddress,
            buffer.toString(CharsetUtil.UTF_8));
    LOGGER.error(msg);
    throw new IllegalArgumentException(msg);
}

From source file:com.heliosapm.webrpc.websocket.WebSocketServiceHandler.java

License:Apache License

private static void sendHttpResponse(final ChannelHandlerContext ctx, final FullHttpRequest req,
        final FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.status().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//from w ww . j a  v  a  2  s.  c o  m
        buf.release();
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}