@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end(new JsonObject().put("time", System.currentTimeMillis()).toString()); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "application/json").end("{\"message\":\"Hello World\"}"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toObservable().subscribe(req -> { req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>"); }); server.listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(routingContext -> { routingContext.response().putHeader("content-type", "text/html").end("Hello World!"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
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 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()); }
protected void notFound(RoutingContext context) { context.response().setStatusCode(404) .putHeader("content-type", "application/json") .end(new JsonObject().put("message", "not_found").encodePrettily()); }
@Test public void testHttpClientFlatMapUnmarshallPojo() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("{\"foo\":\"bar\"}"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); ArrayList<SimplePojo> objects = new ArrayList<>(); req.flatMapObservable(HttpClientResponse::toObservable). lift(io.vertx.rxjava.core.RxHelper.unmarshaller(SimplePojo.class)). forEach( objects::add, err -> fail(), () -> { server.close(); assertEquals(Arrays.asList(new SimplePojo("bar")), objects); testComplete(); }); }); await(); }
@Test public void testPost() { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.bodyHandler(buff -> { assertEquals("onetwothree", buff.toString()); req.response().end(); })); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Observable<Buffer> stream = Observable.just(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three")); Single<HttpResponse<Buffer>> single = client .post(8080, "localhost", "/the_uri") .rxSendStream(stream); for (int i = 0; i < times; i++) { single.subscribe(resp -> complete(), this::fail); } }); await(); } finally { server.close(); } }
@Test public void testHttpClientFlatMap() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); Single<HttpClientResponse> req = client.rxGetNow(8080, "localhost", "/the_uri"); Buffer content = Buffer.buffer(); req.flatMapObservable(HttpClientResponse::toObservable).forEach( content::appendBuffer, err -> fail(), () -> { server.close(); assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); await(); }
@Test public void testHttpClient() { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> { req.response().setChunked(true).end("some_content"); }); try { server.listen(ar -> { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); client.rxGetNow(8080, "localhost", "/the_uri").subscribe(resp -> { Buffer content = Buffer.buffer(); Observable<Buffer> observable = resp.toObservable(); observable.forEach(content::appendBuffer, err -> fail(), () -> { assertEquals("some_content", content.toString("UTF-8")); testComplete(); }); }); }); await(); } finally { server.close(); } }