Search in sources :

Example 16 with ProtonReceiver

use of io.vertx.proton.ProtonReceiver in project hono by eclipse.

the class RequestResponseEndpoint method handleMessage.

/**
 * Handles a request message received from a client.
 * <p>
 * The message gets rejected if
 * <ul>
 * <li>the message does not pass {@linkplain #passesFormalVerification(ResourceIdentifier, Message) formal verification}
 * or</li>
 * <li>the client is not {@linkplain #isAuthorized(HonoUser, ResourceIdentifier, Message) authorized to execute the operation}
 * indicated by the message's <em>subject</em> or</li>
 * <li>its payload cannot be parsed</li>
 * </ul>
 *
 * @param con The connection with the client.
 * @param receiver The link over which the message has been received.
 * @param targetAddress The address the message is sent to.
 * @param delivery The message's delivery status.
 * @param message The message.
 */
protected final void handleMessage(final ProtonConnection con, final ProtonReceiver receiver, final ResourceIdentifier targetAddress, ProtonDelivery delivery, Message message) {
    final Future<Void> formalCheck = Future.future();
    if (passesFormalVerification(targetAddress, message)) {
        formalCheck.complete();
    } else {
        formalCheck.fail(new AmqpErrorException(AmqpError.DECODE_ERROR, "malformed payload"));
    }
    final HonoUser clientPrincipal = Constants.getClientPrincipal(con);
    formalCheck.compose(ok -> isAuthorized(clientPrincipal, targetAddress, message)).compose(authorized -> {
        logger.debug("client [{}] is {}authorized to {}:{}", clientPrincipal.getName(), authorized ? "" : "not ", targetAddress, message.getSubject());
        if (authorized) {
            try {
                processRequest(message, targetAddress, clientPrincipal);
                ProtonHelper.accepted(delivery, true);
                return Future.succeededFuture();
            } catch (DecodeException e) {
                return Future.failedFuture(new AmqpErrorException(AmqpError.DECODE_ERROR, "malformed payload"));
            }
        } else {
            return Future.failedFuture(new AmqpErrorException(AmqpError.UNAUTHORIZED_ACCESS, "unauthorized"));
        }
    }).otherwise(t -> {
        if (t instanceof AmqpErrorException) {
            AmqpErrorException cause = (AmqpErrorException) t;
            MessageHelper.rejected(delivery, cause.asErrorCondition());
        } else {
            logger.debug("error processing request [resource: {}, op: {}]: {}", targetAddress, message.getSubject(), t.getMessage());
            MessageHelper.rejected(delivery, ProtonHelper.condition(AmqpError.INTERNAL_ERROR, "internal error"));
        }
        return null;
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) AmqpErrorException(org.eclipse.hono.util.AmqpErrorException) ProtonDelivery(io.vertx.proton.ProtonDelivery) DecodeException(io.vertx.core.json.DecodeException) Autowired(org.springframework.beans.factory.annotation.Autowired) EventBusMessage(org.eclipse.hono.util.EventBusMessage) HonoUser(org.eclipse.hono.auth.HonoUser) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Constants(org.eclipse.hono.util.Constants) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) Vertx(io.vertx.core.Vertx) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) Optional(java.util.Optional) ProtonSender(io.vertx.proton.ProtonSender) ClaimsBasedAuthorizationService(org.eclipse.hono.service.auth.ClaimsBasedAuthorizationService) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) AuthorizationService(org.eclipse.hono.service.auth.AuthorizationService) HonoUser(org.eclipse.hono.auth.HonoUser) AmqpErrorException(org.eclipse.hono.util.AmqpErrorException) DecodeException(io.vertx.core.json.DecodeException)

Example 17 with ProtonReceiver

use of io.vertx.proton.ProtonReceiver 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.Test)

Aggregations

ProtonReceiver (io.vertx.proton.ProtonReceiver)17 ProtonConnection (io.vertx.proton.ProtonConnection)9 Test (org.junit.Test)9 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)7 Handler (io.vertx.core.Handler)5 Vertx (io.vertx.core.Vertx)4 ProtonDelivery (io.vertx.proton.ProtonDelivery)4 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)4 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)4 Message (org.apache.qpid.proton.message.Message)4 RecordImpl (org.apache.qpid.proton.engine.impl.RecordImpl)3 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)3 AuthorizationService (org.eclipse.hono.service.auth.AuthorizationService)3 AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Async (io.vertx.ext.unit.Async)2 ProtonHelper (io.vertx.proton.ProtonHelper)2 ProtonQoS (io.vertx.proton.ProtonQoS)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Source (org.apache.qpid.proton.amqp.transport.Source)2