Example usage for io.netty.util CharsetUtil ISO_8859_1

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

Introduction

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

Prototype

Charset ISO_8859_1

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

Click Source Link

Document

ISO Latin Alphabet No.

Usage

From source file:com.andrewkroh.cisco.xmlservices.HttpTestServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest httpRequest = (FullHttpRequest) msg;

        String content = httpRequest.content().toString(CharsetUtil.ISO_8859_1);
        content = content.replaceAll("XML=", "");
        content = URLDecoder.decode(content, CharsetUtil.ISO_8859_1.name());

        // Store the received request:
        httpRequestFuture.set(content);/*w ww  . ja v  a  2  s  . c o  m*/

        // Write the given response:
        HttpResponse response = httpResponseRef.get();
        if (response != null) {
            ctx.writeAndFlush(response).sync();
        }
    }
}

From source file:com.andrewkroh.cisco.xmlservices.XmlResponseChannelHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    IpPhone phone = ctx.channel().attr(phoneAttributeKey).get();

    if (msg instanceof HttpResponse && LOGGER.isDebugEnabled()) {
        HttpResponse response = (HttpResponse) msg;

        StringBuilder responseInfo = new StringBuilder();
        responseInfo.append("Source=");
        responseInfo.append(phone.getHostname());
        responseInfo.append(", ");
        responseInfo.append("Status=");
        responseInfo.append(response.getStatus());
        responseInfo.append(", ");
        responseInfo.append("Version=");
        responseInfo.append(response.getProtocolVersion());

        for (String name : response.headers().names()) {
            for (String value : response.headers().getAll(name)) {
                responseInfo.append(", ");
                responseInfo.append(name);
                responseInfo.append('=');
                responseInfo.append(value);
            }/*from  w  w  w  . j a v  a  2s.c om*/
        }

        LOGGER.debug(responseInfo.toString());
    }

    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        SettableFuture<XmlPushResponse> responseFuture = ctx.channel().attr(responseAttributeKey).get();

        // The default charset for HTTP is ISO-8859-1. None
        // of the Cisco phones I've seen to date were actually
        // setting the charset so use the default. We could
        // improve here by checking the header for the value.
        String xmlContent = content.content().toString(CharsetUtil.ISO_8859_1);

        CiscoIPPhoneResponse xmlResponse = XmlMarshaller.unmarshal(xmlContent, CiscoIPPhoneResponse.class);
        responseFuture.set(new DefaultXmlPushResponse(phone, xmlResponse));

        // Cleanup:
        ctx.close();
        ctx.channel().close();
    }
}

From source file:com.barchart.http.server.PooledServerRequest.java

License:BSD License

@Override
public Charset getCharacterEncoding() {

    final String contentType = getContentType();
    final int pos = contentType.indexOf(";");

    if (pos == -1) {
        return CharsetUtil.ISO_8859_1;
    }/*from   ww w.j  a  v a2  s  .  c  o m*/

    return Charset.forName(contentType.substring(pos + 1).trim());

}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

public ByteBuf preprocessJson(Integer jsonIndex, ByteBuf content) throws IOException {
    String packet = URLDecoder.decode(content.toString(CharsetUtil.UTF_8), CharsetUtil.UTF_8.name());

    int startPos = 0;
    if (jsonIndex != null) {
        // skip "d="
        startPos = 2;//from  w  w w  .  ja va 2s .  c  o  m

        /**
        * double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
        * (c) socket.io.js
        *
        * @see https://github.com/Automattic/socket.io-client/blob/1.3.3/socket.io.js#L2682
        */
        packet = packet.replace("\\\\n", "\\n");
    }

    int splitIndex = packet.indexOf(":");
    String len = packet.substring(startPos, splitIndex);
    Integer length = Integer.valueOf(len);

    packet = packet.substring(splitIndex + 1, splitIndex + length + 1);
    packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8);

    return Unpooled.wrappedBuffer(packet.getBytes(CharsetUtil.UTF_8));
}

From source file:com.corundumstudio.socketio.protocol.PacketDecoder.java

License:Apache License

