Search in sources :

Example 11 with AmqpErrorException

use of org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException 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 12 with AmqpErrorException

use of org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException in project qpid-broker-j by apache.

the class FrameHandler method parse.

@Override
public ProtocolHandler parse(QpidByteBuffer in) {
    try {
        LOGGER.debug("RECV {} bytes", in.remaining());
        Error frameParsingError = null;
        int size;
        int remaining;
        List<ChannelFrameBody> channelFrameBodies = new ArrayList<>();
        while ((remaining = in.remaining()) >= 8 && frameParsingError == null) {
            size = in.getInt();
            if (size < 8) {
                frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", size, 8);
                break;
            }
            if (size > _connectionHandler.getMaxFrameSize()) {
                frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connectionHandler.getMaxFrameSize());
                break;
            }
            if (remaining < size) {
                in.position(in.position() - 4);
                break;
            }
            int dataOffset = (in.get() << 2) & 0x3FF;
            if (dataOffset < 8) {
                frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8);
                break;
            }
            if (dataOffset > size) {
                frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, size);
                break;
            }
            byte type = in.get();
            switch(type) {
                case 0:
                    if (_isSasl) {
                        frameParsingError = createFramingError("received an AMQP frame type when expecting an SASL frame");
                    }
                    break;
                case 1:
                    if (!_isSasl) {
                        frameParsingError = createFramingError("received a SASL frame type when expecting an AMQP frame");
                    }
                    break;
                default:
                    frameParsingError = createFramingError("unknown frame type: %d", type);
            }
            if (frameParsingError != null) {
                break;
            }
            int channel = in.getUnsignedShort();
            if (dataOffset != 8) {
                in.position(in.position() + dataOffset - 8);
            }
            try (QpidByteBuffer dup = in.slice()) {
                dup.limit(size - dataOffset);
                in.position(in.position() + size - dataOffset);
                final boolean hasFrameBody = dup.hasRemaining();
                Object frameBody;
                if (hasFrameBody) {
                    frameBody = _valueHandler.parse(dup);
                    if (dup.hasRemaining()) {
                        if (frameBody instanceof Transfer) {
                            try (QpidByteBuffer payload = dup.slice()) {
                                ((Transfer) frameBody).setPayload(payload);
                            }
                        } else {
                            frameParsingError = createFramingError("Frame length %d larger than contained frame body %s.", size, frameBody);
                            break;
                        }
                    }
                } else {
                    frameBody = null;
                    if (_isSasl) {
                        frameParsingError = createFramingError("Empty (heartbeat) frames are not permitted during SASL negotiation");
                        break;
                    }
                }
                channelFrameBodies.add(new ChannelFrameBody() {

                    @Override
                    public int getChannel() {
                        return channel;
                    }

                    @Override
                    public Object getFrameBody() {
                        return frameBody;
                    }
                });
                if (_isSasl) {
                    break;
                }
            } catch (AmqpErrorException ex) {
                frameParsingError = ex.getError();
            }
        }
        if (frameParsingError != null) {
            _connectionHandler.handleError(frameParsingError);
            _errored = true;
        } else {
            _connectionHandler.receive(channelFrameBodies);
        }
    } catch (RuntimeException e) {
        if (e instanceof ServerScopedRuntimeException) {
            throw e;
        }
        LOGGER.warn("Unexpected exception handling frame", e);
        // This exception is unexpected. The up layer should handle error condition gracefully
        _connectionHandler.handleError(this.createError(AmqpError.INTERNAL_ERROR, e.toString()));
    }
    return this;
}
Also used : ArrayList(java.util.ArrayList) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ChannelFrameBody(org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 13 with AmqpErrorException

use of org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException in project qpid-broker-j by apache.

the class MessageConverter_from_1_0 method convertBodyToObject.

static Object convertBodyToObject(final Message_1_0 serverMessage) {
    SectionDecoderImpl sectionDecoder = new SectionDecoderImpl(MessageConverter_v1_0_to_Internal.TYPE_REGISTRY.getSectionDecoderRegistry());
    Object bodyObject = null;
    List<EncodingRetainingSection<?>> sections = null;
    try {
        try (QpidByteBuffer allData = serverMessage.getContent()) {
            sections = sectionDecoder.parseAll(allData);
        }
        List<EncodingRetainingSection<?>> bodySections = new ArrayList<>(sections.size());
        ListIterator<EncodingRetainingSection<?>> iterator = sections.listIterator();
        EncodingRetainingSection<?> previousSection = null;
        while (iterator.hasNext()) {
            EncodingRetainingSection<?> section = iterator.next();
            if (section instanceof AmqpValueSection || section instanceof DataSection || section instanceof AmqpSequenceSection) {
                if (previousSection != null && (previousSection.getClass() != section.getClass() || section instanceof AmqpValueSection)) {
                    throw new MessageConversionException("Message is badly formed and has multiple body section which are not all Data or not all AmqpSequence");
                } else {
                    previousSection = section;
                }
                bodySections.add(section);
            }
        }
        // In 1.0 of the spec, it is illegal to have message with no body but AMQP-127 asks to have that restriction lifted
        if (!bodySections.isEmpty()) {
            EncodingRetainingSection<?> firstBodySection = bodySections.get(0);
            if (firstBodySection instanceof AmqpValueSection) {
                bodyObject = convertValue(firstBodySection.getValue());
            } else if (firstBodySection instanceof DataSection) {
                int totalSize = 0;
                for (EncodingRetainingSection<?> section : bodySections) {
                    totalSize += ((DataSection) section).getValue().getArray().length;
                }
                byte[] bodyData = new byte[totalSize];
                ByteBuffer buf = ByteBuffer.wrap(bodyData);
                for (EncodingRetainingSection<?> section : bodySections) {
                    buf.put(((DataSection) section).getValue().asByteBuffer());
                }
                bodyObject = bodyData;
            } else {
                ArrayList<Object> totalSequence = new ArrayList<>();
                for (EncodingRetainingSection<?> section : bodySections) {
                    totalSequence.addAll(((AmqpSequenceSection) section).getValue());
                }
                bodyObject = convertValue(totalSequence);
            }
        }
    } catch (AmqpErrorException e) {
        throw new ConnectionScopedRuntimeException(e);
    } finally {
        if (sections != null) {
            sections.forEach(EncodingRetaining::dispose);
        }
    }
    return bodyObject;
}
Also used : MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) EncodingRetaining(org.apache.qpid.server.protocol.v1_0.type.messaging.codec.EncodingRetaining) ArrayList(java.util.ArrayList) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) AmqpSequenceSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)

