Skip to content

Commit

Permalink
[#9504] Add Vert.x main page for test
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 14, 2022
1 parent b4cc7d9 commit fc13b07
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 10 deletions.
5 changes: 5 additions & 0 deletions agent-testweb/vertx-3-plugin-testweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>com.j2html</groupId>
<artifactId>j2html</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Route> routes) {
List<ATag> 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<ATag> aTags) {

HtmlTag html = html(
head(title(title)),
body(
h1(title),
each(aTags, aTag -> div(aTag))
)
);
return html.render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,10 +40,18 @@ public void start(Promise<Void> 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<Route> 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/");
Expand Down Expand Up @@ -73,6 +86,12 @@ public void start(Promise<Void> 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);
Expand All @@ -82,6 +101,7 @@ public void start(Promise<Void> startPromise) throws Exception {
routingContext.response().end(arg1);
});


vertx.createHttpServer().requestHandler(router).listen(18080, http -> {
if (http.succeeded()) {
startPromise.complete();
Expand All @@ -92,6 +112,19 @@ public void start(Promise<Void> 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<Route> routes) {
return new ApiLinkPage(title)
.buildRoute(routes);
}

private void executeBlocking(HttpServerRequest request, final int waitSeconds) {
vertx.executeBlocking(new Handler<Promise<Object>>() {
@Override
Expand Down Expand Up @@ -153,4 +186,4 @@ public void handle(Void aVoid) {
}
}).end();
}
}
}
5 changes: 5 additions & 0 deletions agent-testweb/vertx-4-plugin-testweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>vertx-web-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>com.j2html</groupId>
<artifactId>j2html</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Route> routes) {
List<ATag> 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<ATag> aTags) {

HtmlTag html = html(
head(title(title)),
body(
h1(title),
each(aTags, aTag -> div(aTag))
)
);
return html.render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,13 +42,18 @@ public void start(Promise<Void> startPromise) throws Exception {

Router router = Router.router(vertx);

router.get("/").handler(new Handler<RoutingContext>() {
@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<Route> 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/");
Expand Down Expand Up @@ -109,6 +115,11 @@ public void handle(RoutingContext routingContext) {
});
}

private String buildMain(String title, List<Route> routes) {
return new ApiLinkPage(title)
.buildRoute(routes);
}

private void executeBlocking(HttpServerRequest request, final int waitSeconds) {
vertx.executeBlocking(new Handler<Promise<Object>>() {
@Override
Expand Down

0 comments on commit fc13b07

Please sign in to comment.