protected <T> CompletableFuture<T> callWechatService(String url, String param, Class<T> clazz) { CompletableFuture<T> ret = new CompletableFuture<>(); client.get(url + "?" + param).submit() .thenAccept(res -> { log.info("call wechat service -> {}, {}, {}, {}", url, param, res.getStatus(), res.getStringBody()); complete(ret, res, clazz); }); return ret; }
protected CompletableFuture<List<ByteBuffer>> postAndReturnBinaryData(String url, String param, Object data) { CompletableFuture<List<ByteBuffer>> ret = new CompletableFuture<>(); client.post(url + "?" + param).jsonBody(data).submit() .thenAccept(res -> { if (res.getStatus() == HttpStatus.OK_200) { if (res.getJsonObjectBody().getInteger("errcode") != 0) { ErrorResponse errorResponse = res.getJsonBody(ErrorResponse.class); ret.completeExceptionally(errorResponse); } else { ret.complete(res.getResponseBody()); } } else { ErrorResponse errorResponse = res.getJsonBody(ErrorResponse.class); ret.completeExceptionally(errorResponse); } }); return ret; }
protected <T> CompletableFuture<T> postWechatService(String url, String param, Object data, Class<T> clazz) { CompletableFuture<T> ret = new CompletableFuture<>(); client.post(url + "?" + param).jsonBody(data).submit() .thenAccept(res -> { log.info("call wechat service -> {}, {}, {}, {}, {}", url, param, data, res.getStatus(), res.getStringBody()); complete(ret, res, clazz); }); return ret; }
srcRequest.content(srcBuffer -> outputCompletable.thenAccept(dstOutput -> { try { if (count.addAndGet(srcBuffer.remaining()) < contentLength) { outputCompletable.thenAccept($.io::close);
for (int i = 0; i < maxGetSession; i++) { $.httpsClient().get(uri + "/session/foo").cookies(cookies).submit() .thenAccept(res2 -> { String sessionFoo = res2.getStringBody(); System.out.println(sessionFoo); .thenAccept(res -> { System.out.println(res.getStatus()); System.out.println(res.getStringBody()); .thenAccept(res -> { String sessionFoo = res.getStringBody(); System.out.println(sessionFoo);
.addFieldPart("test2", new StringContentProvider("hello multi part2"), null) .submit() .thenAccept(res -> { System.out.println(res.getStringBody()); phaser.arrive(); .addFilePart("poetry", "poem.txt", inputStreamContentProvider, null) .submit() .thenAccept(res -> { System.out.println(res.getStringBody()); $.io.close(inputStreamContentProvider);
p.thenAccept(output -> { try (HTTPOutputStream out = output) { for (ByteBuffer buf : reqBuilder.multiPartProvider) {
p.thenAccept(output -> { try (HTTPOutputStream out = output) { for (ByteBuffer buf : reqBuilder.multiPartProvider) {
.get($.string.replace("http://{}:{}/product/20", host, port)) .submit() .thenAccept(resp -> System.out.println(resp.getStringBody())); .body("Car 20. The color is red.") .submit() .thenAccept(resp -> System.out.println(resp.getStringBody())); .body("Change the color from red to black.") .submit() .thenAccept(resp -> System.out.println(resp.getStringBody())); .thenAccept(resp -> System.out.println(resp.getStringBody()));
connFuture.thenAccept(conn -> { String leakMessage = StringUtils.replace( "The Firefly HTTP client connection leaked. id -> {}, host -> {}:{}",
connFuture.thenAccept(conn -> { String leakMessage = StringUtils.replace( "The Firefly HTTP client connection leaked. id -> {}, host -> {}:{}",
.jsonBody(car) .submit() .thenAccept(resp -> System.out.println(resp.getStringBody())); .jsonBody(car) .submit() .thenAccept(resp -> System.out.println(resp.getStringBody()));
public static void main(String[] args) { Phaser phaser = new Phaser(2); HTTP2ServerBuilder server = $.httpServer(); server.router().post("/task/create") .produces("application/json").consumes("*/json") .handler(ctx -> { Map<String, Object> ret = new HashMap<>(); ret.put("msg", "create task, " + ctx.getJsonBody(Task.class) + " success "); ret.put("code", 0); ctx.put(HttpHeader.CONTENT_TYPE, MimeTypes.Type.APPLICATION_JSON_UTF_8.asString()) .end($.json.toJson(ret)); }).listen("localhost", 8080); Task task = new Task(); task.name = "TODO today"; task.date = new Date(); $.httpClient().post("http://localhost:8080/task/create") .put(HttpHeader.ACCEPT, "text/plain; q=0.9, application/json") .jsonBody(task).submit() .thenAccept(res -> { System.out.println(res.getJsonObjectBody()); phaser.arrive(); }); phaser.arriveAndAwaitAdvance(); server.stop(); $.httpClient().stop(); } }
public static void main(String[] args) { String host = "localhost"; int port = 8081; $.httpServer() .router().get("/product*") .handler(ctx -> { String matched = ctx.getWildcardMatchedResult(0); ctx.write("Intercept the product: " + matched + "\r\n").next(); }) .router().get("/product/:type") .handler(ctx -> { String type = ctx.getPathParameter("type"); ctx.end("List " + type + "\r\n"); }) .listen(host, port); $.httpClient().get($.string.replace("http://{}:{}/product/apple", host, port)) .submit() .thenAccept(resp -> { System.out.println(resp.getStatus()); System.out.println(resp.getStringBody()); }); } }
public static void main(String[] args) { String host = "localhost"; int port = 8081; $.httpServer() .router().method(HttpMethod.GET).pathRegex("/product(.*)") .handler(ctx -> { String matched = ctx.getRegexGroup(1); ctx.write("Intercept the product: " + matched + "\r\n").next(); }) .router().get("/product/:type") .handler(ctx -> { String type = ctx.getPathParameter("type"); ctx.end("List " + type + "\r\n"); }) .listen(host, port); $.httpClient().get($.string.replace("http://{}:{}/product/orange", host, port)) .submit() .thenAccept(resp -> { System.out.println(resp.getStatus()); System.out.println(resp.getStringBody()); }); } }
public static void main(String[] args) { String host = "localhost"; int port = 8081; $.httpServer() .router().put("/product/:id").consumes("*/json") .handler(ctx -> { String id = ctx.getPathParameter("id"); String type = ctx.getWildcardMatchedResult(0); Car car = ctx.getJsonBody(Car.class); ctx.end($.string.replace("Update resource {}: {}. The content type is {}/json", id, car, type)); }) .listen(host, port); Car car = new Car(); car.id = 20L; car.name = "My car"; car.color = "black"; $.httpClient().put($.string.replace("http://{}:{}/product/20", host, port)) .jsonBody(car) .submit() .thenAccept(resp -> System.out.println(resp.getStringBody())); } }
public static void main(String[] args) { Phaser phaser = new Phaser(3); HTTP2ServerBuilder server = $.httpServer(); server.router().path("/all-methods") .handler(ctx -> ctx.end("the HTTP method: " + ctx.getMethod())) .listen("localhost", 8080); $.httpClient().post("http://localhost:8080/all-methods").submit() .thenAccept(res -> { System.out.println(res.getStringBody()); phaser.arrive(); }); $.httpClient().put("http://localhost:8080/all-methods").submit() .thenAccept(res -> { System.out.println(res.getStringBody()); phaser.arrive(); }); phaser.arriveAndAwaitAdvance(); server.stop(); $.httpClient().stop(); } }
public static void main(String[] args) { String host = "localhost"; int port = 8081; $.httpServer().router().get("/product/:id") .handler(ctx -> { String id = ctx.getRouterParameter("id"); ctx.end($.string.replace("Get the product {}", id)); }).listen(host, port); $.httpClient() .get($.string.replace("http://{}:{}/product/20", host, port)) .submit() .thenAccept(resp -> { System.out.println(resp.getStatus()); System.out.println(resp.getStringBody()); }); } }
public static void main(String[] args) { Phaser phaser = new Phaser(3); HTTP2ServerBuilder httpServer = $.httpServer(); httpServer.router().get("/").handler(ctx -> ctx.write("hello world! ").next()) .router().get("/").handler(ctx -> ctx.end("end message")) .listen("localhost", 8080); $.httpClient().get("http://localhost:8080/").submit() .thenAccept(res -> System.out.println(res.getStringBody())) .thenAccept(res -> phaser.arrive()); $.httpClient().get("http://localhost:8080/hello").submit() .thenAccept(res -> System.out.println(res.getStringBody())) .thenAccept(res -> phaser.arrive()); phaser.arriveAndAwaitAdvance(); httpServer.stop(); $.httpClient().stop(); } }
public static void main(String[] args) { Phaser phaser = new Phaser(2); HTTP2ServerBuilder httpServer = $.httpsServer(); httpServer.router().get("/").handler(ctx -> ctx.write("hello world! ").next()) .router().get("/").handler(ctx -> ctx.end("end message")) .listen("localhost", 8081); $.httpsClient().get("https://localhost:8081/").submit() .thenAccept(res -> System.out.println(res.getStringBody())) .thenAccept(res -> phaser.arrive()); phaser.arriveAndAwaitAdvance(); httpServer.stop(); $.httpsClient().stop(); } }