Search in sources :

Example 1 with MalformedFrameException

use of com.rabbitmq.client.MalformedFrameException in project rabbitmq-java-client by rabbitmq.

the class ConnectionOpen method crazyProtocolHeader.

@Test
public void crazyProtocolHeader() throws IOException {
    ConnectionFactory factory = TestUtils.connectionFactory();
    // keep the frame handler's socket
    Socket fhSocket = SocketFactory.getDefault().createSocket("localhost", AMQP.PROTOCOL.PORT);
    SocketFrameHandler fh = new SocketFrameHandler(fhSocket);
    // major, minor
    fh.sendHeader(100, 3);
    DataInputStream in = fh.getInputStream();
    // we should get a valid protocol header back
    byte[] header = new byte[4];
    in.read(header);
    // The protocol header is "AMQP" plus a version that the server
    // supports.  We can really only test for the first bit.
    assertEquals("AMQP", new String(header));
    in.read(header);
    assertEquals(in.available(), 0);
    // gives an error.
    if (!fhSocket.isClosed()) {
        fh.setTimeout(500);
        // NB the frame handler will return null if the socket times out
        try {
            fh.readFrame();
            fail("Expected socket read to fail due to socket being closed");
        } catch (MalformedFrameException mfe) {
            fail("Expected nothing, rather than a badly-formed something");
        } catch (IOException ioe) {
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) MalformedFrameException(com.rabbitmq.client.MalformedFrameException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SocketFrameHandler(com.rabbitmq.client.impl.SocketFrameHandler) Socket(java.net.Socket) Test(org.junit.Test)

Example 2 with MalformedFrameException

use of com.rabbitmq.client.MalformedFrameException in project rabbitmq-java-client by rabbitmq.

the class Frame method protocolVersionMismatch.

/**
 * Private API - A protocol version mismatch is detected by checking the
 * three next bytes if a frame type of (int)'A' is read from an input
 * stream. If the next three bytes are 'M', 'Q' and 'P', then it's
 * likely the broker is trying to tell us we are speaking the wrong AMQP
 * protocol version.
 *
 * @throws MalformedFrameException
 *                 if an AMQP protocol version mismatch is detected
 * @throws MalformedFrameException
 *                 if a corrupt AMQP protocol identifier is read
 */
public static void protocolVersionMismatch(DataInputStream is) throws IOException {
    MalformedFrameException x;
    // We expect the letters M, Q, P in that order: generate an informative error if they're not found
    byte[] expectedBytes = new byte[] { 'M', 'Q', 'P' };
    for (byte expectedByte : expectedBytes) {
        int nextByte = is.readUnsignedByte();
        if (nextByte != expectedByte) {
            throw new MalformedFrameException("Invalid AMQP protocol header from server: expected character " + expectedByte + ", got " + nextByte);
        }
    }
    try {
        int[] signature = new int[4];
        for (int i = 0; i < 4; i++) {
            signature[i] = is.readUnsignedByte();
        }
        if (signature[0] == 1 && signature[1] == 1 && signature[2] == 8 && signature[3] == 0) {
            x = new MalformedFrameException("AMQP protocol version mismatch; we are version " + AMQP.PROTOCOL.MAJOR + "-" + AMQP.PROTOCOL.MINOR + "-" + AMQP.PROTOCOL.REVISION + ", server is 0-8");
        } else {
            String sig = "";
            for (int i = 0; i < 4; i++) {
                if (i != 0)
                    sig += ",";
                sig += signature[i];
            }
            x = new MalformedFrameException("AMQP protocol version mismatch; we are version " + AMQP.PROTOCOL.MAJOR + "-" + AMQP.PROTOCOL.MINOR + "-" + AMQP.PROTOCOL.REVISION + ", server sent signature " + sig);
        }
    } catch (IOException ex) {
        x = new MalformedFrameException("Invalid AMQP protocol header from server");
    }
    throw x;
}
Also used : MalformedFrameException(com.rabbitmq.client.MalformedFrameException) LongString(com.rabbitmq.client.LongString)

Example 3 with MalformedFrameException

use of com.rabbitmq.client.MalformedFrameException in project rabbitmq-java-client by rabbitmq.

the class FrameBuilder method handleProtocolVersionMismatch.

/**
 * Handle a protocol version mismatch.
 * @return
 * @throws IOException
 * @see Frame#protocolVersionMismatch(DataInputStream)
 */
private void handleProtocolVersionMismatch() throws IOException {
    // Probably an AMQP.... header indicating a version mismatch
    // Otherwise meaningless, so try to read the version,
    // and throw an exception, whether we read the version
    // okay or not.
    // Try to read everything from the network, this header
    // is small and should never require several network reads.
    byte[] expectedBytes = new byte[] { 'M', 'Q', 'P' };
    int expectedBytesCount = 0;
    while (somethingToRead() && expectedBytesCount < 3) {
        // We expect the letters M, Q, P in that order: generate an informative error if they're not found
        int nextByte = readFromBuffer();
        if (nextByte != expectedBytes[expectedBytesCount]) {
            throw new MalformedFrameException("Invalid AMQP protocol header from server: expected character " + expectedBytes[expectedBytesCount] + ", got " + nextByte);
        }
        expectedBytesCount++;
    }
    if (expectedBytesCount != 3) {
        throw new MalformedFrameException("Invalid AMQP protocol header from server: read only " + (expectedBytesCount + 1) + " byte(s) instead of 4");
    }
    int[] signature = new int[4];
    for (int i = 0; i < 4; i++) {
        if (somethingToRead()) {
            signature[i] = readFromBuffer();
        } else {
            throw new MalformedFrameException("Invalid AMQP protocol header from server");
        }
    }
    MalformedFrameException x;
    if (signature[0] == 1 && signature[1] == 1 && signature[2] == 8 && signature[3] == 0) {
        x = new MalformedFrameException("AMQP protocol version mismatch; we are version " + AMQP.PROTOCOL.MAJOR + "-" + AMQP.PROTOCOL.MINOR + "-" + AMQP.PROTOCOL.REVISION + ", server is 0-8");
    } else {
        String sig = "";
        for (int i = 0; i < 4; i++) {
            if (i != 0)
                sig += ",";
            sig += signature[i];
        }
        x = new MalformedFrameException("AMQP protocol version mismatch; we are version " + AMQP.PROTOCOL.MAJOR + "-" + AMQP.PROTOCOL.MINOR + "-" + AMQP.PROTOCOL.REVISION + ", server sent signature " + sig);
    }
    throw x;
}
Also used : MalformedFrameException(com.rabbitmq.client.MalformedFrameException)

Example 4 with MalformedFrameException

use of com.rabbitmq.client.MalformedFrameException in project rabbitmq-java-client by rabbitmq.

the class FrameBuilderTest method protocolMismatchHeader.

@Test
public void protocolMismatchHeader() throws IOException {
    ByteBuffer[] buffers = new ByteBuffer[] { ByteBuffer.wrap(new byte[] { 'A' }), ByteBuffer.wrap(new byte[] { 'A', 'M', 'Q' }), ByteBuffer.wrap(new byte[] { 'A', 'N', 'Q', 'P' }), ByteBuffer.wrap(new byte[] { 'A', 'M', 'Q', 'P' }), ByteBuffer.wrap(new byte[] { 'A', 'M', 'Q', 'P', 1, 1, 8 }), ByteBuffer.wrap(new byte[] { 'A', 'M', 'Q', 'P', 1, 1, 8, 0 }), ByteBuffer.wrap(new byte[] { 'A', 'M', 'Q', 'P', 1, 1, 9, 1 }) };
    String[] messages = new String[] { "Invalid AMQP protocol header from server: read only 1 byte(s) instead of 4", "Invalid AMQP protocol header from server: read only 3 byte(s) instead of 4", "Invalid AMQP protocol header from server: expected character 77, got 78", "Invalid AMQP protocol header from server", "Invalid AMQP protocol header from server", "AMQP protocol version mismatch; we are version 0-9-1, server is 0-8", "AMQP protocol version mismatch; we are version 0-9-1, server sent signature 1,1,9,1" };
    for (int i = 0; i < buffers.length; i++) {
        builder = new FrameBuilder(channel, buffers[i]);
        try {
            builder.readFrame();
            fail("protocol header not correct, exception should have been thrown");
        } catch (MalformedFrameException e) {
            assertThat(e.getMessage(), is(messages[i]));
        }
    }
}
Also used : MalformedFrameException(com.rabbitmq.client.MalformedFrameException) FrameBuilder(com.rabbitmq.client.impl.nio.FrameBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with MalformedFrameException

use of com.rabbitmq.client.MalformedFrameException in project rabbitmq-java-client by rabbitmq.

the class ValueReader method readFieldValue.

private static Object readFieldValue(DataInputStream in) throws IOException {
    Object value = null;
    switch(in.readUnsignedByte()) {
        case 'S':
            value = readLongstr(in);
            break;
        case 'I':
            value = in.readInt();
            break;
        case 'D':
            int scale = in.readUnsignedByte();
            byte[] unscaled = new byte[4];
            in.readFully(unscaled);
            value = new BigDecimal(new BigInteger(unscaled), scale);
            break;
        case 'T':
            value = readTimestamp(in);
            break;
        case 'F':
            value = readTable(in);
            break;
        case 'A':
            value = readArray(in);
            break;
        case 'b':
            value = in.readByte();
            break;
        case 'd':
            value = in.readDouble();
            break;
        case 'f':
            value = in.readFloat();
            break;
        case 'l':
            value = in.readLong();
            break;
        case 's':
            value = in.readShort();
            break;
        case 't':
            value = in.readBoolean();
            break;
        case 'x':
            value = readBytes(in);
            break;
        case 'V':
            value = null;
            break;
        default:
            throw new MalformedFrameException("Unrecognised type in table");
    }
    return value;
}
Also used : MalformedFrameException(com.rabbitmq.client.MalformedFrameException) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

MalformedFrameException (com.rabbitmq.client.MalformedFrameException)5 Test (org.junit.Test)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 LongString (com.rabbitmq.client.LongString)1 SocketFrameHandler (com.rabbitmq.client.impl.SocketFrameHandler)1 FrameBuilder (com.rabbitmq.client.impl.nio.FrameBuilder)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1