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;
}
}
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));
}
Aggregations