Example 14 with AmqpErrorException

use of org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException in project qpid-broker-j by apache.

the class Message_1_0 method getContent.

@Override
public QpidByteBuffer getContent(final int offset, final int length) {
    if (getMessageMetaData().getVersion() == 0) {
        SectionDecoder sectionDecoder = new SectionDecoderImpl(DESCRIBED_TYPE_REGISTRY.getSectionDecoderRegistry());
        try {
            List<EncodingRetainingSection<?>> sections;
            // not just #getSize()
            try (QpidByteBuffer allSectionsContent = super.getContent(0, Integer.MAX_VALUE)) {
                sections = sectionDecoder.parseAll(allSectionsContent);
            }
            List<QpidByteBuffer> bodySectionContent = new ArrayList<>();
            for (final EncodingRetainingSection<?> section : sections) {
                if (section instanceof DataSection || section instanceof AmqpValueSection || section instanceof AmqpSequenceSection) {
                    bodySectionContent.add(section.getEncodedForm());
                }
                section.dispose();
            }
            try (QpidByteBuffer bodyContent = QpidByteBuffer.concatenate(bodySectionContent)) {
                bodySectionContent.forEach(QpidByteBuffer::dispose);
                return bodyContent.view(offset, length);
            }
        } catch (AmqpErrorException e) {
            throw new ConnectionScopedRuntimeException(e);
        }
    } else {
        return super.getContent(offset, length);
    }
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) ArrayList(java.util.ArrayList) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) AmqpSequenceSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection) DataSection(org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AmqpValueSection(org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection) SectionDecoder(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoder)

Example 15 with AmqpErrorException

use of org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException in project qpid-broker-j by apache.

the class SendingLinkEndpoint method reattachLink.

@Override
protected void reattachLink(final Attach attach) throws AmqpErrorException {
    if (getSource() == null) {
        throw new IllegalStateException("Terminus should be set when resuming a Link.");
    }
    if (attach.getSource() == null) {
        throw new IllegalStateException("Attach.getSource should not be null when resuming a Link. That would be recovering the Link.");
    }
    Source newSource = (Source) attach.getSource();
    Source oldSource = getSource();
    final SendingDestination destination = getSession().getSendingDestination(getLink(), oldSource);
    prepareConsumerOptionsAndFilters(destination);
    if (getDestination() instanceof ExchangeSendingDestination && !Boolean.TRUE.equals(newSource.getDynamic())) {
        final SendingDestination newDestination = getSession().getSendingDestination(getLink(), newSource);
        if (getSession().updateSourceForSubscription(this, newSource, newDestination)) {
            setDestination(newDestination);
        }
    }
    attachReceived(attach);
}
Also used : MessageSource(org.apache.qpid.server.message.MessageSource) BaseSource(org.apache.qpid.server.protocol.v1_0.type.BaseSource) Source(org.apache.qpid.server.protocol.v1_0.type.messaging.Source)

Aggregations

AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)20 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)10 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)10 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)8 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)8 MessageSource (org.apache.qpid.server.message.MessageSource)7 BaseSource (org.apache.qpid.server.protocol.v1_0.type.BaseSource)6 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)6 ArrayList (java.util.ArrayList)5 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)5 EncodingRetainingSection (org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TokenMgrError (org.apache.qpid.server.filter.selector.TokenMgrError)4 AmqpValueSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection)4 TransactionError (org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError)4 DeliveryState (org.apache.qpid.server.protocol.v1_0.type.DeliveryState)3 AmqpSequenceSection (org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpSequenceSection)3 DataSection (org.apache.qpid.server.protocol.v1_0.type.messaging.DataSection)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2