Search in sources :

Example 1 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-iot-sdk-java by Azure.

the class AmqpsTransport method protonMessageToIoTHubMessage.

/**
     * Converts an AMQPS message to a corresponding IoT Hub message.
     *
     * @param protonMsg the AMQPS message.
     *
     * @return the corresponding IoT Hub message.
     */
private Message protonMessageToIoTHubMessage(MessageImpl protonMsg) {
    logger.LogInfo("Started converting AmpqsMessage into IoT Hub message, method name is %s ", logger.getMethodName());
    Data d = (Data) protonMsg.getBody();
    Binary b = d.getValue();
    byte[] msgBody = new byte[b.getLength()];
    ByteBuffer buffer = b.asByteBuffer();
    buffer.get(msgBody);
    Message msg = new Message(msgBody);
    logger.LogInfo("Content of received message is %s, method name is %s ", new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET), logger.getMethodName());
    Properties properties = protonMsg.getProperties();
    //in the IoT Hub message properties if they exist.
    for (Method m : properties.getClass().getMethods()) {
        if (m.getName().startsWith("get")) {
            try {
                String propertyName = Character.toLowerCase(m.getName().charAt(3)) + m.getName().substring(4);
                Object value = m.invoke(properties);
                if (value != null && !propertyName.equals("class")) {
                    String val = value.toString();
                    if (MessageProperty.isValidAppProperty(propertyName, val)) {
                        msg.setProperty(propertyName, val);
                    }
                }
            } catch (IllegalAccessException e) {
                logger.LogError("Attempted to access private or protected member of class during message conversion." + e.getMessage());
            } catch (InvocationTargetException e) {
                logger.LogError("Exception thrown while attempting to get member variable. See: " + e.getMessage());
            }
        }
    }
    // Setting the user properties
    if (protonMsg.getApplicationProperties() != null) {
        Map<String, String> applicationProperties = protonMsg.getApplicationProperties().getValue();
        for (Map.Entry<String, String> entry : applicationProperties.entrySet()) {
            String propertyKey = entry.getKey();
            if (!MessageProperty.RESERVED_PROPERTY_NAMES.contains(propertyKey)) {
                msg.setProperty(entry.getKey(), entry.getValue());
            }
        }
    }
    logger.LogInfo("Completed the conversion of AmpqsMessage into IoT Hub message, method name is %s ", logger.getMethodName());
    return msg;
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) Method(java.lang.reflect.Method) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) ByteBuffer(java.nio.ByteBuffer) InvocationTargetException(java.lang.reflect.InvocationTargetException) Binary(org.apache.qpid.proton.amqp.Binary) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-iot-sdk-java by Azure.

the class AmqpsTransportTest method sendMessagesAddsUserPropertiesToProtonApplicationProperties.

// Tests_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
@Test
public void sendMessagesAddsUserPropertiesToProtonApplicationProperties(@Mocked final Message mockMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final IotHubOutboundPacket mockPacket) throws IOException {
    final Map<String, Object> context = new HashMap<>();
    final byte[] messageBytes = new byte[] { 1, 2 };
    final MessageProperty[] iotHubMessageProperties = new MessageProperty[] { new MessageProperty("key1", "value1"), new MessageProperty("key2", "value2") };
    final Map<String, String> userProperties = new HashMap<>(2);
    userProperties.put(iotHubMessageProperties[0].getName(), iotHubMessageProperties[0].getValue());
    userProperties.put(iotHubMessageProperties[1].getName(), iotHubMessageProperties[1].getValue());
    new NonStrictExpectations() {

        {
            new AmqpsIotHubConnection(mockConfig, false);
            result = mockConnection;
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            result = mockPacket;
            mockPacket.getMessage();
            result = mockMsg;
            mockMsg.getBytes();
            result = messageBytes;
            new MessageImpl();
            result = mockProtonMessage;
            mockMsg.getProperties();
            result = iotHubMessageProperties;
            mockConnection.sendMessage(mockProtonMessage);
            result = 1;
            new ApplicationProperties(userProperties);
        }
    };
    AmqpsTransport transport = new AmqpsTransport(mockConfig, false);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    new Verifications() {

        {
            new IotHubOutboundPacket(mockMsg, mockCallback, context);
            times = 1;
            mockPacket.getMessage();
            times = 1;
            mockConnection.sendMessage(mockProtonMessage);
            times = 1;
            new ApplicationProperties(userProperties);
            times = 1;
            mockProtonMessage.setApplicationProperties((ApplicationProperties) any);
            times = 1;
        }
    };
}
Also used : AmqpsTransport(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsTransport) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AmqpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.amqps.AmqpsIotHubConnection) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Test(org.junit.Test)

