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.QueryStringDecoder.java

License:Apache License

/**
 * Creates a new decoder that decodes the specified URI. The decoder will
 * assume that the query string is encoded in UTF-8.
 *//* w ww .  jav  a2  s. c om*/
public QueryStringDecoder(String uri) {
    this(uri, CharsetUtil.UTF_8);
}

From source file:com.QueryStringDecoder.java

License:Apache License

/**
 * Creates a new decoder that decodes the specified URI encoded in the
 * specified charset./*  w  ww . j  a  v  a 2  s.  co  m*/
 */
public QueryStringDecoder(String uri, boolean hasPath) {
    this(uri, CharsetUtil.UTF_8, hasPath);
}

From source file:com.QueryStringDecoder.java

License:Apache License

/**
 * Creates a new decoder that decodes the specified URI. The decoder will
 * assume that the query string is encoded in UTF-8.
 *//*from w w w.j  ava  2s  .c o m*/
public QueryStringDecoder(URI uri) {
    this(uri, CharsetUtil.UTF_8);
}

From source file:com.QueryStringDecoder.java

License:Apache License

/**
 * Decodes a bit of an URL encoded by a browser.
 * <p>/*from  w w w  . j  a  v  a  2 s . com*/
 * This is equivalent to calling {@link #decodeComponent(String, Charset)}
 * with the UTF-8 charset (recommended to comply with RFC 3986, Section 2).
 * 
 * @param s
 *            The string to decode (can be empty).
 * @return The decoded string, or {@code s} if there's nothing to decode. If
 *         the string to decode is {@code null}, returns an empty string.
 * @throws IllegalArgumentException
 *             if the string contains a malformed escape sequence.
 */
public static String decodeComponent(final String s) {
    return decodeComponent(s, CharsetUtil.UTF_8);
}

From source file:com.rackspacecloud.blueflood.inputs.handlers.HttpEventsIngestionHandler.java

License:Apache License

@Override
public void handle(ChannelHandlerContext ctx, FullHttpRequest request) {

    final String tenantId = request.headers().get(Event.FieldLabels.tenantId.name());
    String response = "";
    ObjectMapper objectMapper = new ObjectMapper();
    final Timer.Context httpEventsIngestTimerContext = httpEventsIngestTimer.time();
    try {/*from w  w w .  jav a2  s.c om*/
        String body = request.content().toString(0, request.content().writerIndex(), CharsetUtil.UTF_8);
        Event event = objectMapper.readValue(body, Event.class);

        // To verify if the request has multiple elements. If some one sends request with multiple root elements,
        // parser does not throw any validation exceptions. We are checking it manually here.
        Iterator<Event> iterator = objectMapper.reader(Event.class).readValues(body);
        if (iterator.hasNext()) {
            iterator.next();
            if (iterator.hasNext()) { //has more than one element
                throw new InvalidDataException("Only one event is allowed per request");
            }
        }

        Set<ConstraintViolation<Event>> constraintViolations = validator.validate(event);

        List<ErrorResponse.ErrorData> validationErrors = new ArrayList<ErrorResponse.ErrorData>();
        for (ConstraintViolation<Event> constraintViolation : constraintViolations) {
            validationErrors.add(
                    new ErrorResponse.ErrorData(tenantId, "", constraintViolation.getPropertyPath().toString(),
                            constraintViolation.getMessage(), event.getWhen()));
        }

        if (!validationErrors.isEmpty()) {
            DefaultHandler.sendErrorResponse(ctx, request, validationErrors, HttpResponseStatus.BAD_REQUEST);
            return;
        }

        searchIO.insert(tenantId, Arrays.asList(event.toMap()));
        DefaultHandler.sendResponse(ctx, request, response, HttpResponseStatus.OK);
    } catch (JsonMappingException e) {
        log.debug(String.format("Exception %s", e.toString()));
        response = String.format("Invalid Data: %s", e.getMessage());
        DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST);
    } catch (JsonParseException e) {
        log.debug(String.format("Exception %s", e.toString()));
        response = String.format("Invalid Data: %s", e.getMessage());
        DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST);
    } catch (InvalidDataException e) {
        log.debug(String.format("Exception %s", e.toString()));
        response = String.format("Invalid Data: %s", e.getMessage());
        DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.BAD_REQUEST);
    } catch (Exception e) {
        log.error(String.format("Exception %s", e.toString()));
        response = String.format("Error: %s", e.getMessage());
        DefaultHandler.sendErrorResponse(ctx, request, response, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    } finally {
        httpEventsIngestTimerContext.stop();
    }
}

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 ww  .  ja  v  a  2  s . co  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);
    }/*from  w  w w .j a v  a2s  .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   ww w  . ja v  a2s  . c  o  m
    }

}

From source file:com.sixmac.imspeak.app.WebSocketClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();// w w  w. j a va 2 s  .c o  m
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        System.out.println("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus().code()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        System.out.println("WebSocket Client received message: " + textFrame.text());
    } else if (frame instanceof PongWebSocketFrame) {
        System.out.println("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        System.out.println("WebSocket Client received closing");
        ch.close();
    }
}