Java Code Examples for org.apache.qpid.proton.message.Message#getCorrelationId()

The following examples show how to use org.apache.qpid.proton.message.Message#getCorrelationId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CredentialsMessageFilter.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Checks whether a given credentials message contains all required properties.
 *
 * @param linkTarget The resource path to check the message's properties against for consistency.
 * @param msg The AMQP 1.0 message to perform the checks on.
 * @return {@code true} if the message passes all checks.
 */
public static boolean verify(final ResourceIdentifier linkTarget, final Message msg) {

    if (msg.getMessageId() == null && msg.getCorrelationId() == null) {
        LOG.trace("message has neither a message-id nor correlation-id");
        return false;
    } else if (!CredentialsConstants.CredentialsAction.isValid(msg.getSubject())) {
        LOG.trace("message [{}] does not contain valid subject property", msg.getMessageId());
        return false;
    } else if (msg.getReplyTo() == null) {
        LOG.trace("message [{}] has no reply-to address set", msg.getMessageId());
        return false;
    } else if (!MessageHelper.hasDataBody(msg)) {
        LOG.trace("message [{}] contains no Data section payload", msg.getMessageId());
        return false;
    } else {
        return true;
    }
}
 
Example 2
Source File: AmqpAdapterClientCommandConsumer.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
private static void traceCommand(final HonoConnection con, final ResourceIdentifier address,
        final Message message) {
    final Tracer tracer = con.getTracer();
    if (tracer instanceof NoopTracer) {
        return;
    }

    // try to extract Span context from incoming message
    final SpanContext spanContext = TracingHelper.extractSpanContext(tracer, message);
    final Span currentSpan = createSpan("receive command", address.getTenantId(),
            address.getResourceId(), null, tracer, spanContext);
    final Object correlationId = message.getCorrelationId();
    if (correlationId == null || correlationId instanceof String) {
        final Map<String, String> items = new HashMap<>(5);
        items.put(Fields.EVENT, "received command message");
        TracingHelper.TAG_CORRELATION_ID.set(currentSpan, ((String) correlationId));
        items.put("to", message.getAddress());
        items.put("reply-to", message.getReplyTo());
        items.put("name", message.getSubject());
        items.put("content-type", message.getContentType());
        currentSpan.log(items);
    } else {
        TracingHelper.logError(currentSpan,
                "received invalid command message. correlation-id is not of type string.");
    }
}
 
Example 3
Source File: AmqpExampleDevice.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private void handleCommand(final Message commandMessage) {

        final String subject = commandMessage.getSubject();
        final String commandPayload = MessageHelper.getPayloadAsString(commandMessage);

        if (commandMessage.getReplyTo() == null || commandMessage.getCorrelationId() == null) {
            // one-way command
            System.out.println(String.format("Received one-way command [name: %s]: %s", subject, commandPayload));
        } else {
            // request-response command
            System.out.println(String.format("Received command [name: %s]: %s", subject, commandPayload));
            sendCommandResponse(commandMessage);
        }
    }
 
Example 4
Source File: CommandResponse.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates a command response from a given message.
 *
 * @param message The command response message.
 *
 * @return The command response or {@code null} if message or any of correlationId, address and status is null or if
 *         the status code is &lt; 200 or &gt;= 600.
 * @throws NullPointerException if message is {@code null}.
 */
public static CommandResponse from(final Message message) {
    Objects.requireNonNull(message);

    final String correlationId = message.getCorrelationId() instanceof String ? (String) message.getCorrelationId() : null;
    final Integer status = MessageHelper.getStatus(message);

    if (correlationId == null || message.getAddress() == null || status == null) {
        LOG.debug("cannot create CommandResponse: invalid message (correlationId: {}, address: {}, status: {})",
                correlationId, message.getAddress(), status);
        return null;
    } else if (INVALID_STATUS_CODE.test(status)) {
        LOG.debug("cannot create CommandResponse: status is invalid: {}", status);
        return null;
    } else {
        try {
            final ResourceIdentifier resource = ResourceIdentifier.fromString(message.getAddress());
            MessageHelper.addTenantId(message, resource.getTenantId());
            MessageHelper.addDeviceId(message, resource.getResourceId());

            final String deviceId = resource.getResourceId();
            final String pathWithoutBase = resource.getPathWithoutBase();
            if (pathWithoutBase.length() < deviceId.length() + 3) {
                throw new IllegalArgumentException("invalid resource length");
            }
            // pathWithoutBase starts with deviceId/[bit flag]
            final String replyToOptionsBitFlag = pathWithoutBase.substring(deviceId.length() + 1, deviceId.length() + 2);
            final boolean replyToContainedDeviceId = Command.isReplyToContainedDeviceIdOptionSet(replyToOptionsBitFlag);
            final String replyToId = pathWithoutBase.replaceFirst(deviceId + "/" + replyToOptionsBitFlag,
                    replyToContainedDeviceId ? deviceId + "/" : "");
            return new CommandResponse(message, replyToId);
        } catch (NullPointerException | IllegalArgumentException e) {
            LOG.debug("error creating CommandResponse", e);
            return null;
        }
    }
}
 
Example 5
Source File: BaseMessageFilter.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Checks if an AMQP message contains either a message ID or a correlation ID.
 *
 * @param msg The message.
 * @return {@code true} if the message has an ID that can be used for correlation.
 */
protected static final boolean hasCorrelationId(final Message msg) {

    if (msg.getMessageId() == null && msg.getCorrelationId() == null) {
        LOG.trace("message has neither a message-id nor correlation-id");
        return false;
    } else {
        return true;
    }
}