Search in sources :

Example 11 with AMQPSymbol

use of com.swiftmq.amqp.v100.types.AMQPSymbol in project swiftmq-client by iitsoftware.

the class Source method decode.

private void decode() throws Exception {
    List l = getValue();
    AMQPType t = null;
    int idx = 0;
    // Factory  : AddressFactory
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() != AMQPTypeDecoder.NULL)
        address = AddressFactory.create(t);
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            durable = new TerminusDurability(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'durable' in 'Source' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            expiryPolicy = new TerminusExpiryPolicy(((AMQPSymbol) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'expiryPolicy' in 'Source' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            timeout = new Seconds(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'timeout' in 'Source' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamic = (AMQPBoolean) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamic' in 'Source' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamicNodeProperties = new NodeProperties(((AMQPMap) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamicNodeProperties' in 'Source' type: " + e);
    }
    // Factory  : DistributionModeFactory
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() != AMQPTypeDecoder.NULL)
        distributionMode = DistributionModeFactory.create(t);
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            filter = new FilterSet(((AMQPMap) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'filter' in 'Source' type: " + e);
    }
    // Factory  : OutcomeFactory
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() != AMQPTypeDecoder.NULL)
        defaultOutcome = OutcomeFactory.create(t);
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            outcomes = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'outcomes' in 'Source' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            capabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'capabilities' in 'Source' type: " + e);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) Seconds(com.swiftmq.amqp.v100.generated.transport.definitions.Seconds) IOException(java.io.IOException)

Example 12 with AMQPSymbol

use of com.swiftmq.amqp.v100.types.AMQPSymbol in project swiftmq-client by iitsoftware.

the class Target method decode.

private void decode() throws Exception {
    List l = getValue();
    AMQPType t = null;
    int idx = 0;
    // Factory  : AddressFactory
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() != AMQPTypeDecoder.NULL)
        address = AddressFactory.create(t);
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            durable = new TerminusDurability(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'durable' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            expiryPolicy = new TerminusExpiryPolicy(((AMQPSymbol) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'expiryPolicy' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            timeout = new Seconds(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'timeout' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamic = (AMQPBoolean) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamic' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamicNodeProperties = new NodeProperties(((AMQPMap) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamicNodeProperties' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            capabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'capabilities' in 'Target' type: " + e);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) Seconds(com.swiftmq.amqp.v100.generated.transport.definitions.Seconds) IOException(java.io.IOException)

Example 13 with AMQPSymbol

use of com.swiftmq.amqp.v100.types.AMQPSymbol in project rabbitmq-stream-java-client by rabbitmq.

the class SwiftMqCodec method encode.

@Override
public EncodedMessage encode(Message message) {
    AMQPMessage outboundMessage;
    if (message instanceof SwiftMqAmqpMessageWrapper) {
        outboundMessage = ((SwiftMqAmqpMessageWrapper) message).message;
    } else {
        outboundMessage = new AMQPMessage();
        if (message.getProperties() != null) {
            com.rabbitmq.stream.Properties headers = message.getProperties();
            com.swiftmq.amqp.v100.generated.messaging.message_format.Properties properties = new com.swiftmq.amqp.v100.generated.messaging.message_format.Properties();
            boolean propertiesSet = false;
            if (headers.getMessageId() != null) {
                if (headers.getMessageId() instanceof String) {
                    properties.setMessageId(new MessageIdString(headers.getMessageIdAsString()));
                } else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
                    properties.setMessageId(new MessageIdUlong(headers.getMessageIdAsLong()));
                } else if (headers.getMessageId().getClass().isArray()) {
                    properties.setMessageId(new MessageIdBinary(headers.getMessageIdAsBinary()));
                } else if (headers.getMessageId() instanceof UUID) {
                    properties.setMessageId(new MessageIdUuid(headers.getMessageIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getUserId() != null) {
                properties.setUserId(new AMQPBinary(headers.getUserId()));
                propertiesSet = true;
            }
            if (headers.getTo() != null) {
                properties.setTo(new AddressString(headers.getTo()));
                propertiesSet = true;
            }
            if (headers.getSubject() != null) {
                properties.setSubject(new AMQPString(headers.getSubject()));
                propertiesSet = true;
            }
            if (headers.getReplyTo() != null) {
                properties.setReplyTo(new AddressString(headers.getReplyTo()));
                propertiesSet = true;
            }
            if (headers.getCorrelationId() != null) {
                if (headers.getCorrelationId() instanceof String) {
                    properties.setCorrelationId(new MessageIdString(headers.getCorrelationIdAsString()));
                } else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
                    properties.setCorrelationId(new MessageIdUlong(headers.getCorrelationIdAsLong()));
                } else if (headers.getCorrelationId().getClass().isArray()) {
                    properties.setCorrelationId(new MessageIdBinary(headers.getCorrelationIdAsBinary()));
                } else if (headers.getCorrelationId() instanceof UUID) {
                    properties.setCorrelationId(new MessageIdUuid(headers.getCorrelationIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getContentType() != null) {
                properties.setContentType(new AMQPSymbol(headers.getContentType()));
                propertiesSet = true;
            }
            if (headers.getContentEncoding() != null) {
                properties.setContentEncoding(new AMQPSymbol(headers.getContentEncoding()));
                propertiesSet = true;
            }
            if (headers.getAbsoluteExpiryTime() > 0) {
                properties.setAbsoluteExpiryTime(new AMQPTimestamp(headers.getAbsoluteExpiryTime()));
                propertiesSet = true;
            }
            if (headers.getCreationTime() > 0) {
                properties.setCreationTime(new AMQPTimestamp(headers.getCreationTime()));
                propertiesSet = true;
            }
            if (headers.getGroupId() != null) {
                properties.setGroupId(new AMQPString(headers.getGroupId()));
                propertiesSet = true;
            }
            if (headers.getGroupSequence() >= 0) {
                properties.setGroupSequence(new SequenceNo(headers.getGroupSequence()));
                propertiesSet = true;
            }
            if (headers.getReplyToGroupId() != null) {
                properties.setReplyToGroupId(new AMQPString(headers.getReplyToGroupId()));
                propertiesSet = true;
            }
            if (propertiesSet) {
                outboundMessage.setProperties(properties);
            }
        }
        if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
            Map<AMQPType, AMQPType> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
            for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
                applicationProperties.put(new AMQPString(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
            } catch (IOException e) {
                throw new StreamException("Error while setting application properties", e);
            }
        }
        if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
            Map<AMQPType, AMQPType> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
            for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
                messageAnnotations.put(new AMQPSymbol(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
            } catch (IOException e) {
                throw new StreamException("Error while setting message annotations", e);
            }
        }
        if (message.getBodyAsBinary() != null) {
            outboundMessage.addData(new Data(message.getBodyAsBinary()));
        }
    }
    try {
        int bufferSize;
        // FIXME estimate the size with all message data
        if (outboundMessage.getData() != null && !outboundMessage.getData().isEmpty()) {
            bufferSize = outboundMessage.getData().get(0).getPredictedSize();
        } else {
            bufferSize = 8192;
        }
        DataByteArrayOutputStream output = new DataByteArrayOutputStream(bufferSize);
        outboundMessage.writeContent(output);
        return new EncodedMessage(output.getCount(), output.getBuffer());
    } catch (IOException e) {
        throw new StreamException("Error while writing AMQP 1.0 message to output stream");
    }
}
Also used : SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) DataByteArrayOutputStream(com.swiftmq.tools.util.DataByteArrayOutputStream) com.swiftmq.amqp.v100.generated.messaging.message_format(com.swiftmq.amqp.v100.generated.messaging.message_format) LinkedHashMap(java.util.LinkedHashMap) StreamException(com.rabbitmq.stream.StreamException) UUID(java.util.UUID) com.rabbitmq.stream.amqp(com.rabbitmq.stream.amqp) IOException(java.io.IOException) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with AMQPSymbol

use of com.swiftmq.amqp.v100.types.AMQPSymbol in project swiftmq-client by iitsoftware.

the class SessionDispatcher method visit.

public void visit(POSendEnd po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " ...");
    endPO = po;
    try {
        EndFrame endFrame = new EndFrame(mySession.getChannel());
        if (po.getCondition() != null) {
            com.swiftmq.amqp.v100.generated.transport.definitions.Error error = new com.swiftmq.amqp.v100.generated.transport.definitions.Error();
            error.setCondition(ErrorConditionFactory.create(new AMQPSymbol(po.getCondition())));
            endFrame.setError(error);
        }
        outboundHandler.send(endFrame);
        checkBothSidesEnd();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " done");
}
Also used : Error(com.swiftmq.amqp.v100.generated.transport.definitions.Error) Error(com.swiftmq.amqp.v100.generated.transport.definitions.Error) com.swiftmq.amqp.v100.generated.transport.definitions(com.swiftmq.amqp.v100.generated.transport.definitions) IOException(java.io.IOException)

Example 15 with AMQPSymbol

use of com.swiftmq.amqp.v100.types.AMQPSymbol in project swiftmq-client by iitsoftware.

the class TargetFactory method create.

/**
 * Creates a TargetIF object.
 *
 * @param bare the bare AMQP type
 * @return TargetIF
 */
public static TargetIF create(AMQPType bare) throws Exception {
    if (bare.getCode() == AMQPTypeDecoder.NULL)
        return null;
    AMQPDescribedConstructor constructor = bare.getConstructor();
    if (constructor == null)
        throw new IOException("Missing constructor: " + bare);
    AMQPType descriptor = constructor.getDescriptor();
    int code = descriptor.getCode();
    if (AMQPTypeDecoder.isULong(code)) {
        long type = ((AMQPUnsignedLong) descriptor).getValue();
        if (type == Target.DESCRIPTOR_CODE)
            return new Target(((AMQPList) bare).getValue());
        if (type == Coordinator.DESCRIPTOR_CODE)
            return new Coordinator(((AMQPList) bare).getValue());
        throw new Exception("Invalid descriptor type: " + type + ", bare=" + bare);
    } else if (AMQPTypeDecoder.isSymbol(code)) {
        String type = ((AMQPSymbol) descriptor).getValue();
        if (type.equals(Target.DESCRIPTOR_NAME))
            return new Target(((AMQPList) bare).getValue());
        if (type.equals(Coordinator.DESCRIPTOR_NAME))
            return new Coordinator(((AMQPList) bare).getValue());
        throw new Exception("Invalid descriptor type: " + type + ", bare=" + bare);
    } else
        throw new Exception("Invalid type of constructor descriptor (actual type=" + code + ", expected=symbold or ulong), bare= " + bare);
}
Also used : IOException(java.io.IOException) Coordinator(com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)14 SequenceNo (com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo)4 Map (java.util.Map)4 JMSException (javax.jms.JMSException)4 AMQPException (com.swiftmq.amqp.v100.client.AMQPException)3 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)3 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NoLocalFilter (com.swiftmq.amqp.v100.generated.filter.filter_types.NoLocalFilter)2 SelectorFilter (com.swiftmq.amqp.v100.generated.filter.filter_types.SelectorFilter)2 Declared (com.swiftmq.amqp.v100.generated.transactions.coordination.Declared)2 Milliseconds (com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds)2 Seconds (com.swiftmq.amqp.v100.generated.transport.definitions.Seconds)2 Iterator (java.util.Iterator)2 StreamException (com.rabbitmq.stream.StreamException)1 com.rabbitmq.stream.amqp (com.rabbitmq.stream.amqp)1 POSendClose (com.swiftmq.amqp.v100.client.po.POSendClose)1 com.swiftmq.amqp.v100.generated.messaging.message_format (com.swiftmq.amqp.v100.generated.messaging.message_format)1 Coordinator (com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator)1