diff --git a/agent-testweb/vertx-3-plugin-testweb/pom.xml b/agent-testweb/vertx-3-plugin-testweb/pom.xml index d69b3997bbd1..ca5a3172822e 100644 --- a/agent-testweb/vertx-3-plugin-testweb/pom.xml +++ b/agent-testweb/vertx-3-plugin-testweb/pom.xml @@ -50,6 +50,11 @@ vertx-web ${vertx.version} + + com.j2html + j2html + 1.6.0 + diff --git a/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java b/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java new file mode 100644 index 000000000000..fdf2c1a2e703 --- /dev/null +++ b/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java @@ -0,0 +1,55 @@ +package com.pinpoint.test.plugin; + +import io.vertx.ext.web.Route; +import j2html.tags.specialized.ATag; +import j2html.tags.specialized.HtmlTag; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import static j2html.TagCreator.a; +import static j2html.TagCreator.body; +import static j2html.TagCreator.div; +import static j2html.TagCreator.each; +import static j2html.TagCreator.h1; +import static j2html.TagCreator.head; +import static j2html.TagCreator.html; +import static j2html.TagCreator.title; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ApiLinkPage { + private final String title; + + public ApiLinkPage(String title) { + this.title = Objects.requireNonNull(title, "title"); + } + + public String buildRoute(List routes) { + List collect = routes.stream() + .map(this::aTag) + .collect(Collectors.toList()); + return buildHtml(collect); + } + + private ATag aTag(Route route) { + return a() + .withText(route.getPath()) + .withHref(route.getPath()) + .withTarget("_blank"); + } + + public String buildHtml(List aTags) { + + HtmlTag html = html( + head(title(title)), + body( + h1(title), + each(aTags, aTag -> div(aTag)) + ) + ); + return html.render(); + } +} diff --git a/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/VertxPluginTestStarter.java b/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/VertxPluginTestStarter.java index 2f163901a4dc..bbdd2f5d4060 100644 --- a/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/VertxPluginTestStarter.java +++ b/agent-testweb/vertx-3-plugin-testweb/src/main/java/com/pinpoint/test/plugin/VertxPluginTestStarter.java @@ -21,11 +21,16 @@ import io.vertx.core.Promise; import io.vertx.core.http.HttpClient; import io.vertx.core.http.HttpClientResponse; +import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerRequest; +import io.vertx.core.http.HttpServerResponse; +import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; +import io.vertx.ext.web.RoutingContext; +import java.util.List; import java.util.concurrent.TimeUnit; public class VertxPluginTestStarter extends AbstractVerticle { @@ -35,10 +40,18 @@ public void start(Promise startPromise) throws Exception { HttpServerOptions options = new HttpServerOptions(); options.setIdleTimeout(1000); Router router = Router.router(vertx); - router.get("/").handler(routingContext -> { - routingContext.response().end("Welcome pinpoint vert.x HTTP server test."); + redirect(routingContext, "/main"); + }); + router.get("/reroute").handler(routingContext -> { + routingContext.reroute("/main"); + }); + router.get("/main").handler(routingContext -> { + List routes = router.getRoutes(); + routingContext.response().end(buildMain("Welcome pinpoint vert.x HTTP server test", routes)); }); + + router.get("/request").handler(routingContext -> { request(80, "naver.com", "/"); routingContext.response().end("Request http://naver.com:80/"); @@ -73,6 +86,12 @@ public void start(Promise startPromise) throws Exception { router.get("/runOnContext/request").handler(routingContext -> { runOnContextRequest(routingContext.request()); }); + router.get("/runOnContext/error").handler(routingContext -> { + vertx.runOnContext(aVoid -> { + throw new RuntimeException("/runOnContext/error"); + }); + }); + router.get("/test/:arg1/*").handler(routingContext -> { String arg1 = routingContext.pathParam("arg1"); routingContext.response().end(arg1); @@ -82,6 +101,7 @@ public void start(Promise startPromise) throws Exception { routingContext.response().end(arg1); }); + vertx.createHttpServer().requestHandler(router).listen(18080, http -> { if (http.succeeded()) { startPromise.complete(); @@ -92,6 +112,19 @@ public void start(Promise startPromise) throws Exception { }); } + private static void redirect(RoutingContext routingContext, String redirectUrl) { + HttpServerResponse response = routingContext.response(); + response.putHeader(HttpHeaders.LOCATION, redirectUrl); + response.setStatusCode(302); + response.putHeader(HttpHeaders.CONTENT_TYPE, "text/plain; charset=utf-8"); + response.end("Redirecting to " + redirectUrl); + } + + private String buildMain(String title, List routes) { + return new ApiLinkPage(title) + .buildRoute(routes); + } + private void executeBlocking(HttpServerRequest request, final int waitSeconds) { vertx.executeBlocking(new Handler>() { @Override @@ -153,4 +186,4 @@ public void handle(Void aVoid) { } }).end(); } -} \ No newline at end of file +} diff --git a/agent-testweb/vertx-4-plugin-testweb/pom.xml b/agent-testweb/vertx-4-plugin-testweb/pom.xml index 4b355ed585c2..f6ce577b72cd 100644 --- a/agent-testweb/vertx-4-plugin-testweb/pom.xml +++ b/agent-testweb/vertx-4-plugin-testweb/pom.xml @@ -56,6 +56,11 @@ vertx-web-client ${vertx.version} + + com.j2html + j2html + 1.6.0 + diff --git a/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java b/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java new file mode 100644 index 000000000000..fdf2c1a2e703 --- /dev/null +++ b/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/ApiLinkPage.java @@ -0,0 +1,55 @@ +package com.pinpoint.test.plugin; + +import io.vertx.ext.web.Route; +import j2html.tags.specialized.ATag; +import j2html.tags.specialized.HtmlTag; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import static j2html.TagCreator.a; +import static j2html.TagCreator.body; +import static j2html.TagCreator.div; +import static j2html.TagCreator.each; +import static j2html.TagCreator.h1; +import static j2html.TagCreator.head; +import static j2html.TagCreator.html; +import static j2html.TagCreator.title; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ApiLinkPage { + private final String title; + + public ApiLinkPage(String title) { + this.title = Objects.requireNonNull(title, "title"); + } + + public String buildRoute(List routes) { + List collect = routes.stream() + .map(this::aTag) + .collect(Collectors.toList()); + return buildHtml(collect); + } + + private ATag aTag(Route route) { + return a() + .withText(route.getPath()) + .withHref(route.getPath()) + .withTarget("_blank"); + } + + public String buildHtml(List aTags) { + + HtmlTag html = html( + head(title(title)), + body( + h1(title), + each(aTags, aTag -> div(aTag)) + ) + ); + return html.render(); + } +} diff --git a/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/Vertx4PluginTestStarter.java b/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/Vertx4PluginTestStarter.java index 18955cd30be2..a229dc8d2439 100644 --- a/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/Vertx4PluginTestStarter.java +++ b/agent-testweb/vertx-4-plugin-testweb/src/main/java/com/pinpoint/test/plugin/Vertx4PluginTestStarter.java @@ -22,10 +22,11 @@ import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerRequest; import io.vertx.core.net.SelfSignedCertificate; +import io.vertx.ext.web.Route; import io.vertx.ext.web.Router; -import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.client.WebClient; +import java.util.List; import java.util.concurrent.TimeUnit; public class Vertx4PluginTestStarter extends AbstractVerticle { @@ -41,13 +42,18 @@ public void start(Promise startPromise) throws Exception { Router router = Router.router(vertx); - router.get("/").handler(new Handler() { - @Override - public void handle(RoutingContext routingContext) { - System.out.println("## WELCOME"); - routingContext.response().end("Welcome pinpoint vert.x HTTP server test."); - } + router.get("/").handler(routingContext -> { + routingContext.redirect("/main"); + }); + router.get("/reroute").handler(routingContext -> { + routingContext.reroute("/main"); + }); + router.get("/main").handler(routingContext -> { + List routes = router.getRoutes(); + routingContext.response().end(buildMain("Welcome pinpoint vert.x HTTP server test", routes)); }); + + router.get("/request").handler(routingContext -> { request(80, "naver.com", "/"); routingContext.response().end("Request http://naver.com:80/"); @@ -109,6 +115,11 @@ public void handle(RoutingContext routingContext) { }); } + private String buildMain(String title, List routes) { + return new ApiLinkPage(title) + .buildRoute(routes); + } + private void executeBlocking(HttpServerRequest request, final int waitSeconds) { vertx.executeBlocking(new Handler>() { @Override