Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:io.crate.protocols.postgres.MessagesTest.java

License:Apache License

private static void verifyResponse(EmbeddedChannel channel, String response) {
    byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
    ByteBuf buffer = (ByteBuf) channel.outboundMessages().poll();
    assertThat(buffer.readByte(), is((byte) 'C'));
    assertThat(buffer.readInt(), is(responseBytes.length + 4 + 1));
    byte[] string = new byte[9];
    buffer.readBytes(string);//from   w ww .  j a v  a2  s .  c  o  m
    assertThat(string, is(responseBytes));
}

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

/**
 * Describe Message/*  ww  w .  j a  va2s .  c  o  m*/
 * Header:
 * | 'D' | int32 len
 * <p>
 * Body:
 * | 'S' = prepared statement or 'P' = portal
 * | string nameOfPortalOrStatement
 */
private void handleDescribeMessage(ByteBuf buffer, Channel channel) {
    byte type = buffer.readByte();
    String portalOrStatement = readCString(buffer);
    Session.DescribeResult describeResult = session.describe((char) type, portalOrStatement);
    Collection<Field> fields = describeResult.getFields();
    DataType[] parameterTypes = describeResult.getParameters();
    if (parameterTypes != null) {
        Messages.sendParameterDescription(channel, parameterTypes);
    }
    if (fields == null) {
        Messages.sendNoData(channel);
    } else {
        Messages.sendRowDescription(channel, fields, session.getResultFormatCodes(portalOrStatement));
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

/**
 * | 'C' | int32 len | byte portalOrStatement | string portalOrStatementName |
 *//*from ww w . j a va  2 s  .  c  o  m*/
private void handleClose(ByteBuf buffer, Channel channel) {
    byte b = buffer.readByte();
    String portalOrStatementName = readCString(buffer);
    session.close(b, portalOrStatementName);
    Messages.sendCloseComplete(channel);
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testDescribePortalMessage() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, new AlwaysOKNullAuthentication(), null);

    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    {/*  w ww .j av  a  2s  . c  o  m*/
        ByteBuf buffer = Unpooled.buffer();

        ClientMessages.sendStartupMessage(buffer, "doc");
        ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)",
                new int[] { PGTypes.get(DataTypes.LONG).oid() });
        ClientMessages.sendBindMessage(buffer, "P1", "S1", Collections.singletonList(1));
        channel.writeInbound(buffer);

        // we're not interested in the startup, parse, or bind replies
        channel.flushOutbound();
        channel.outboundMessages().clear();
    }
    {
        // try portal describe message
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.PORTAL, "P1");
        channel.writeInbound(buffer);

        // we should get back a RowDescription message
        ByteBuf response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 'T'));
        assertThat(response.readInt(), is(42));
        assertThat(response.readShort(), is((short) 1));
        assertThat(PostgresWireProtocol.readCString(response), is("($1 IN (1, 2, 3))"));

        assertThat(response.readInt(), is(0));
        assertThat(response.readShort(), is((short) 0));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
        assertThat(response.readShort(), is((short) PGTypes.get(DataTypes.BOOLEAN).typeLen()));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
        assertThat(response.readShort(), is((short) 0));
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testDescribeStatementMessage() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, new AlwaysOKNullAuthentication(), null);

    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    {/*from   w w  w  . j av  a  2  s. c  om*/
        ByteBuf buffer = Unpooled.buffer();

        ClientMessages.sendStartupMessage(buffer, "doc");
        ClientMessages.sendParseMessage(buffer, "S1", "select ? in (1, 2, 3)", new int[0]);
        channel.writeInbound(buffer);

        // we're not interested in the startup, parse, or bind replies
        channel.flushOutbound();
        channel.outboundMessages().clear();
    }
    {
        // try the describe statement variant
        ByteBuf buffer = Unpooled.buffer();
        ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
        channel.writeInbound(buffer);

        // we should get back a ParameterDescription message
        ByteBuf response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 't'));
        assertThat(response.readInt(), is(10));
        assertThat(response.readShort(), is((short) 1));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).oid()));

        // we should get back a RowDescription message
        response = channel.readOutbound();
        assertThat(response.readByte(), is((byte) 'T'));
        assertThat(response.readInt(), is(42));
        assertThat(response.readShort(), is((short) 1));
        assertThat(PostgresWireProtocol.readCString(response), is("($1 IN (1, 2, 3))"));

        assertThat(response.readInt(), is(0));
        assertThat(response.readShort(), is((short) 0));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
        assertThat(response.readShort(), is((short) PGTypes.get(DataTypes.BOOLEAN).typeLen()));
        assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
        assertThat(response.readShort(), is((short) 0));
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testSslRejection() {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class),
            new AlwaysOKNullAuthentication(), null);

    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    ByteBuf buffer = Unpooled.buffer();//  w  w  w. j  av  a 2s.c  o m
    ClientMessages.sendSslReqMessage(buffer);
    channel.writeInbound(buffer);

    // We should get back an 'N'...
    ByteBuf responseBuffer = channel.readOutbound();
    byte response = responseBuffer.readByte();
    assertEquals(response, 'N');

    // ...and continue unencrypted (no ssl handler)
    for (Map.Entry<String, ChannelHandler> entry : channel.pipeline()) {
        assertThat(entry.getValue(), isOneOf(ctx.decoder, ctx.handler));
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testCrateServerVersionIsReceivedOnStartup() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, new AlwaysOKNullAuthentication(), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    ByteBuf buf = Unpooled.buffer();/*from   w w w  .j av  a2s .  c o  m*/
    ClientMessages.sendStartupMessage(buf, "doc");
    channel.writeInbound(buf);

    ByteBuf respBuf;
    respBuf = channel.readOutbound();
    assertThat((char) respBuf.readByte(), is('R')); // Auth OK

    respBuf = channel.readOutbound();
    assertThat((char) respBuf.readByte(), is('S')); // ParameterStatus
    respBuf.readInt(); // length
    String key = PostgresWireProtocol.readCString(respBuf);
    String value = PostgresWireProtocol.readCString(respBuf);

    assertThat(key, is("crate_version"));
    assertThat(value, is(Version.CURRENT.toString()));
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testPasswordMessageAuthenticationProcess() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), new Authentication() {
        @Nullable//  w w w.  ja va  2 s . co m
        @Override
        public AuthenticationMethod resolveAuthenticationType(String user,
                ConnectionProperties connectionProperties) {
            return new AuthenticationMethod() {
                @Nullable
                @Override
                public User authenticate(String userName, @Nullable SecureString passwd,
                        ConnectionProperties connProperties) {
                    return null;
                }

                @Override
                public String name() {
                    return "password";
                }
            };
        }
    }, null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    ByteBuf respBuf;
    ByteBuf buffer = Unpooled.buffer();
    ClientMessages.sendStartupMessage(buffer, "doc");
    channel.writeInbound(buffer);

    respBuf = channel.readOutbound();
    assertThat((char) respBuf.readByte(), is('R')); // AuthenticationCleartextPassword

    buffer = Unpooled.buffer();
    ClientMessages.sendPasswordMessage(buffer, "pw");
    channel.writeInbound(buffer);

    respBuf = channel.readOutbound();
    assertThat((char) respBuf.readByte(), is('R')); // Auth OK
}

From source file:io.crate.protocols.postgres.SslReqHandlerTest.java

@Test
public void testSslReqHandler() {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class),
            new AlwaysOKNullAuthentication(),
            // use a simple ssl context
            getSelfSignedSslContext());//from   www  .  j a  va  2  s.  c  om

    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    sendSslRequest(channel);

    // We should get back an 'S'...
    ByteBuf responseBuffer = channel.readOutbound();
    byte response = responseBuffer.readByte();
    assertEquals(response, 'S');

    // ...and continue encrypted (ssl handler)
    assertThat(channel.pipeline().first(), instanceOf(SslHandler.class));
}

From source file:io.crate.protocols.postgres.types.BooleanType.java

License:Apache License

@Override
public Object readBinaryValue(ByteBuf buffer, int valueLength) {
    assert valueLength == TYPE_LEN : "length should be " + TYPE_LEN
            + " because boolean is just a byte. Actual length: " + valueLength;
    byte value = buffer.readByte();
    switch (value) {
    case 0://ww w  .  j  av  a2 s  . c  o m
        return false;
    case 1:
        return true;
    default:
        throw new IllegalArgumentException("Unsupported binary bool: " + value);
    }
}