Search in sources :

Example 1 with Properties

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

the class AmqpSendHandlerTest method createProtonObjects.

private void createProtonObjects() {
    String exceptionMessage = "Not expected function called";
    message = Proton.message();
    messageWithException = new Message() {

        @Override
        public boolean isDurable() {
            return false;
        }

        @Override
        public long getDeliveryCount() {
            return 0;
        }

        @Override
        public short getPriority() {
            return 0;
        }

        @Override
        public boolean isFirstAcquirer() {
            return false;
        }

        @Override
        public long getTtl() {
            return 0;
        }

        @Override
        public void setDurable(boolean b) {
        }

        @Override
        public void setTtl(long l) {
        }

        @Override
        public void setDeliveryCount(long l) {
        }

        @Override
        public void setFirstAcquirer(boolean b) {
        }

        @Override
        public void setPriority(short i) {
        }

        @Override
        public Object getMessageId() {
            return null;
        }

        @Override
        public long getGroupSequence() {
            return 0;
        }

        @Override
        public String getReplyToGroupId() {
            return null;
        }

        @Override
        public long getCreationTime() {
            return 0;
        }

        @Override
        public String getAddress() {
            return null;
        }

        @Override
        public byte[] getUserId() {
            return new byte[0];
        }

        @Override
        public String getReplyTo() {
            return null;
        }

        @Override
        public String getGroupId() {
            return null;
        }

        @Override
        public String getContentType() {
            return null;
        }

        @Override
        public long getExpiryTime() {
            return 0;
        }

        @Override
        public Object getCorrelationId() {
            return null;
        }

        @Override
        public String getContentEncoding() {
            return null;
        }

        @Override
        public String getSubject() {
            return null;
        }

        @Override
        public void setGroupSequence(long l) {
        }

        @Override
        public void setUserId(byte[] bytes) {
        }

        @Override
        public void setCreationTime(long l) {
        }

        @Override
        public void setSubject(String s) {
        }

        @Override
        public void setGroupId(String s) {
        }

        @Override
        public void setAddress(String s) {
        }

        @Override
        public void setExpiryTime(long l) {
        }

        @Override
        public void setReplyToGroupId(String s) {
        }

        @Override
        public void setContentEncoding(String s) {
        }

        @Override
        public void setContentType(String s) {
        }

        @Override
        public void setReplyTo(String s) {
        }

        @Override
        public void setCorrelationId(Object o) {
        }

        @Override
        public void setMessageId(Object o) {
        }

        @Override
        public Header getHeader() {
            return null;
        }

        @Override
        public DeliveryAnnotations getDeliveryAnnotations() {
            return null;
        }

        @Override
        public MessageAnnotations getMessageAnnotations() {
            return null;
        }

        @Override
        public Properties getProperties() {
            return null;
        }

        @Override
        public ApplicationProperties getApplicationProperties() {
            return null;
        }

        @Override
        public Section getBody() {
            return null;
        }

        @Override
        public Footer getFooter() {
            return null;
        }

        @Override
        public void setHeader(Header header) {
        }

        @Override
        public void setDeliveryAnnotations(DeliveryAnnotations deliveryAnnotations) {
        }

        @Override
        public void setMessageAnnotations(MessageAnnotations messageAnnotations) {
        }

        @Override
        public void setProperties(Properties properties) {
        }

        @Override
        public void setApplicationProperties(ApplicationProperties applicationProperties) {
        }

        @Override
        public void setBody(Section section) {
        }

        @Override
        public void setFooter(Footer footer) {
        }

        @Override
        public int decode(byte[] bytes, int i, int i1) {
            return 0;
        }

        @Override
        public int encode(byte[] bytes, int i, int i1) {
            if (exceptionCount == 0) {
                exceptionCount++;
                throw new BufferOverflowException();
            } else {
                return 0;
            }
        }

        @Override
        public void clear() {
        }

        @Override
        public MessageError getError() {
            return null;
        }
    };
    sender = new Sender() {

        @Override
        public Record attachments() {
            return null;
        }

        @Override
        public EndpointState getLocalState() {
            return null;
        }

        @Override
        public EndpointState getRemoteState() {
            return null;
        }

        @Override
        public ErrorCondition getCondition() {
            return null;
        }

        @Override
        public void setCondition(ErrorCondition errorCondition) {
        }

        @Override
        public ErrorCondition getRemoteCondition() {
            return null;
        }

        @Override
        public void free() {
        }

        @Override
        public void open() {
        }

        @Override
        public void close() {
        }

        @Override
        public void setContext(Object o) {
        }

        @Override
        public Object getContext() {
            return null;
        }

        @Override
        public void offer(int i) {
        }

        @Override
        public int send(byte[] bytes, int i, int i1) {
            return 0;
        }

        @Override
        public int send(ReadableBuffer readableBuffer) {
            return 0;
        }

        @Override
        public void abort() {
        }

        @Override
        public String getName() {
            return null;
        }

        @Override
        public Delivery delivery(byte[] bytes) {
            return delivery;
        }

        @Override
        public Delivery delivery(byte[] bytes, int i, int i1) {
            return null;
        }

        @Override
        public Delivery head() {
            return null;
        }

        @Override
        public Delivery current() {
            return null;
        }

        @Override
        public boolean advance() {
            return false;
        }

        @Override
        public Source getSource() {
            return null;
        }

        @Override
        public org.apache.qpid.proton.amqp.transport.Target getTarget() {
            return null;
        }

        @Override
        public void setSource(Source source) {
        }

        @Override
        public void setTarget(org.apache.qpid.proton.amqp.transport.Target target) {
        }

        @Override
        public Source getRemoteSource() {
            return null;
        }

        @Override
        public org.apache.qpid.proton.amqp.transport.Target getRemoteTarget() {
            return null;
        }

        @Override
        public Link next(EnumSet<EndpointState> enumSet, EnumSet<EndpointState> enumSet1) {
            return null;
        }

        @Override
        public int getCredit() {
            return 1;
        }

        @Override
        public int getQueued() {
            return 0;
        }

        @Override
        public int getUnsettled() {
            return 0;
        }

        @Override
        public Session getSession() {
            return session;
        }

        @Override
        public SenderSettleMode getSenderSettleMode() {
            return null;
        }

        @Override
        public void setSenderSettleMode(SenderSettleMode senderSettleMode) {
        }

        @Override
        public SenderSettleMode getRemoteSenderSettleMode() {
            return null;
        }

        @Override
        public ReceiverSettleMode getReceiverSettleMode() {
            return null;
        }

        @Override
        public void setReceiverSettleMode(ReceiverSettleMode receiverSettleMode) {
        }

        @Override
        public ReceiverSettleMode getRemoteReceiverSettleMode() {
            return null;
        }

        @Override
        public void setRemoteSenderSettleMode(SenderSettleMode senderSettleMode) {
        }

        @Override
        public Map<Symbol, Object> getProperties() {
            return null;
        }

        @Override
        public void setProperties(Map<Symbol, Object> map) {
        }

        @Override
        public Map<Symbol, Object> getRemoteProperties() {
            return null;
        }

        @Override
        public int drained() {
            return 0;
        }

        @Override
        public int getRemoteCredit() {
            return 0;
        }

        @Override
        public boolean getDrain() {
            return false;
        }

        @Override
        public void detach() {
        }

        @Override
        public boolean detached() {
            return false;
        }

        @Override
        public void setOfferedCapabilities(Symbol[] symbols) {
        }

        @Override
        public Symbol[] getOfferedCapabilities() {
            return new Symbol[0];
        }

        @Override
        public Symbol[] getRemoteOfferedCapabilities() {
            return new Symbol[0];
        }

        @Override
        public void setDesiredCapabilities(Symbol[] symbols) {
        }

        @Override
        public Symbol[] getDesiredCapabilities() {
            return new Symbol[0];
        }

        @Override
        public Symbol[] getRemoteDesiredCapabilities() {
            return new Symbol[0];
        }
    };
    event = new Event() {

        @Override
        public EventType getEventType() {
            return null;
        }

        @Override
        public Event.Type getType() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Object getContext() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Handler getRootHandler() {
            return null;
        }

        @Override
        public void dispatch(Handler hndlr) throws HandlerException {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public void redispatch(EventType eventType, Handler handler) throws HandlerException {
        }

        @Override
        public void delegate() throws HandlerException {
        }

        @Override
        public Connection getConnection() {
            return connection;
        }

        @Override
        public Session getSession() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Link getLink() {
            return sender;
        }

        @Override
        public Sender getSender() {
            return null;
        }

        @Override
        public Receiver getReceiver() {
            return null;
        }

        @Override
        public Delivery getDelivery() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Transport getTransport() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Reactor getReactor() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Selectable getSelectable() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Task getTask() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Event copy() {
            throw new UnsupportedOperationException(exceptionMessage);
        }

        @Override
        public Record attachments() {
            throw new UnsupportedOperationException(exceptionMessage);
        }
    };
}
Also used : Task(org.apache.qpid.proton.reactor.Task) ReadableBuffer(org.apache.qpid.proton.codec.ReadableBuffer) Reactor(org.apache.qpid.proton.reactor.Reactor) AmqpSendHandler(com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler) Message(org.apache.qpid.proton.message.Message) Symbol(org.apache.qpid.proton.amqp.Symbol) Properties(org.apache.qpid.proton.amqp.messaging.Properties) Source(org.apache.qpid.proton.amqp.transport.Source) Target(org.apache.qpid.proton.amqp.messaging.Target) MessageError(org.apache.qpid.proton.message.MessageError) Selectable(org.apache.qpid.proton.reactor.Selectable) BufferOverflowException(java.nio.BufferOverflowException) org.apache.qpid.proton.amqp.transport(org.apache.qpid.proton.amqp.transport)

Example 2 with Properties

use of org.apache.qpid.proton.amqp.messaging.Properties 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 Properties

use of org.apache.qpid.proton.amqp.messaging.Properties 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 Properties

use of org.apache.qpid.proton.amqp.messaging.Properties 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

Properties (org.apache.qpid.proton.amqp.messaging.Properties)4 Binary (org.apache.qpid.proton.amqp.Binary)3 AmqpSendHandler (com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)2 Data (org.apache.qpid.proton.amqp.messaging.Data)2 Message (org.apache.qpid.proton.message.Message)2 IotHubServiceClientProtocol (com.microsoft.azure.sdk.iot.service.IotHubServiceClientProtocol)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BufferOverflowException (java.nio.BufferOverflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Expectations (mockit.Expectations)1 Verifications (mockit.Verifications)1 Symbol (org.apache.qpid.proton.amqp.Symbol)1 Section (org.apache.qpid.proton.amqp.messaging.Section)1 Target (org.apache.qpid.proton.amqp.messaging.Target)1 org.apache.qpid.proton.amqp.transport (org.apache.qpid.proton.amqp.transport)1 Source (org.apache.qpid.proton.amqp.transport.Source)1 ReadableBuffer (org.apache.qpid.proton.codec.ReadableBuffer)1