protected void requireLogin(RoutingContext context, BiConsumer<RoutingContext, JsonObject> biHandler) { Optional<JsonObject> principal = Optional.ofNullable(context.request().getHeader("user-principal")) .map(JsonObject::new); if (principal.isPresent()) { biHandler.accept(context, principal.get()); } else { context.response() .setStatusCode(401) .end(new JsonObject().put("message", "need_auth").encode()); } }
protected void serviceUnavailable(RoutingContext context, Throwable ex) { context.response().setStatusCode(503) .putHeader("content-type", "application/json") .end(new JsonObject().put("error", ex.getMessage()).encodePrettily()); }
protected void internalError(RoutingContext context, Throwable ex) { context.response().setStatusCode(500) .putHeader("content-type", "application/json") .end(new JsonObject().put("error", ex.getMessage()).encodePrettily()); }
protected void badRequest(RoutingContext context, Throwable ex) { context.response().setStatusCode(400) .putHeader("content-type", "application/json") .end(new JsonObject().put("error", ex.getMessage()).encodePrettily()); }
protected void badGateway(Throwable ex, RoutingContext context) { ex.printStackTrace(); context.response() .setStatusCode(502) .putHeader("content-type", "application/json") .end(new JsonObject().put("error", "bad_gateway") //.put("message", ex.getMessage()) .encodePrettily()); }
protected void notFound(RoutingContext context) { context.response().setStatusCode(404) .putHeader("content-type", "application/json") .end(new JsonObject().put("message", "not_found").encodePrettily()); }
protected void notImplemented(RoutingContext context) { context.response().setStatusCode(501) .putHeader("content-type", "application/json") .end(new JsonObject().put("message", "not_implemented").encodePrettily()); }
protected void serviceUnavailable(RoutingContext context, String cause) { context.response().setStatusCode(503) .putHeader("content-type", "application/json") .end(new JsonObject().put("error", cause).encodePrettily()); }
res.setStatusCode(200).end("{\"count:\": " + v + "}"); }, err -> { res.setStatusCode(400); } else { res.setStatusCode(500);
@Test public void testResponseMissingBody() throws Exception { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().setStatusCode(403).end()); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<Buffer>> single = client .get(8080, "localhost", "/the_uri") .rxSend(); for (int i = 0; i < times; i++) { single.subscribe(resp -> { assertEquals(403, resp.statusCode()); assertNull(resp.body()); complete(); }, this::fail); } }); await(); } finally { server.close(); } }