use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.
the class MessageHelper 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.
* @param properties The map containing application properties.
* @throws NullPointerException if the message passed in is {@code null}.
* @throws IllegalArgumentException if the properties contain any value that AMQP 1.0 disallows.
*/
public static void setApplicationProperties(final Message msg, final Map<String, ?> properties) {
Objects.requireNonNull(msg);
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());
}
msg.setApplicationProperties(new ApplicationProperties(propsToAdd));
}
}
use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.
the class MessageHelper method getRegistrationAssertion.
private static String getRegistrationAssertion(final Message msg, final boolean removeAssertion) {
Objects.requireNonNull(msg);
String assertion = null;
final ApplicationProperties properties = msg.getApplicationProperties();
if (properties != null) {
final Object obj;
if (removeAssertion) {
obj = properties.getValue().remove(APP_PROPERTY_REGISTRATION_ASSERTION);
} else {
obj = properties.getValue().get(APP_PROPERTY_REGISTRATION_ASSERTION);
}
if (obj instanceof String) {
assertion = (String) obj;
}
}
return assertion;
}
use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.
the class MessageHelper method addProperty.
/**
* Adds a property to an AMQP 1.0 message.
* <p>
* The property is added to the message's <em>application-properties</em>.
*
* @param msg The message.
* @param key The property key.
* @param value The property value.
* @throws NullPointerException if any of the parameters are {@code null}.
*/
public static void addProperty(final Message msg, final String key, final Object value) {
Objects.requireNonNull(msg);
Objects.requireNonNull(key);
Objects.requireNonNull(value);
final ApplicationProperties props = Optional.ofNullable(msg.getApplicationProperties()).orElseGet(() -> {
final ApplicationProperties result = new ApplicationProperties(new HashMap<>());
msg.setApplicationProperties(result);
return result;
});
props.getValue().put(key, value);
}
use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-iot-sdk-java by Azure.
the class AmqpsCbsSenderLinkHandler method createCBSAuthenticationMessage.
// The warning is for how getSasTokenAuthentication() may return null, but this code only executes when our config
// uses SAS_TOKEN auth, and that is sufficient at confirming that getSasTokenAuthentication() will return a non-null instance
@SuppressWarnings("ConstantConditions")
private MessageImpl createCBSAuthenticationMessage(DeviceClientConfig deviceClientConfig, UUID correlationId) throws TransportException {
MessageImpl outgoingMessage = (MessageImpl) Proton.message();
Properties properties = new Properties();
// Note that setting "messageId = correlationId" is intentional.
// IotHub only responds correctly if this correlation id is set this way
properties.setMessageId(correlationId);
properties.setTo(CBS_TO);
properties.setReplyTo(CBS_REPLY);
outgoingMessage.setProperties(properties);
Map<String, Object> userProperties = new HashMap<>(3);
userProperties.put(OPERATION_KEY, OPERATION_VALUE);
userProperties.put(TYPE_KEY, TYPE_VALUE);
String host = deviceClientConfig.getGatewayHostname();
if (host == null || host.isEmpty()) {
host = deviceClientConfig.getIotHubHostname();
}
userProperties.put(NAME_KEY, host + DEVICES_PATH + deviceClientConfig.getDeviceId());
ApplicationProperties applicationProperties = new ApplicationProperties(userProperties);
outgoingMessage.setApplicationProperties(applicationProperties);
Section section;
try {
section = new AmqpValue(String.valueOf(deviceClientConfig.getSasTokenAuthentication().getSasToken()));
outgoingMessage.setBody(section);
} catch (IOException e) {
log.error("Failed to renew sas token while building new cbs authentication message", e);
throw new TransportException(e);
}
return outgoingMessage;
}
use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-iot-sdk-java by Azure.
the class CbsSenderLinkHandler method sendAuthenticationMessage.
public int sendAuthenticationMessage(UUID authenticationMessageCorrelationId) {
MessageImpl outgoingMessage = (MessageImpl) Proton.message();
Properties properties = new Properties();
// Note that setting "messageId = correlationId" is intentional.
// IotHub only responds correctly if this correlation id is set this way
properties.setMessageId(authenticationMessageCorrelationId);
properties.setTo(CBS_TO);
properties.setReplyTo(CBS_REPLY);
outgoingMessage.setProperties(properties);
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put(PUT_TOKEN_OPERATION, PUT_TOKEN_OPERATION_VALUE);
if (credential != null) {
TokenRequestContext context = new TokenRequestContext().addScopes(IOTHUB_PUBLIC_SCOPE);
this.currentAccessToken = credential.getToken(context).block();
if (this.currentAccessToken == null) {
log.error("The AccessToken supplied by the TokenCredential for the CbsSenderLinkHandler was null.");
return -1;
}
applicationProperties.put(PUT_TOKEN_EXPIRY, Date.from(this.currentAccessToken.getExpiresAt().toInstant()));
applicationProperties.put(PUT_TOKEN_TYPE, BEARER);
Section section = new AmqpValue("Bearer " + this.currentAccessToken.getToken());
outgoingMessage.setBody(section);
} else if (this.sasTokenProvider != null) {
String sasToken = this.sasTokenProvider.getSignature();
this.currentAccessToken = getAccessTokenFromSasToken(sasToken);
applicationProperties.put(PUT_TOKEN_TYPE, SAS_TOKEN);
Section section = new AmqpValue(sasToken);
outgoingMessage.setBody(section);
} else {
this.currentAccessToken = getAccessTokenFromSasToken(this.sasToken);
applicationProperties.put(PUT_TOKEN_TYPE, SAS_TOKEN);
Section section = new AmqpValue(this.sasToken);
outgoingMessage.setBody(section);
}
applicationProperties.put(PUT_TOKEN_AUDIENCE, this.senderLink.getSession().getConnection().getHostname());
outgoingMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
return this.sendMessageAndGetDeliveryTag(outgoingMessage);
}
Aggregations