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.crm.provisioning.thread.osa.CallbackServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx, String actionType) {
    StringBuilder content = new StringBuilder();

    content.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    content.append("<SOAP-ENV:Envelope");
    content.append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"");
    content.append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"");
    content.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    content.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
    content.append(" xmlns:osaxsd=\"http://www.csapi.org/osa/schema\"");
    content.append(" xmlns:osa=\"http://www.csapi.org/osa/wsdl\"");
    content.append(" xmlns:csxsd=\"http://www.csapi.org/cs/schema\"");
    content.append(" xmlns:cs=\"http://www.csapi.org/cs/wsdl\">");
    content.append("<SOAP-ENV:Body");
    content.append(" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
    content.append("<cs:");
    content.append(actionType);// ww w  .j a v  a2s  .c om
    content.append("Response></cs:");
    content.append(actionType);
    content.append("Response>");
    content.append("</SOAP-ENV:Body>");
    content.append("</SOAP-ENV:Envelope>");
    content.append("\r\n");

    // Decide whether to close the connection or not.
    boolean keepAlive = isKeepAlive(request);

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_0,
            currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
            Unpooled.copiedBuffer(content.toString(), CharsetUtil.UTF_8));

    dfGMT.setTimeZone(tz);

    response.headers().set(DATE, dfGMT.format(new Date()));
    response.headers().set(CONTENT_TYPE, "text/htlm");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    response.headers().set(CONNECTION, keepAlive ? HttpHeaders.Values.KEEP_ALIVE : HttpHeaders.Values.CLOSE);

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

    return keepAlive;
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

private static String readString(ByteBuf cb, int length) {
    try {//  w w  w.  j  a  va2  s.  com
        String str = cb.toString(cb.readerIndex(), length, CharsetUtil.UTF_8);
        cb.readerIndex(cb.readerIndex() + length);
        return str;
    } catch (IllegalStateException e) {
        // That's the way netty encapsulate a CCE
        if (e.getCause() instanceof CharacterCodingException)
            throw new DriverInternalError("Cannot decode string as UTF8");
        else
            throw e;
    }
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeString(String str, ByteBuf cb) {
    byte[] bytes = str.getBytes(CharsetUtil.UTF_8);
    cb.writeShort(bytes.length);//from  w  ww . j a v  a 2 s  .c om
    cb.writeBytes(bytes);
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeLongString(String str, ByteBuf cb) {
    byte[] bytes = str.getBytes(CharsetUtil.UTF_8);
    cb.writeInt(bytes.length);//w  ww. j ava  2 s.c o m
    cb.writeBytes(bytes);
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static int sizeOfLongString(String str) {
    return 4 + str.getBytes(CharsetUtil.UTF_8).length;
}

From source file:com.dempe.ketty.srv.http.HttpStaticFileServerHandler.java

License:Apache License

public void sendError(ChannelHandlerContext ctx, HttpResponseStatus status, FullHttpRequest request) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status,
            Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(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.digisky.innerproxy.server.InnerProxyHttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
    LogMgr.debug("channelRead0()", "channelRead0");
    if (msg instanceof HttpRequest) {
        HttpRequest request = this.request = (HttpRequest) msg;
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);//  w ww  . ja v a  2s .c o m
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        LogMgr.debug(InnerProxyHttpServerHandler.class.getName(),
                "content:" + content.toString(CharsetUtil.UTF_8));
        String jsonmsg = null;
        try {
            jsonmsg = java.net.URLDecoder.decode(content.toString(CharsetUtil.UTF_8), "utf-8");
        } catch (UnsupportedEncodingException e) {
            LogMgr.warn("", "URLDecoder exceptionn caught.");
            // e.printStackTrace();
        }
        if (msgCheck(jsonmsg) == false) { //contain ';', bad.
            LogMgr.error(InnerProxyHttpServerHandler.class.getName(),
                    "jsonmsg contain ; or ', close it. peer info:" + ctx.channel().remoteAddress().toString());
            ctx.channel().close();
            return;
        }
        LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "jsonmsg:" + jsonmsg);
        LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "uri:" + request.getUri());
        LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "method:" + request.getMethod());
        String type = request.getUri().split("/")[1];
        if (type.equals("email_activate") == true) { // GET?
            String[] tmp = request.getUri().split("=");
            String acode = tmp[tmp.length - 1];
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "acode:" + acode);
            ProcessServerManager.getInstance().process(ctx.channel(), type, "{\"acode\":\"" + acode + "\"}");
        } else { //POST
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "type:" + type);
            // ugly code
            ProcessServerManager.getInstance().process(ctx.channel(), type, jsonmsg.replace("val=", ""));
        }
    }
}

From source file:com.digisky.innerproxy.testclient.HttpSnoopClientHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    TestTimer.add("channelRead0()::37");
    TestTimer.print();// www . j  a  va 2 s.c  o  m
    System.out.println("http CLient received a package!");
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.getStatus());
        System.err.println("VERSION: " + response.getProtocolVersion());
        System.err.println();

        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();
        }

        if (HttpHeaders.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();
        }
    }

}

From source file:com.digisky.stresstest.HttpSnoopClientHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("http CLient received a package!");
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.getStatus());
        System.err.println("VERSION: " + response.getProtocolVersion());
        System.err.println();//  w w  w. ja v a  2s.c  om

        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();
        }

        if (HttpHeaders.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();
        }
    }

}

From source file:com.dlc.server.DLCHttpServerHandler.java

private void writeResponse(ChannelHandlerContext ctx, String json) {
    buf.setLength(0);//from   www . j a v a  2 s. com
    buf.append(json);
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
            Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, "text/plain");
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
    response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*");

    ctx.write(response).addListener(ChannelFutureListener.CLOSE);

}