Example usage for io.vertx.core.eventbus Message body

List of usage examples for io.vertx.core.eventbus Message body

Introduction

In this page you can find the example usage for io.vertx.core.eventbus Message body.

Prototype

@CacheReturn
T body();

Source Link

Document

The body of the message.

Usage

From source file:name.bpdp.vertx.changeme.ChangemeServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }//  w  w w  .j a va  2 s .c  om
    accessed();
    switch (action) {

    case "method1": {
        service.method1((java.lang.String) json.getValue("method1Arg"), createHandler(msg));
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:net.kuujo.copycat.vertx.VertxEventBusProtocolServer.java

License:Apache License

@Override
public void handle(Message<byte[]> message) {
    if (handler != null) {
        handler.apply(ByteBuffer.wrap(message.body())).whenComplete((reply, error) -> {
            context.runOnContext(v -> {
                if (error != null) {
                    message.fail(0, error.getMessage());
                } else {
                    byte[] bytes = new byte[reply.remaining()];
                    reply.get(bytes);//from w  w w  . j a v  a 2  s  . co  m
                    message.reply(bytes);
                }
            });
        });
    } else {
        message.fail(ReplyFailure.NO_HANDLERS.toInt(), "No message handler registered");
    }
}

From source file:net.kuujo.vertigo.message.impl.VertigoMessageImpl.java

License:Apache License

public VertigoMessageImpl(String id, Message<T> message) {
    this.id = id;
    this.body = message.body();
    this.headers = message.headers();
    this.message = message;
}

From source file:org.eclipse.hono.authentication.impl.BaseAuthenticationService.java

License:Open Source License

private void processMessage(final Message<JsonObject> message) {
    final JsonObject body = message.body();
    final String mechanism = body.getString(FIELD_MECHANISM);
    if (!isSupported(mechanism)) {
        replyWithError(message, ERROR_CODE_UNSUPPORTED_MECHANISM, "unsupported SASL mechanism");
    } else {//w w w.j a v a 2  s .  c o  m
        final byte[] response = body.getBinary(FIELD_RESPONSE);
        LOG.debug("received authentication request [mechanism: {}, response: {}]", mechanism,
                response != null ? "*****" : "empty");

        validateResponse(mechanism, response, validation -> {
            if (validation.succeeded()) {
                replyWithAuthorizationId(message, validation.result());
            } else {
                replyWithError(message, ERROR_CODE_AUTHENTICATION_FAILED, validation.cause().getMessage());
            }
        });
    }
}

From source file:org.eclipse.hono.authorization.impl.BaseAuthorizationService.java

License:Open Source License

private void processMessage(final Message<JsonObject> message) {
    final JsonObject body = message.body();
    final String authSubject = body.getString(AUTH_SUBJECT_FIELD);
    final Permission permission = Permission.valueOf(body.getString(PERMISSION_FIELD));
    final ResourceIdentifier resource = ResourceIdentifier.fromString(body.getString(RESOURCE_FIELD));

    boolean hasPermission = hasPermission(authSubject, resource, permission);
    message.reply(hasPermission ? ALLOWED : DENIED);
    LOG.debug("subject [{}] is {}allowed to {} on resource [{}]", authSubject, hasPermission ? "" : "not ",
            permission, resource);//  w  ww. j  av a 2 s . c  om
}

From source file:org.eclipse.hono.service.auth.BaseAuthenticationService.java

License:Open Source License

private void processMessage(final Message<JsonObject> message) {
    final JsonObject body = message.body();
    authenticate(body, validation -> {
        if (validation.succeeded()) {
            message.reply(AuthenticationConstants.getAuthenticationReply(validation.result().getToken()));
        } else {/*from w  w  w .  j  a v  a 2s.  c o  m*/
            message.fail(ERROR_CODE_AUTHENTICATION_FAILED, validation.cause().getMessage());
        }
    });
}

From source file:org.eclipse.hono.service.auth.BaseAuthorizationService.java

License:Open Source License

private void processMessage(final Message<JsonObject> message) {
    final JsonObject body = message.body();
    final String authSubject = body.getString(AuthorizationConstants.AUTH_SUBJECT_FIELD);
    final HonoUser user = new HonoUser() {

        @Override//from   ww  w  .  j  av a2s.c  o m
        public String getName() {
            return authSubject;
        }

        @Override
        public Authorities getAuthorities() {
            return null;
        }

        @Override
        public String getToken() {
            return null;
        }

        @Override
        public boolean isExpired() {
            return false;
        }
    };
    final Activity permission = Activity.valueOf(body.getString(AuthorizationConstants.PERMISSION_FIELD));
    final ResourceIdentifier resource = ResourceIdentifier
            .fromString(body.getString(AuthorizationConstants.RESOURCE_FIELD));

    isAuthorized(user, resource, permission).setHandler(authAttempt -> {
        boolean hasPermission = false;
        if (authAttempt.succeeded()) {
            hasPermission = authAttempt.result();
        }
        LOG.debug("subject [{}] is {}allowed to {} on resource [{}]", authSubject, hasPermission ? "" : "not ",
                permission, resource);
        message.reply(hasPermission ? AuthorizationConstants.ALLOWED : AuthorizationConstants.DENIED);
    });
}

From source file:org.eclipse.hono.service.credentials.BaseCredentialsService.java

License:Open Source License

/**
 * Processes a credentials request message received via the Vertx event bus.
 * /*from www  . j a  v  a2  s .  c o m*/
 * @param regMsg The message.
 */
public final void processCredentialsMessage(final Message<JsonObject> regMsg) {

    final JsonObject body = regMsg.body();
    if (body == null) {
        log.debug("credentials request did not contain body - not supported");
        reply(regMsg, CredentialsResult.from(HTTP_BAD_REQUEST, (JsonObject) null));
        return;
    }

    final String tenantId = body.getString(RequestResponseApiConstants.FIELD_TENANT_ID);
    final String subject = body.getString(MessageHelper.SYS_PROPERTY_SUBJECT);
    final JsonObject payload = getRequestPayload(body);

    if (tenantId == null) {
        log.debug("credentials request did not contain tenantId - not supported");
        reply(regMsg, CredentialsResult.from(HTTP_BAD_REQUEST, (JsonObject) null));
        return;
    } else if (subject == null) {
        log.debug("credentials request did not contain subject - not supported");
        reply(regMsg, CredentialsResult.from(HTTP_BAD_REQUEST, (JsonObject) null));
        return;
    } else if (payload == null) {
        log.debug(
                "credentials request contained invalid or no payload at all (expected json format) - not supported");
        reply(regMsg, CredentialsResult.from(HTTP_BAD_REQUEST, (JsonObject) null));
        return;
    }

    switch (subject) {
    case OPERATION_GET:
        processCredentialsMessageGetOperation(regMsg, tenantId, payload);
        break;
    case OPERATION_ADD:
        processCredentialsMessageAddOperation(regMsg, tenantId, payload);
        break;
    case OPERATION_UPDATE:
        processCredentialsMessageUpdateOperation(regMsg, tenantId, payload);
        break;
    case OPERATION_REMOVE:
        processCredentialsMessageRemoveOperation(regMsg, tenantId, payload);
        break;
    default:
        log.debug("operation [{}] not supported", subject);
        reply(regMsg, CredentialsResult.from(HTTP_BAD_REQUEST, (JsonObject) null));
    }
}

From source file:org.eclipse.hono.service.credentials.BaseCredentialsService.java

License:Open Source License

/**
 * Sends a response to a credentials request over the Vertx event bus.
 * // w ww .  j a va  2 s  . c  o m
 * @param request The message to respond to.
 * @param result The credentials result that should be conveyed in the response.
 */
protected final void reply(final Message<JsonObject> request, final CredentialsResult<JsonObject> result) {

    final JsonObject body = request.body();
    final String tenantId = body.getString(RequestResponseApiConstants.FIELD_TENANT_ID);
    final String deviceId = body.getString(RequestResponseApiConstants.FIELD_DEVICE_ID);

    request.reply(CredentialsConstants.getServiceReplyAsJson(tenantId, deviceId, result));
}

From source file:org.eclipse.hono.service.EventBusService.java

License:Open Source License

private void processRequestMessage(final Message<JsonObject> msg) {

    if (log.isTraceEnabled()) {
        log.trace("received request message: {}", msg.body().encodePrettily());
    }//w w w  . j  a va2s  .c om

    final EventBusMessage request = EventBusMessage.fromJson(msg.body());
    processRequest(request).recover(t -> {
        log.debug("cannot process request [operation: {}]: {}", request.getOperation(), t.getMessage());
        final int status = Optional.of(t).map(cause -> {
            if (cause instanceof ServiceInvocationException) {
                return ((ServiceInvocationException) cause).getErrorCode();
            } else {
                return null;
            }
        }).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
        return Future.succeededFuture(request.getResponse(status));
    }).map(response -> {
        if (response.getReplyToAddress() == null) {
            log.debug("sending response as direct reply to request [operation: {}]", request.getOperation());
            msg.reply(response.toJson());
        } else if (response.hasResponseProperties()) {
            log.debug("sending response [operation: {}, reply-to: {}]", request.getOperation(),
                    request.getReplyToAddress());
            vertx.eventBus().send(request.getReplyToAddress(), response.toJson());
        } else {
            log.warn("discarding response lacking correlation ID or operation");
        }
        return null;
    });
}