Example usage for io.netty.buffer ByteBuf toString

List of usage examples for io.netty.buffer ByteBuf toString

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf toString.

Prototype

public abstract String toString(Charset charset);

Source Link

Document

Decodes this buffer's readable bytes into a string with the specified character set name.

Usage

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testResourceMembersWithExcludeFields() throws Exception {
    DefaultResourceState parentState = new DefaultResourceState("parent");
    parentState.uri(new URI("/parent"));
    parentState.putProperty("A", 1);
    parentState.putProperty("B", 2);

    DefaultResourceState child1State = new DefaultResourceState("child1");
    child1State.uri(new URI("/parent/child1"));
    child1State.putProperty("foo", "bar");
    child1State.putProperty("hello", "world");

    DefaultResourceState child2State = new DefaultResourceState("child2");
    child2State.uri(new URI("/parent/child2"));
    child2State.putProperty("foo", "baz");
    child2State.putProperty("goodbye", "world");

    parentState.addMember(child1State);
    parentState.addMember(child2State);

    ByteBuf buffer = encode(parentState, "*,-A,members(*,-foo,-self)");
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(encoded);

    assertThat(root.get(LiveOak.ID).asText()).isEqualTo("parent");
    assertThat(root.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent");
    assertThat(root.size()).isEqualTo(4);
    assertThat(root.get("B").asInt()).isEqualTo(2);

    ObjectNode child1Node = (ObjectNode) root.get(LiveOak.MEMBERS).get(0);
    assertThat(child1Node.get(LiveOak.ID).asText()).isEqualTo("child1");
    assertThat(child1Node.get(LiveOak.SELF)).isNull();
    assertThat(child1Node.size()).isEqualTo(2);
    assertThat(child1Node.get("hello").asText()).isEqualTo("world");

    ObjectNode child2Node = (ObjectNode) root.get(LiveOak.MEMBERS).get(1);
    assertThat(child2Node.get(LiveOak.ID).asText()).isEqualTo("child2");
    assertThat(child2Node.get(LiveOak.SELF)).isNull();
    assertThat(child2Node.size()).isEqualTo(2);
    assertThat(child2Node.get("goodbye").asText()).isEqualTo("world");
}

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testResourceWithReferenceResource() throws Exception {
    DefaultResourceState parentState = new DefaultResourceState("parent");
    parentState.uri(new URI("/parent"));
    parentState.putProperty("A", 1);
    parentState.putProperty("B", 2);

    DefaultResourceState child1State = new DefaultResourceState("child1");
    child1State.uri(new URI("/parent/child1"));
    child1State.putProperty("foo", "bar");
    child1State.putProperty("hello", "world");

    DefaultResourceState child2State = new DefaultResourceState("child2");
    child2State.uri(new URI("/parent/child2"));
    child2State.putProperty("foo", "baz");
    child2State.putProperty("goodbye", "world");

    parentState.putProperty("child1", child1State);
    parentState.putProperty("child2", child2State);

    ByteBuf buffer = encode(parentState, "*");
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(encoded);

    assertThat(root.get(LiveOak.ID).asText()).isEqualTo("parent");
    assertThat(root.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent");
    assertThat(root.size()).isEqualTo(6);
    assertThat(root.get("B").asInt()).isEqualTo(2);

    ObjectNode child1Node = (ObjectNode) root.get("child1");
    assertThat(child1Node.get(LiveOak.ID).asText()).isEqualTo("child1");
    assertThat(child1Node.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent/child1");
    assertThat(child1Node.size()).isEqualTo(2);

    ObjectNode child2Node = (ObjectNode) root.get("child2");
    assertThat(child2Node.get(LiveOak.ID).asText()).isEqualTo("child2");
    assertThat(child2Node.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent/child2");
    assertThat(child2Node.size()).isEqualTo(2);
}

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testResourceWithReferenceReturnFields() throws Exception {
    DefaultResourceState parentState = new DefaultResourceState("parent");
    parentState.uri(new URI("/parent"));
    parentState.putProperty("A", 1);
    parentState.putProperty("B", 2);

    DefaultResourceState child1State = new DefaultResourceState("child1");
    child1State.uri(new URI("/parent/child1"));
    child1State.putProperty("foo", "bar");
    child1State.putProperty("hello", "world");

    DefaultResourceState child2State = new DefaultResourceState("child2");
    child2State.uri(new URI("/parent/child2"));
    child2State.putProperty("foo", "baz");
    child2State.putProperty("goodbye", "world");

    parentState.putProperty("child1", child1State);
    parentState.putProperty("child2", child2State);

    ByteBuf buffer = encode(parentState, "*,child1(foo,hello),child2(foo)");
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(encoded);

    assertThat(root.get(LiveOak.ID).asText()).isEqualTo("parent");
    assertThat(root.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent");
    assertThat(root.size()).isEqualTo(6);
    assertThat(root.get("B").asInt()).isEqualTo(2);

    ObjectNode child1Node = (ObjectNode) root.get("child1");
    assertThat(child1Node.get(LiveOak.ID).asText()).isEqualTo("child1");
    assertThat(child1Node.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent/child1");
    assertThat(child1Node.size()).isEqualTo(4);
    assertThat(child1Node.get("foo").asText()).isEqualTo("bar");
    assertThat(child1Node.get("hello").asText()).isEqualTo("world");

    ObjectNode child2Node = (ObjectNode) root.get("child2");
    assertThat(child2Node.get(LiveOak.ID).asText()).isEqualTo("child2");
    assertThat(child2Node.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent/child2");
    assertThat(child2Node.size()).isEqualTo(3);
    assertThat(child2Node.get("foo").asText()).isEqualTo("baz");
}

From source file:io.liveoak.container.codec.json.JSONEncoderTest.java

License:Open Source License

@Test
public void testResourceWithReferenceExcludeReturnFields() throws Exception {
    DefaultResourceState parentState = new DefaultResourceState("parent");
    parentState.uri(new URI("/parent"));
    parentState.putProperty("A", 1);
    parentState.putProperty("B", 2);

    DefaultResourceState child1State = new DefaultResourceState("child1");
    child1State.uri(new URI("/parent/child1"));
    child1State.putProperty("foo", "bar");
    child1State.putProperty("hello", "world");

    DefaultResourceState child2State = new DefaultResourceState("child2");
    child2State.uri(new URI("/parent/child2"));
    child2State.putProperty("foo", "baz");
    child2State.putProperty("goodbye", "world");

    parentState.putProperty("child1", child1State);
    parentState.putProperty("child2", child2State);

    ByteBuf buffer = encode(parentState, "*,child1(*,-hello,-id),child2(*,-self,-foo)");
    String encoded = buffer.toString(Charset.defaultCharset());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(encoded);

    assertThat(root.get(LiveOak.ID).asText()).isEqualTo("parent");
    assertThat(root.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent");
    assertThat(root.size()).isEqualTo(6);
    assertThat(root.get("B").asInt()).isEqualTo(2);

    ObjectNode child1Node = (ObjectNode) root.get("child1");
    assertThat(child1Node.get(LiveOak.ID)).isNull();
    assertThat(child1Node.get(LiveOak.SELF).get(LiveOak.HREF).asText()).isEqualTo("/parent/child1");
    assertThat(child1Node.size()).isEqualTo(2);
    assertThat(child1Node.get("foo").asText()).isEqualTo("bar");

    ObjectNode child2Node = (ObjectNode) root.get("child2");
    assertThat(child2Node.get(LiveOak.ID).asText()).isEqualTo("child2");
    assertThat(child2Node.get(LiveOak.SELF)).isNull();
    assertThat(child2Node.size()).isEqualTo(2);
    assertThat(child2Node.get("goodbye").asText()).isEqualTo("world");
}

From source file:io.liveoak.container.protocols.ProtocolDetector.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    int nonNewlineBytes = in.bytesBefore((byte) '\n');

    in.markReaderIndex();//from  w  w  w  . j  a v  a2 s. c o m

    if (nonNewlineBytes > 0) {
        ByteBuf lineBuffer = in.readBytes(nonNewlineBytes);
        String line = lineBuffer.toString(UTF_8);

        //SslHandler sslHandler = context.getPipeline().writeState( SslHandler.class );

        in.resetReaderIndex();
        ByteBuf fullBuffer = in.readBytes(super.actualReadableBytes());

        if (line.startsWith("CONNECT") || line.startsWith("STOMP")) {
            this.configurator.switchToPureStomp(ctx.pipeline());
        } else {
            this.configurator.switchToHttpWebSockets(ctx.pipeline());
        }

        ctx.pipeline().fireChannelRead(fullBuffer);
    }
}

From source file:io.liveoak.stomp.common.StompFrameDecoder.java

License:Open Source License

protected FrameHeader decodeHeader(ByteBuf buffer) {
    FrameHeader header = null;//w  ww. ja  va2 s .co  m

    while (header == null || buffer.isReadable()) {
        int nonNewLineBytes = buffer.bytesBefore((byte) '\n');

        if (nonNewLineBytes == 0) {
            buffer.readByte();
            break;
        }
        if (nonNewLineBytes >= 0) {
            ByteBuf line = buffer.readBytes(nonNewLineBytes);
            buffer.readByte();
            header = processHeaderLine(header, line.toString(UTF_8));
        }
    }

    return header;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readRequestLine() {
    ByteBuf line = readLine();//from ww  w .  j  av a 2s.  c  o  m
    if (line == null) {
        return false;
    }

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');
    if (space < 0) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf methodBuf = line.readSlice(len);

    String methodName = methodBuf.toString(UTF8);
    for (int i = 0; i < METHODS.length; ++i) {
        if (METHODS[i].equals(methodName)) {
            this.method = i;
            break;
        }
    }

    if (this.method == null) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    if ("CONNECT".equals(methodName)) {
        this.upgrade = true;
    }

    // skip the space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    ByteBuf urlBuf = null;
    ByteBuf versionBuf = null;
    if (space < 0) {
        // HTTP/1.0
        urlBuf = line.readSlice(line.readableBytes());
    } else {
        len = space - line.readerIndex();
        urlBuf = line.readSlice(len);
        versionBuf = line.readSlice(line.readableBytes());
    }

    this.url = urlBuf.toString(UTF8).trim();

    if (versionBuf != null) {
        if (!readVersion(versionBuf)) {
            setError(Error.INVALID_VERSION);
            return false;
        }
    } else {
        this.versionMajor = 1;
        this.versionMinor = 0;
    }
    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readStatusLine() {
    ByteBuf line = readLine();//www .  j  a v  a  2  s  . com

    if (line == null) {
        return false;
    }

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf versionBuf = line.readSlice(len);

    if (!readVersion(versionBuf)) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    // skip space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    len = space - line.readerIndex();

    ByteBuf statusBuf = line.readSlice(len);

    int status = -1;

    try {
        status = Integer.parseInt(statusBuf.toString(UTF8));
    } catch (NumberFormatException e) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    if (status > 999 || status < 100) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    this.statusCode = status;

    // skip space
    line.readByte();

    ByteBuf messageBuf = line.readSlice(line.readableBytes());

    this.statusMessage = messageBuf.toString(UTF8).trim();

    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readHeader(ByteBuf line, List<String> target, boolean analyze) {
    int colonLoc = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ':');

    if (colonLoc < 0) {
        // maybe it's a continued header
        if (line.readableBytes() > 1) {
            char c = (char) line.getByte(0);
            if (c == ' ' || c == '\t') {
                // it IS a continued header value
                int lastIndex = this.headers.size() - 1;
                String val = this.headers.get(lastIndex);
                val = val + " " + line.toString(ASCII).trim();
                this.headers.set(lastIndex, val);
                return true;
            }//w w  w . j  a v  a 2 s.  co m
        }
        return false;
    }

    int len = colonLoc - line.readerIndex();
    ByteBuf keyBuf = line.readSlice(len);

    // skip colon
    line.readByte();

    ByteBuf valueBuf = line.readSlice(line.readableBytes());

    String key = keyBuf.toString(UTF8).trim();
    String value = valueBuf.toString(UTF8).trim();

    target.add(key);
    target.add(value);

    if (analyze) {
        return analyzeHeader(key.toLowerCase(), value);
    }

    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readChunkStart() {
    ByteBuf line = readLine();
    if (line == null) {
        return false;
    }//from   w ww .j av  a  2 s.com

    try {
        int len = Integer.parseInt(line.toString(UTF8).trim(), 16);
        this.length = len;
    } catch (NumberFormatException e) {
        setError(Error.INVALID_CHUNK_SIZE);
        return false;
    }

    return true;
}