Search in sources :

Example 1 with Binary

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

the class AmqpSendHandler method createProtonMessage.

/**
     * Create Proton message from deviceId and content string
     * @param deviceId The device name string
     * @param message The message to be sent
     */
public void createProtonMessage(String deviceId, com.microsoft.azure.sdk.iot.service.Message message) {
    // Codes_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_005: [The function shall create a new Message (Proton) object]
    org.apache.qpid.proton.message.Message protonMessage = Proton.message();
    // Codes_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_006: [The function shall set
    // the standard properties on the Proton Message object]
    Properties properties = new Properties();
    properties.setMessageId(message.getMessageId());
    properties.setTo(buildToDevicePath(deviceId));
    properties.setAbsoluteExpiryTime(message.getExpiryTimeUtc());
    properties.setCorrelationId(message.getCorrelationId());
    if (message.getUserId() != null) {
        properties.setUserId(new Binary(message.getUserId().getBytes()));
    }
    protonMessage.setProperties(properties);
    // the application properties on the Proton Message object]
    if (message.getProperties() != null && message.getProperties().size() > 0) {
        Map<String, String> applicationPropertiesMap = new HashMap<>(message.getProperties().size());
        for (Map.Entry<String, String> entry : message.getProperties().entrySet()) {
            applicationPropertiesMap.put(entry.getKey(), entry.getValue());
        }
        ApplicationProperties applicationProperties = new ApplicationProperties(applicationPropertiesMap);
        protonMessage.setApplicationProperties(applicationProperties);
    }
    // Codes_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_007: [The function shall create a Binary (Proton) object from the content string]
    Binary binary = new Binary(message.getBytes());
    // Codes_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_008: [The function shall create a data Section (Proton) object from the Binary]
    Section section = new Data(binary);
    // Codes_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_009: [The function shall set the Message body to the created data section]
    protonMessage.setBody(section);
    messagesToBeSent.add(protonMessage);
}
Also used : HashMap(java.util.HashMap) Binary(org.apache.qpid.proton.amqp.Binary) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Binary

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

the class AmqpSendHandlerTest method createProtonMessage_creates_Message_and_sets_Properties.

// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_004: [The function shall create a new Message (Proton) object]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_005: [The function shall set the “to” property on the Message object using the created device path]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_006: [The function shall create a Binary (Proton) object from the content string]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_007: [The function shall create a data Section (Proton) object from the Binary]
// Tests_SRS_SERVICE_SDK_JAVA_AMQPSENDHANDLER_12_008: [The function shall set the Message body to the created data section]
@Test
public void createProtonMessage_creates_Message_and_sets_Properties() throws UnsupportedEncodingException {
    // Arrange
    String hostName = "aaa";
    String userName = "bbb";
    String sasToken = "ccc";
    String deviceId = "deviceId";
    String content = "abcdefghijklmnopqrst";
    String toProperty = "/devices/deviceId/messages/devicebound";
    IotHubServiceClientProtocol iotHubServiceClientProtocol = IotHubServiceClientProtocol.AMQPS;
    AmqpSendHandler amqpSendHandler = new AmqpSendHandler(hostName, userName, sasToken, iotHubServiceClientProtocol);
    com.microsoft.azure.sdk.iot.service.Message iotMessage = new com.microsoft.azure.sdk.iot.service.Message(content);
    Map<String, String> userDefinedProperties = new HashMap<>(5);
    userDefinedProperties.put("key1", "value1");
    userDefinedProperties.put("key2", "value2");
    userDefinedProperties.put("key3", "value3");
    userDefinedProperties.put("key4", "value4");
    userDefinedProperties.put("key5", "value5");
    iotMessage.setProperties(userDefinedProperties);
    // Assert
    new Expectations() {

        {
            message = Proton.message();
            new Properties();
            result = properties;
            properties.setTo(toProperty);
            message.setProperties(properties);
            binary = new Binary(content.getBytes());
            section = new Data(binary);
            message.setApplicationProperties((ApplicationProperties) any);
            message.setBody(section);
        }
    };
    // Act
    amqpSendHandler.createProtonMessage(deviceId, iotMessage);
    new Verifications() {

        {
            properties.setTo(toProperty);
            properties.setMessageId(any);
            properties.setAbsoluteExpiryTime((Date) any);
            properties.setCorrelationId(any);
        }
    };
}
Also used : Expectations(mockit.Expectations) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) Message(org.apache.qpid.proton.message.Message) Properties(org.apache.qpid.proton.amqp.messaging.Properties) Verifications(mockit.Verifications) IotHubServiceClientProtocol(com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol) Binary(org.apache.qpid.proton.amqp.Binary) Test(org.junit.Test)

Example 3 with Binary

use of org.apache.qpid.proton.amqp.Binary 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 4 with Binary

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

the class AmqpsTransport method iotHubMessageToProtonMessage.

/**
     * Creates a proton message from the IoTHub message.
     * @param message the IoTHub input message.
     * @return the proton message.
     */
private MessageImpl iotHubMessageToProtonMessage(com.microsoft.azure.sdk.iot.device.Message message) {
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    MessageImpl outgoingMessage = (MessageImpl) Proton.message();
    logger.LogInfo("Content of message is %s, method name is %s ", new String(message.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET), logger.getMethodName());
    Properties properties = new Properties();
    if (message.getMessageId() != null) {
        properties.setMessageId(message.getMessageId());
    }
    outgoingMessage.setProperties(properties);
    // Codes_SRS_AMQPSTRANSPORT_15_038: [The function shall add all user properties to the application properties of the Proton message.]
    if (message.getProperties().length > 0) {
        Map<String, String> userProperties = new HashMap<>(message.getProperties().length);
        for (MessageProperty messageProperty : message.getProperties()) {
            if (!MessageProperty.RESERVED_PROPERTY_NAMES.contains(messageProperty.getName())) {
                userProperties.put(messageProperty.getName(), messageProperty.getValue());
            }
        }
        ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
        outgoingMessage.setApplicationProperties(applicationProperties);
    }
    Binary binary = new Binary(message.getBytes());
    Section section = new Data(binary);
    outgoingMessage.setBody(section);
    logger.LogInfo("Started converting IoT Hub message into AmpqsMessage, method name is %s ", logger.getMethodName());
    return outgoingMessage;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Data(org.apache.qpid.proton.amqp.messaging.Data) Binary(org.apache.qpid.proton.amqp.Binary) Properties(org.apache.qpid.proton.amqp.messaging.Properties) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl) Section(org.apache.qpid.proton.amqp.messaging.Section)

Aggregations

Binary (org.apache.qpid.proton.amqp.Binary)4 Properties (org.apache.qpid.proton.amqp.messaging.Properties)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)2 Data (org.apache.qpid.proton.amqp.messaging.Data)2 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)1 AmqpSendHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Expectations (mockit.Expectations)1 Verifications (mockit.Verifications)1 Section (org.apache.qpid.proton.amqp.messaging.Section)1 Message (org.apache.qpid.proton.message.Message)1 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)1 Test (org.junit.Test)1