Search in sources :

Example 36 with ApplicationProperties

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));
    }
}
Also used : HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with ApplicationProperties

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;
}
Also used : ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject)

Example 38 with ApplicationProperties

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);
}
Also used : ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties)

Example 39 with ApplicationProperties

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;
}
Also used : HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) IOException(java.io.IOException) 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) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 40 with ApplicationProperties

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);
}
Also used : TokenRequestContext(com.azure.core.credential.TokenRequestContext) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) 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) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

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