use of org.apache.qpid.proton.amqp.messaging.Data 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;
}
use of org.apache.qpid.proton.amqp.messaging.Data in project azure-iot-sdk-java by Azure.
the class AmqpFileUploadNotificationReceivedHandler method onDelivery.
/**
* Event handler for the on delivery event
* @param event The proton event object
*/
@Override
public void onDelivery(Event event) {
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_004: [The event handler shall get the Link, Receiver and Delivery (Proton) objects from the event]
Receiver recv = (Receiver) event.getLink();
Delivery delivery = recv.current();
if (delivery.isReadable() && !delivery.isPartial() && delivery.getLink().getName().equalsIgnoreCase(FILE_NOTIFICATION_RECEIVE_TAG)) {
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_005: [The event handler shall read the received buffer]
int size = delivery.pending();
byte[] buffer = new byte[size];
int read = recv.recv(buffer, 0, buffer.length);
recv.advance();
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_006: [The event handler shall create a Message (Proton) object from the decoded buffer]
org.apache.qpid.proton.message.Message msg = Proton.message();
msg.decode(buffer, 0, read);
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_007: [The event handler shall settle the Delivery with the Accepted outcome]
delivery.disposition(Accepted.getInstance());
delivery.settle();
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_008: [The event handler shall close the Session and Connection (Proton)]
recv.getSession().close();
recv.getSession().getConnection().close();
// Codes_SRS_SERVICE_SDK_JAVA_AMQPFILEUPLOADNOTIFICATIONRECEIVEDHANDLER_25_009: [The event handler shall call the FeedbackReceived callback if it has been initialized]
if (amqpFeedbackReceivedEvent != null) {
if (msg.getBody() instanceof Data) {
Data feedbackJson = (Data) msg.getBody();
amqpFeedbackReceivedEvent.onFeedbackReceived(feedbackJson.getValue().toString());
}
}
}
}
use of org.apache.qpid.proton.amqp.messaging.Data 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;
}
Aggregations