Example 3 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class AbstractHonoClient method setApplicationProperties.

/**
 * Set the application properties for a Proton Message but do a check for all properties first if they only contain
 * values that the AMQP 1.0 spec allows.
 *
 * @param msg The Proton message. Must not be null.
 * @param properties The map containing application properties.
 * @throws NullPointerException if the message passed in is null.
 * @throws IllegalArgumentException if the properties contain any value that AMQP 1.0 disallows.
 */
protected static final void setApplicationProperties(final Message msg, final Map<String, ?> properties) {
    if (properties != null) {
        final Map<String, Object> propsToAdd = new HashMap<>();
        // check the three types not allowed by AMQP 1.0 spec for application properties (list, map and array)
        for (final Map.Entry<String, ?> entry : properties.entrySet()) {
            if (entry.getValue() != null) {
                if (entry.getValue() instanceof List) {
                    throw new IllegalArgumentException(String.format("Application property %s can't be a List", entry.getKey()));
                } else if (entry.getValue() instanceof Map) {
                    throw new IllegalArgumentException(String.format("Application property %s can't be a Map", entry.getKey()));
                } else if (entry.getValue().getClass().isArray()) {
                    throw new IllegalArgumentException(String.format("Application property %s can't be an Array", entry.getKey()));
                }
            }
            propsToAdd.put(entry.getKey(), entry.getValue());
        }
        final ApplicationProperties applicationProperties = new ApplicationProperties(propsToAdd);
        msg.setApplicationProperties(applicationProperties);
    }
}
Also used : HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-service-bus-java by Azure.

the class MessageConverter method convertAmqpMessageToBrokeredMessage.

