@Override public void start() throws Exception { HttpServer server = vertx.createHttpServer(); server.requestStream().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().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().toFlowable().subscribe(req -> { HttpServerResponse resp = req.response(); String contentType = req.getHeader("Content-Type"); if (contentType != null) { resp.putHeader("Content-Type", contentType); } resp.setChunked(true); req.toFlowable().subscribe( resp::write, err -> {}, resp::end ); }); server.listen(8080); } }
@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.route().handler(req -> req.response().putHeader("content-type", "text/html") .end("<html><body><h1>Hello from vert.x!</h1></body></html>")); HttpServer server = vertx.createHttpServer(); server.requestStream() .toFlowable() .map(HttpServerRequest::pause) .onBackpressureDrop(req -> req.response().setStatusCode(503).end()) .observeOn(RxHelper.scheduler(vertx.getDelegate())) .subscribe(req -> { req.resume(); router.accept(req); }); server.rxListen(PORT).subscribe(res -> generateRequests()); }
private void routeSafe(HttpServerRequest req, Router router) { try { router.accept(req); } catch (IllegalArgumentException ex) { LOGGER.warn("Problem decoding Query String ", ex); req.response() .setStatusCode(BAD_REQUEST.code()) .setStatusMessage(BAD_REQUEST.reasonPhrase()) .end("Invalid characters in Query Parameter"); } } }
private void routeSafe(HttpServerRequest req, Router router) { try { router.accept(req); } catch (IllegalArgumentException ex) { LOGGER.warn("Problem decoding Query String ", ex); req.response() .setStatusCode(BAD_REQUEST.code()) .setStatusMessage(BAD_REQUEST.reasonPhrase()) .end("Invalid characters in Query Parameter"); } } }
@Override public void start() { ServiceDiscovery.create(vertx, discovery -> { this.discovery = discovery; // Create the service object PortfolioServiceImpl service = new PortfolioServiceImpl(vertx, discovery, config().getDouble("money", 10000.00)); // Register the service proxy on the event bus ProxyHelper.registerService(PortfolioService.class, vertx.getDelegate(), service, ADDRESS); Record record = EventBusService.createRecord("portfolio", ADDRESS, PortfolioService.class.getName()); discovery.publish(record, ar -> { if (ar.succeeded()) { this.record = record; System.out.println("Portfolio service published"); // Used for health check vertx.createHttpServer().requestHandler(req -> req.response().end("OK")).listen(8080); } else { ar.cause().printStackTrace(); } }); }); }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().end(expected.encode())); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<WineAndCheese>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.json(WineAndCheese.class)) .rxSend(); single.subscribe(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); }, this::fail); }); await(); } finally { server.close(); } }
server.requestStream().toFlowable() .doOnNext(request -> { HttpServerResponse response = request.response() .putHeader("content-type", "application/json");
@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(); } }
ResteasyUriInfo uriInfo = VertxUtil.extractUriInfo(request.getDelegate(), servletMappingPrefix); ResteasyHttpHeaders headers = VertxUtil.extractHttpHeaders(request.getDelegate()); HttpServerResponse response = request.response(); VertxHttpResponse vertxResponse = new VertxHttpResponseWithWorkaround(response.getDelegate(), dispatcher.getProviderFactory(), request.method()); VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx.getDelegate(), headers, uriInfo, request.rawMethod(), dispatcher.getDispatcher(), vertxResponse, false);
ResteasyUriInfo uriInfo = VertxUtil.extractUriInfo(request.getDelegate(), servletMappingPrefix); ResteasyHttpHeaders headers = VertxUtil.extractHttpHeaders(request.getDelegate()); HttpServerResponse response = request.response(); VertxHttpResponse vertxResponse = new VertxHttpResponseWithWorkaround(response.getDelegate(), dispatcher.getProviderFactory(), request.method()); VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx.getDelegate(), headers, uriInfo, request.rawMethod(), dispatcher.getDispatcher(), vertxResponse, false);
options.getBackpressureStrategy()) .onBackpressureDrop( req -> req.response().setStatusCode(options.getDropRequestResponseCode()).end()) .observeOn(RxHelper.scheduler(vertx.getDelegate())) .subscribe(req -> {
@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(); } }
@Test public void testGet() { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.response().setChunked(true).end("some_content")); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Single<HttpResponse<Buffer>> single = client .get(8080, "localhost", "/the_uri") .as(BodyCodec.buffer()) .rxSend(); for (int i = 0; i < times; i++) { single.subscribe(resp -> { Buffer body = resp.body(); assertEquals("some_content", body.toString("UTF-8")); complete(); }, this::fail); } }); await(); } finally { server.close(); } }