Search in sources :

Example 11 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project hono by eclipse.

the class EventBusAuthenticationService method authenticate.

@Override
public void authenticate(final JsonObject authRequest, final Handler<AsyncResult<HonoUser>> authenticationResultHandler) {
    final DeliveryOptions options = new DeliveryOptions().setSendTimeout(AUTH_REQUEST_TIMEOUT_MILLIS);
    vertx.eventBus().request(AuthenticationConstants.EVENT_BUS_ADDRESS_AUTHENTICATION_IN, authRequest, options, reply -> {
        if (reply.succeeded()) {
            final JsonObject result = (JsonObject) reply.result().body();
            final String token = result.getString(AuthenticationConstants.FIELD_TOKEN);
            log.debug("received token [length: {}] in response to authentication request", token.length());
            try {
                final Jws<Claims> expandedToken = tokenValidator.expand(result.getString(AuthenticationConstants.FIELD_TOKEN));
                authenticationResultHandler.handle(Future.succeededFuture(new HonoUserImpl(expandedToken, token)));
            } catch (final JwtException e) {
                authenticationResultHandler.handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, e)));
            }
        } else {
            final ServiceInvocationException resultException;
            if (reply.cause() instanceof ReplyException) {
                switch(((ReplyException) reply.cause()).failureType()) {
                    case TIMEOUT:
                        log.debug("timeout processing authentication request", reply.cause());
                        resultException = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, reply.cause());
                        break;
                    case NO_HANDLERS:
                        log.debug("could not process authentication request", reply.cause());
                        resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause());
                        break;
                    case RECIPIENT_FAILURE:
                        final int statusCode = ((ReplyException) reply.cause()).failureCode();
                        if (statusCode < 400 || statusCode >= 600) {
                            log.error("got illegal status code in authentication response exception: {}", statusCode);
                            resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause().getMessage());
                        } else {
                            resultException = StatusCodeMapper.from(statusCode, reply.cause().getMessage());
                        }
                        break;
                    default:
                        resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause());
                }
            } else {
                resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause());
            }
            authenticationResultHandler.handle(Future.failedFuture(resultException));
        }
    });
}
Also used : Claims(io.jsonwebtoken.Claims) JsonObject(io.vertx.core.json.JsonObject) JwtException(io.jsonwebtoken.JwtException) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 12 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project VX-API-Gateway by EliMirren.

the class ClientVerticle method deployAPP.

/**
 * 启动应用
 *
 * @param rct
 */
public void deployAPP(RoutingContext rct) {
    String name = rct.request().getParam("name");
    LOG.info("执行启动应用-->" + name + "...");
    HttpServerResponse response = rct.response().putHeader(CONTENT_TYPE, CONTENT_VALUE_JSON_UTF8);
    if (StrUtil.isNullOrEmpty(name)) {
        response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C1404));
    } else {
        vertx.eventBus().<JsonObject>send(thisVertxName + VxApiEventBusAddressConstant.GET_APP, name, body -> {
            if (body.succeeded()) {
                if (body.result().body().isEmpty()) {
                    response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C1404));
                } else {
                    JsonObject app = new JsonObject(body.result().body().getString("content"));
                    JsonObject config = new JsonObject();
                    config.put("app", app);
                    config.put("appName", name);
                    vertx.eventBus().<String>send(thisVertxName + VxApiEventBusAddressConstant.DEPLOY_APP_DEPLOY, config, deploy -> {
                        if (deploy.succeeded()) {
                            LOG.info("启动应用-->" + name + ":成功!");
                            response.end(ResultFormat.formatAsOne(HTTPStatusCodeMsgEnum.C200));
                            if (vertx.isClustered()) {
                                vertx.eventBus().publish(VxApiEventBusAddressConstant.DEPLOY_APP_DEPLOY, config.copy().put("thisVertxName", thisVertxName));
                                LOG.info("广播告诉集群环境中启动应用:" + name);
                            }
                        } else {
                            LOG.error("启动应用-->" + name + " 失败:" + deploy.cause());
                            HTTPStatusCodeMsgEnum msgCode = HTTPStatusCodeMsgEnum.C500;
                            if (deploy.cause() != null && deploy.cause() instanceof ReplyException) {
                                ReplyException cause = (ReplyException) deploy.cause();
                                if (cause.failureCode() == 1111) {
                                    msgCode = HTTPStatusCodeMsgEnum.C1111;
                                }
                            }
                            response.end(ResultFormat.formatAsZero(msgCode));
                        }
                    });
                }
            } else {
                LOG.error("启动应用-->" + name + " 失败:" + body.cause());
                System.out.println("启动应用-->" + name + " 失败:" + body.cause());
                response.end(ResultFormat.formatAsZero(HTTPStatusCodeMsgEnum.C500));
            }
        });
    }
}
Also used : HTTPStatusCodeMsgEnum(com.szmirren.vxApi.core.enums.HTTPStatusCodeMsgEnum) HttpServerResponse(io.vertx.core.http.HttpServerResponse) JsonObject(io.vertx.core.json.JsonObject) ReplyException(io.vertx.core.eventbus.ReplyException)

Aggregations

ReplyException (io.vertx.core.eventbus.ReplyException)12 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 EventBus (io.vertx.core.eventbus.EventBus)3 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)3 CharsetUtil (io.netty.util.CharsetUtil)2 AbstractVerticle (io.vertx.core.AbstractVerticle)2 Context (io.vertx.core.Context)2 DeploymentOptions (io.vertx.core.DeploymentOptions)2 Future (io.vertx.core.Future)2 Verticle (io.vertx.core.Verticle)2 Buffer (io.vertx.core.buffer.Buffer)2 MessageCodec (io.vertx.core.eventbus.MessageCodec)2 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Consumer (java.util.function.Consumer)2 CoreMatchers (org.hamcrest.CoreMatchers)2 Test (org.junit.Test)2