public Packet decodePackets(ByteBuf buffer, ClientHead client) throws IOException {
    if (isStringPacket(buffer)) {
        // TODO refactor
        int headEndIndex = buffer.bytesBefore((byte) -1);
        int len = (int) readLong(buffer, headEndIndex);

        ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len);
        // skip this frame
        buffer.readerIndex(buffer.readerIndex() + 1 + len);
        return decode(client, frame);
    } else if (hasLengthHeader(buffer)) {
        // TODO refactor
        int lengthEndIndex = buffer.bytesBefore((byte) ':');
        int lenHeader = (int) readLong(buffer, lengthEndIndex);
        int len = utf8scanner.getActualLength(buffer, lenHeader);

        ByteBuf frame = buffer.slice(buffer.readerIndex() + 1, len);
        if (lenHeader != len) {
            frame = Unpooled.wrappedBuffer(frame.toString(CharsetUtil.UTF_8).getBytes(CharsetUtil.ISO_8859_1));
        }/*from   ww w. ja  v a2s  .  c om*/
        // skip this frame
        buffer.readerIndex(buffer.readerIndex() + 1 + len);
        return decode(client, frame);
    }
    return decode(client, buffer);
}

From source file:com.ibasco.agql.core.utils.ByteBufUtils.java

License:Open Source License

public static String readString(ByteBuf buffer) {
    return readString(buffer, CharsetUtil.ISO_8859_1, false, null);
}

From source file:com.myftpserver.handler.SendTextFileHandler.java

License:Apache License

@Override
public void startToSend(ChannelHandlerContext ctx) throws Exception {
    br = new BufferedReader(new InputStreamReader(new FileInputStream(fs.getDownloadFile()), "ISO-8859-1"));
    line = br.readLine();//from   w w w . j  av a  2 s  . c o m
    ctx.writeAndFlush(Unpooled.copiedBuffer(line + "\r\n", CharsetUtil.ISO_8859_1));
}

From source file:com.myftpserver.handler.SendTextFileHandler.java

License:Apache License

@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    if (isCompleted) {
        br.close();//from w  w w  . j av a  2 s .c  o  m
        br = null;
        closeChannel(ctx);
    } else {
        if (br.ready()) {
            if (ctx.channel().isWritable()) {
                line = br.readLine();
                if (line == null)
                    isCompleted = true;
                else
                    ctx.writeAndFlush(Unpooled.copiedBuffer(line + "\r\n", CharsetUtil.ISO_8859_1));
            }
        } else
            isCompleted = true;
    }
}

From source file:com.streamsets.pipeline.lib.parser.net.TestDelimitedLengthFieldBasedFrameDecoder.java

License:Apache License

@Test
public void testDelimitedLengths() throws Exception {
    Charset charset = CharsetUtil.ISO_8859_1;
    EmbeddedChannel ch = getTestChannel(charset, 100, 0, false);

    String v1 = "a";
    String v2 = "abcdefghij";
    String v3 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsxt"
            + "uvwxyz";

    writeStringAndAssert(ch, v1, charset, false, false);
    writeStringAndAssert(ch, v2, charset, false, false);
    writeStringAndAssert(ch, v3, charset, false, true);

    writeStringAndAssert(ch, v1, charset, true, false);
    writeStringAndAssert(ch, v2, charset, true, false);
    writeStringAndAssert(ch, v3, charset, true, true);

    writeStringAndAssert(ch, v1, charset, false, false);

    ch.close();/*  w  w w  .  j  a v a  2s.co  m*/
}

From source file:com.streamsets.pipeline.lib.parser.net.TestDelimitedLengthFieldBasedFrameDecoder.java

License:Apache License

@Test
public void testMaxFrameLengthOverflow() throws Exception {
    Charset charset = CharsetUtil.ISO_8859_1;
    // maxFrameLength plus adjustment would overflow an int
    final long numBytes = Integer.MAX_VALUE - 1;
    final int lengthAdjustment = 10;
    EmbeddedChannel ch = getTestChannel(charset, (int) numBytes, lengthAdjustment, true);

    //this is a bad frame, but will still test the overflow condition
    String longString = String.valueOf(numBytes) + " abcd";

    try {//from  w  w w  .  j a va2s  . com
        ch.writeInbound(Unpooled.copiedBuffer(longString, charset));
        Assert.fail("TooLongFrameException should have been thrown");
    } catch (TooLongFrameException ignored) {
        //ignored
    }
    Assert.assertNull(ch.readInbound());

    ch.close();
}