public static Message convertAmqpMessageToBrokeredMessage(org.apache.qpid.proton.message.Message amqpMessage, byte[] deliveryTag) {
    Message brokeredMessage;
    Section body = amqpMessage.getBody();
    if (body != null) {
        if (body instanceof Data) {
            Binary messageData = ((Data) body).getValue();
            brokeredMessage = new Message(messageData.getArray());
        } else {
            // TODO: handle other types of message body
            brokeredMessage = new Message();
        }
    } else {
        brokeredMessage = new Message();
    }
    // Application properties
    ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
    if (applicationProperties != null) {
        brokeredMessage.setProperties(applicationProperties.getValue());
    }
    // Header
    brokeredMessage.setTimeToLive(Duration.ofMillis(amqpMessage.getTtl()));
    brokeredMessage.setDeliveryCount(amqpMessage.getDeliveryCount());
    // Properties
    brokeredMessage.setMessageId(amqpMessage.getMessageId().toString());
    brokeredMessage.setContentType(amqpMessage.getContentType());
    Object correlationId = amqpMessage.getCorrelationId();
    if (correlationId != null) {
        brokeredMessage.setCorrelationId(amqpMessage.getCorrelationId().toString());
    }
    brokeredMessage.setLabel(amqpMessage.getSubject());
    brokeredMessage.setTo(amqpMessage.getProperties().getTo());
    brokeredMessage.setReplyTo(amqpMessage.getReplyTo());
    brokeredMessage.setReplyToSessionId(amqpMessage.getReplyToGroupId());
    brokeredMessage.setSessionId(amqpMessage.getGroupId());
    // Message Annotations
    MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
    if (messageAnnotations != null) {
        Map<Symbol, Object> messageAnnotationsMap = messageAnnotations.getValue();
        if (messageAnnotationsMap != null) {
            for (Map.Entry<Symbol, Object> entry : messageAnnotationsMap.entrySet()) {
                String entryName = entry.getKey().toString();
                switch(entryName) {
                    case ClientConstants.ENQUEUEDTIMEUTCNAME:
                        brokeredMessage.setEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.SCHEDULEDENQUEUETIMENAME:
                        brokeredMessage.setScheduledEnqueuedTimeUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.SEQUENCENUBMERNAME:
                        brokeredMessage.setSequenceNumber((long) entry.getValue());
                        break;
                    case ClientConstants.LOCKEDUNTILNAME:
                        brokeredMessage.setLockedUntilUtc(((Date) entry.getValue()).toInstant());
                        break;
                    case ClientConstants.PARTITIONKEYNAME:
                        brokeredMessage.setPartitionKey((String) entry.getValue());
                        break;
                    case ClientConstants.DEADLETTERSOURCENAME:
                        brokeredMessage.setDeadLetterSource((String) entry.getValue());
                        break;
                }
            }
        }
    }
    if (deliveryTag != null && deliveryTag.length == ClientConstants.LOCKTOKENSIZE) {
        UUID lockToken = Util.convertDotNetBytesToUUID(deliveryTag);
        brokeredMessage.setLockToken(lockToken);
    } else {
        brokeredMessage.setLockToken(ClientConstants.ZEROLOCKTOKEN);
    }
    brokeredMessage.setDeliveryTag(deliveryTag);
    return brokeredMessage;
}
Also used : Symbol(org.apache.qpid.proton.amqp.Symbol) Data(org.apache.qpid.proton.amqp.messaging.Data) Section(org.apache.qpid.proton.amqp.messaging.Section) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Binary(org.apache.qpid.proton.amqp.Binary) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-service-bus-java by Azure.

the class Util method getDataSerializedSize.

// Remove this.. Too many cases, too many types...
public static int getDataSerializedSize(Message amqpMessage) {
    if (amqpMessage == null) {
        return 0;
    }
    int payloadSize = getPayloadSize(amqpMessage);
    // EventData - accepts only PartitionKey - which is a String & stuffed into MessageAnnotation
    MessageAnnotations messageAnnotations = amqpMessage.getMessageAnnotations();
    ApplicationProperties applicationProperties = amqpMessage.getApplicationProperties();
    int annotationsSize = 0;
    int applicationPropertiesSize = 0;
    if (messageAnnotations != null) {
        annotationsSize += Util.sizeof(messageAnnotations.getValue());
    }
    if (applicationProperties != null) {
        applicationPropertiesSize += Util.sizeof(applicationProperties.getValue());
    }
    return annotationsSize + applicationPropertiesSize + payloadSize;
}
Also used : MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties)

Aggregations

ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)41 HashMap (java.util.HashMap)25 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)14 Properties (org.apache.qpid.proton.amqp.messaging.Properties)12 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)11 Message (org.apache.qpid.proton.message.Message)11 Binary (org.apache.qpid.proton.amqp.Binary)10 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)10 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)9 Test (org.junit.Test)9 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)8 Data (org.apache.qpid.proton.amqp.messaging.Data)8 Symbol (org.apache.qpid.proton.amqp.Symbol)7 Section (org.apache.qpid.proton.amqp.messaging.Section)7 Map (java.util.Map)6 Header (org.apache.qpid.proton.amqp.messaging.Header)6 Test (org.junit.jupiter.api.Test)5 Buffer (io.vertx.core.buffer.Buffer)3 JsonObject (io.vertx.core.json.JsonObject)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3