Search in sources :

Example 81 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class CommandResponse method fromAddressAndCorrelationId.

/**
 * Creates a response for a given response message address and correlation ID.
 *
 * @param address The address of the response message. It has to contain a tenant segment followed by the segments
 *                returned by {@link Commands#getDeviceFacingReplyToId(String, String, MessagingType)}.
 * @param correlationId The correlation ID of the command that this is the response for.
 * @param payload The payload of the response or {@code null} if the response has no payload.
 * @param contentType The contentType of the response or {@code null} if the response has no payload.
 * @param status The HTTP status code indicating the outcome of the command.
 * @return The response or {@code null} if correlation ID is {@code null}, the address cannot be parsed,
 *         the status is {@code null} or if the status code is < 200 or >= 600.
 */
public static CommandResponse fromAddressAndCorrelationId(final String address, final String correlationId, final Buffer payload, final String contentType, final Integer status) {
    if (correlationId == null || !ResourceIdentifier.isValid(address) || status == null) {
        LOG.debug("cannot create CommandResponse: invalid message (correlationId: {}, address: {}, status: {})", correlationId, address, status);
        return null;
    } else if (INVALID_STATUS_CODE.test(status)) {
        LOG.debug("cannot create CommandResponse: status is invalid: {}", status);
        return null;
    }
    final ResourceIdentifier resource = ResourceIdentifier.fromString(address);
    final String tenantId = resource.getTenantId();
    final String deviceId = resource.getResourceId();
    if (tenantId == null || deviceId == null) {
        LOG.debug("cannot create CommandResponse: invalid address, missing tenant and/or device identifier");
        return null;
    }
    try {
        // resource.getPathWithoutBase() represents the result of Commands.getDeviceFacingReplyToId()
        final Pair<String, MessagingType> replyToIdMessagingTypePair = Commands.getOriginalReplyToIdAndMessagingType(resource.getPathWithoutBase(), deviceId);
        return new CommandResponse(tenantId, deviceId, payload, contentType, status, correlationId, replyToIdMessagingTypePair.one(), replyToIdMessagingTypePair.two());
    } catch (final IllegalArgumentException e) {
        LOG.debug("error creating CommandResponse: invalid address, invalid last path component", e);
        return null;
    }
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MessagingType(org.eclipse.hono.util.MessagingType)

Example 82 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class AmqpServiceBaseTest method testHandleReceiverOpenForwardsToEndpoint.

/**
 * Verifies that the service notifies a registered endpoint about a client
 * that has established a link.
 */
@Test
public void testHandleReceiverOpenForwardsToEndpoint() {
    // GIVEN a server with an endpoint
    final ResourceIdentifier targetAddress = ResourceIdentifier.from(ENDPOINT, Constants.DEFAULT_TENANT, null);
    final AmqpEndpoint endpoint = mock(AmqpEndpoint.class);
    when(endpoint.getName()).thenReturn(ENDPOINT);
    final AuthorizationService authService = mock(AuthorizationService.class);
    when(authService.isAuthorized(Constants.PRINCIPAL_ANONYMOUS, targetAddress, Activity.WRITE)).thenReturn(Future.succeededFuture(Boolean.TRUE));
    final AmqpServiceBase<ServiceConfigProperties> server = createServer(endpoint);
    server.setAuthorizationService(authService);
    // WHEN a client connects to the server using this endpoint
    final Target target = getTarget(targetAddress);
    final ProtonReceiver receiver = mock(ProtonReceiver.class);
    when(receiver.getRemoteTarget()).thenReturn(target);
    when(receiver.attachments()).thenReturn(mock(Record.class));
    server.handleReceiverOpen(newConnection(Constants.PRINCIPAL_ANONYMOUS), receiver);
    // THEN the server delegates link establishment to the endpoint
    verify(endpoint).onLinkAttach(any(ProtonConnection.class), eq(receiver), eq(targetAddress));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ProtonConnection(io.vertx.proton.ProtonConnection) Target(org.apache.qpid.proton.amqp.transport.Target) AuthorizationService(org.eclipse.hono.service.auth.AuthorizationService) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Record(org.apache.qpid.proton.engine.Record) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)82 Message (org.apache.qpid.proton.message.Message)30 Future (io.vertx.core.Future)24 HttpURLConnection (java.net.HttpURLConnection)22 MessageHelper (org.eclipse.hono.util.MessageHelper)22 ClientErrorException (org.eclipse.hono.client.ClientErrorException)20 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 Handler (io.vertx.core.Handler)18 Map (java.util.Map)18 Span (io.opentracing.Span)17 Buffer (io.vertx.core.buffer.Buffer)17 SpanContext (io.opentracing.SpanContext)16 Constants (org.eclipse.hono.util.Constants)16 Promise (io.vertx.core.Promise)15 Objects (java.util.Objects)14 AsyncResult (io.vertx.core.AsyncResult)13 Vertx (io.vertx.core.Vertx)13 ProtonConnection (io.vertx.proton.ProtonConnection)13 ProtonReceiver (io.vertx.proton.ProtonReceiver)13