Search in sources :

Example 1 with ValueHandler

use of org.apache.qpid.server.protocol.v1_0.codec.ValueHandler in project qpid-broker-j by apache.

the class ValueHandlerTest method performTest.

private void performTest(final byte type, final byte[] encodedBytes, ValueHandler valueHandler) {
    QpidByteBuffer qbb = QpidByteBuffer.wrap(encodedBytes);
    try {
        valueHandler.parse(qbb);
        fail(String.format("AmqpErrorException is expected for %#02x", type));
    } catch (AmqpErrorException e) {
        assertEquals(String.format("Unexpected error code for %#02x", type), AmqpError.DECODE_ERROR, e.getError().getCondition());
    } catch (Exception e) {
        fail(String.format("Unexpected exception for %#02x: %s", type, e));
    }
}
Also used : AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)

Example 2 with ValueHandler

use of org.apache.qpid.server.protocol.v1_0.codec.ValueHandler in project qpid-broker-j by apache.

the class ArrayTypeConstructor method construct.

@Override
public Object[] construct(final QpidByteBuffer in, final ValueHandler handler) throws AmqpErrorException {
    int size = read(in);
    long remaining = in.remaining();
    if (remaining < (long) size) {
        throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Insufficient data to decode array - requires %d octects, only %d remaining.", size, remaining);
    }
    List<Object> rval;
    int count = read(in);
    TypeConstructor t = handler.readConstructor(in);
    rval = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        rval.add(t.construct(in, handler));
    }
    long expectedRemaining = remaining - size;
    long unconsumedBytes = in.remaining() - expectedRemaining;
    if (unconsumedBytes > 0) {
        final String msg = String.format("Array incorrectly encoded, %d bytes remaining after decoding %d elements", unconsumedBytes, count);
        throw new AmqpErrorException(AmqpError.DECODE_ERROR, msg);
    } else if (unconsumedBytes < 0) {
        final String msg = String.format("Array incorrectly encoded, %d bytes beyond provided size consumed after decoding %d elements", -unconsumedBytes, count);
        throw new AmqpErrorException(AmqpError.DECODE_ERROR, msg);
    }
    if (rval.size() == 0) {
        return null;
    } else {
        return rval.toArray((Object[]) Array.newInstance(rval.get(0).getClass(), rval.size()));
    }
}
Also used : AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)

Example 3 with ValueHandler

use of org.apache.qpid.server.protocol.v1_0.codec.ValueHandler in project qpid-broker-j by apache.

the class FrameHandlerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    _valueHandler = new ValueHandler(AMQPDescribedTypeRegistry.newInstance());
}
Also used : ValueHandler(org.apache.qpid.server.protocol.v1_0.codec.ValueHandler)

Example 4 with ValueHandler

use of org.apache.qpid.server.protocol.v1_0.codec.ValueHandler in project qpid-broker-j by apache.

the class SymbolTypeConstructor method construct.

@Override
public Symbol construct(final QpidByteBuffer in, final ValueHandler handler) throws AmqpErrorException {
    int size;
    if (!in.hasRemaining(getSize())) {
        throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Cannot construct symbol: insufficient input data");
    }
    if (getSize() == 1) {
        size = in.getUnsignedByte();
    } else {
        size = in.getInt();
    }
    if (!in.hasRemaining(size)) {
        throw new AmqpErrorException(AmqpError.DECODE_ERROR, "Cannot construct symbol: insufficient input data");
    }
    byte[] data = new byte[size];
    in.get(data);
    final BinaryString binaryStr = new BinaryString(data);
    Symbol symbolVal = SYMBOL_MAP.get(binaryStr);
    if (symbolVal == null) {
        symbolVal = Symbol.valueOf(new String(data, ASCII));
        SYMBOL_MAP.putIfAbsent(binaryStr, symbolVal);
    }
    return symbolVal;
}
Also used : Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)

Example 5 with ValueHandler

use of org.apache.qpid.server.protocol.v1_0.codec.ValueHandler in project qpid-broker-j by apache.

the class AbstractSection method decode.

private S decode(DescribedTypeConstructor<S> constructor) {
    try (QpidByteBuffer input = getEncodedForm()) {
        int originalPosition = input.position();
        int describedByte = input.get();
        if (describedByte != ValueHandler.DESCRIBED_TYPE) {
            throw new ConnectionScopedRuntimeException("Cannot decode section", new AmqpErrorException(AmqpError.DECODE_ERROR, "Not a described type."));
        }
        ValueHandler handler = new ValueHandler(TYPE_REGISTRY);
        try {
            Object descriptor = handler.parse(input);
            return constructor.construct(descriptor, input, originalPosition, handler).construct(input, handler);
        } catch (AmqpErrorException e) {
            throw new ConnectionScopedRuntimeException("Cannot decode section", e);
        }
    }
}
Also used : ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) ValueHandler(org.apache.qpid.server.protocol.v1_0.codec.ValueHandler)

Aggregations

AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)4 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)2 ValueHandler (org.apache.qpid.server.protocol.v1_0.codec.ValueHandler)2 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1