diff --git a/simple-java-dedup/.dockerignore b/simple-java-dedup/.dockerignore index b482b67b..deef71cd 100644 --- a/simple-java-dedup/.dockerignore +++ b/simple-java-dedup/.dockerignore @@ -1,5 +1,6 @@ ** !target/simple-java-dedup.jar +!target/lib/** !target/classes/** !target/keploy-sdk.jar !target/jacocoagent.jar diff --git a/simple-java-dedup/Dockerfile b/simple-java-dedup/Dockerfile index 454e5d55..04d29135 100644 --- a/simple-java-dedup/Dockerfile +++ b/simple-java-dedup/Dockerfile @@ -7,6 +7,7 @@ RUN groupadd --gid 10001 appuser \ && useradd --uid 10001 --gid 10001 --home-dir /home/appuser --create-home --shell /usr/sbin/nologin appuser COPY --chown=10001:10001 target/simple-java-dedup.jar /app/app.jar +COPY --chown=10001:10001 target/lib /app/lib COPY --chown=10001:10001 target/classes /app/classes COPY --chown=10001:10001 target/keploy-sdk.jar /app/keploy-sdk.jar COPY --chown=10001:10001 target/jacocoagent.jar /app/jacocoagent.jar diff --git a/simple-java-dedup/README.md b/simple-java-dedup/README.md index 58ffb03d..3a27c8af 100644 --- a/simple-java-dedup/README.md +++ b/simple-java-dedup/README.md @@ -1,6 +1,16 @@ # Simple Java Dynamic Deduplication Sample -This is a small plain-Java HTTP service used to smoke-test Keploy Enterprise Java dynamic deduplication on Java 8 and Java 17. It uses only JDK APIs (`com.sun.net.httpserver.HttpServer`) and does not compile against the Keploy SDK. +This is a small plain-Java HTTP service used to smoke-test Keploy Enterprise Java dynamic deduplication on Java 8 and Java 17. It runs on embedded Tomcat (`tomcat-embed-core`) and does not compile against the Keploy SDK. + +## Why Embedded Tomcat (Supported Java HTTP Servers) + +Keploy's Java dynamic deduplication agent (`keploy-sdk` >= 2.3.x) counts HTTP request windows at the server's request entry point so background threads or post-response work cannot leak coverage between test cases. Supported Java HTTP servers are: + +- **Apache Tomcat** (8.5–11, including Spring Boot's default embedded Tomcat) +- **Eclipse Jetty** (9.4–12) +- **Undertow** + +Uncounted servers (such as `com.sun.net.httpserver.HttpServer`, Netty, Reactor Netty, gRPC, Vert.x, and Micronaut) fail closed with `unverified:no-server-entry` in Kubernetes dynamic dedup (`k8s-proxy`), which keeps all test cases and reports 0 duplicates rather than risking unsafe drops. This sample uses embedded Tomcat so it works out of the box with both Kubernetes (`k8s-proxy`) and CLI dynamic deduplication. The checked-in fixtures exercise `/healthz`, `/grade`, `/shipping`, `/inventory`, and `/invoice`. They intentionally contain duplicate coverage paths: diff --git a/simple-java-dedup/pom.xml b/simple-java-dedup/pom.xml index 3acef6df..02bf42e2 100644 --- a/simple-java-dedup/pom.xml +++ b/simple-java-dedup/pom.xml @@ -10,15 +10,27 @@ jar simple-java-dedup - Small plain-Java sample for Keploy dynamic deduplication smoke tests. + Small plain-Java sample on embedded Tomcat for Keploy dynamic deduplication smoke tests. 1.8 1.8 UTF-8 0.8.12 + 9.0.121 + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.embed.version} + + + simple-java-dedup @@ -39,6 +51,8 @@ io.keploy.samples.simplededup.SimpleJavaDedupApplication + true + lib/ @@ -48,6 +62,17 @@ maven-dependency-plugin 3.6.1 + + copy-runtime-dependencies + package + + copy-dependencies + + + runtime + ${project.build.directory}/lib + + copy-jacoco-agent package diff --git a/simple-java-dedup/src/main/java/io/keploy/samples/simplededup/SimpleJavaDedupApplication.java b/simple-java-dedup/src/main/java/io/keploy/samples/simplededup/SimpleJavaDedupApplication.java index 72e3aa9b..139a6016 100644 --- a/simple-java-dedup/src/main/java/io/keploy/samples/simplededup/SimpleJavaDedupApplication.java +++ b/simple-java-dedup/src/main/java/io/keploy/samples/simplededup/SimpleJavaDedupApplication.java @@ -1,50 +1,69 @@ package io.keploy.samples.simplededup; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; +import org.apache.catalina.Context; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.startup.Tomcat; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; -import java.net.InetSocketAddress; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Executors; public final class SimpleJavaDedupApplication { private SimpleJavaDedupApplication() { } - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws Exception { int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080")); - HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0); - server.createContext("/healthz", new HealthHandler()); - server.createContext("/grade", new GradeHandler()); - server.createContext("/shipping", new ShippingHandler()); - server.createContext("/inventory", new InventoryHandler()); - server.createContext("/invoice", new InvoiceHandler()); - server.setExecutor(Executors.newCachedThreadPool()); - server.start(); + Path baseDir = Files.createTempDirectory("simple-java-dedup-tomcat-"); + Tomcat tomcat = new Tomcat(); + tomcat.setBaseDir(baseDir.toAbsolutePath().toString()); + tomcat.setHostname("0.0.0.0"); + + Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); + connector.setPort(port); + connector.setProperty("address", "0.0.0.0"); + tomcat.setConnector(connector); + + Context ctx = tomcat.addContext("", baseDir.toAbsolutePath().toString()); + register(ctx, "healthz", "/healthz", new HealthHandler()); + register(ctx, "grade", "/grade", new GradeHandler()); + register(ctx, "shipping", "/shipping", new ShippingHandler()); + register(ctx, "inventory", "/inventory", new InventoryHandler()); + register(ctx, "invoice", "/invoice", new InvoiceHandler()); + + tomcat.start(); System.out.println("simple-java-dedup listening on " + port); + tomcat.getServer().await(); } - private static final class HealthHandler implements HttpHandler { + private static void register(Context ctx, String name, String path, HttpServlet servlet) { + Tomcat.addServlet(ctx, name, servlet); + ctx.addServletMappingDecoded(path, name); + } + + private static final class HealthHandler extends HttpServlet { @Override - public void handle(HttpExchange exchange) throws IOException { - if (!"GET".equals(exchange.getRequestMethod())) { - respond(exchange, 405, "{\"error\":\"method_not_allowed\"}"); + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + if (!"GET".equals(req.getMethod())) { + respond(resp, 405, "{\"error\":\"method_not_allowed\"}"); return; } - respond(exchange, 200, "{\"status\":\"ok\"}"); + respond(resp, 200, "{\"status\":\"ok\"}"); } } - private static final class GradeHandler implements HttpHandler { + private static final class GradeHandler extends HttpServlet { @Override - public void handle(HttpExchange exchange) throws IOException { - Map query = parseQuery(exchange.getRequestURI().getRawQuery()); + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Map query = parseQuery(req.getQueryString()); int score = parseInt(query.get("score"), 0); String grade; String message; @@ -61,14 +80,14 @@ public void handle(HttpExchange exchange) throws IOException { grade = "F"; message = "retry"; } - respond(exchange, 200, "{\"score\":" + score + ",\"grade\":\"" + grade + "\",\"message\":\"" + message + "\"}"); + respond(resp, 200, "{\"score\":" + score + ",\"grade\":\"" + grade + "\",\"message\":\"" + message + "\"}"); } } - private static final class ShippingHandler implements HttpHandler { + private static final class ShippingHandler extends HttpServlet { @Override - public void handle(HttpExchange exchange) throws IOException { - Map query = parseQuery(exchange.getRequestURI().getRawQuery()); + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Map query = parseQuery(req.getQueryString()); String country = query.getOrDefault("country", "US"); int total = parseInt(query.get("total"), 0); String tier; @@ -83,14 +102,14 @@ public void handle(HttpExchange exchange) throws IOException { tier = "standard"; etaDays = 5; } - respond(exchange, 200, "{\"country\":\"" + country.toUpperCase() + "\",\"total\":" + total + ",\"tier\":\"" + tier + "\",\"etaDays\":" + etaDays + "}"); + respond(resp, 200, "{\"country\":\"" + country.toUpperCase() + "\",\"total\":" + total + ",\"tier\":\"" + tier + "\",\"etaDays\":" + etaDays + "}"); } } - private static final class InventoryHandler implements HttpHandler { + private static final class InventoryHandler extends HttpServlet { @Override - public void handle(HttpExchange exchange) throws IOException { - Map query = parseQuery(exchange.getRequestURI().getRawQuery()); + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Map query = parseQuery(req.getQueryString()); String sku = query.getOrDefault("sku", "BOOK-1").toUpperCase(); int quantity = parseInt(query.get("quantity"), 1); String lane; @@ -105,14 +124,14 @@ public void handle(HttpExchange exchange) throws IOException { lane = "manual-review"; reserved = false; } - respond(exchange, 200, "{\"sku\":\"" + sku + "\",\"quantity\":" + quantity + ",\"lane\":\"" + lane + "\",\"reserved\":" + reserved + "}"); + respond(resp, 200, "{\"sku\":\"" + sku + "\",\"quantity\":" + quantity + ",\"lane\":\"" + lane + "\",\"reserved\":" + reserved + "}"); } } - private static final class InvoiceHandler implements HttpHandler { + private static final class InvoiceHandler extends HttpServlet { @Override - public void handle(HttpExchange exchange) throws IOException { - Map query = parseQuery(exchange.getRequestURI().getRawQuery()); + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + Map query = parseQuery(req.getQueryString()); String customer = query.getOrDefault("customer", "guest").toLowerCase(); int subtotal = parseInt(query.get("subtotal"), 0); int discount; @@ -131,7 +150,7 @@ public void handle(HttpExchange exchange) throws IOException { segment = "standard"; } int total = subtotal - discount; - respond(exchange, 200, "{\"customer\":\"" + customer + "\",\"subtotal\":" + subtotal + ",\"discount\":" + discount + ",\"total\":" + total + ",\"segment\":\"" + segment + "\"}"); + respond(resp, 200, "{\"customer\":\"" + customer + "\",\"subtotal\":" + subtotal + ",\"discount\":" + discount + ",\"total\":" + total + ",\"segment\":\"" + segment + "\"}"); } } @@ -171,15 +190,13 @@ private static String decode(String value) { } } - private static void respond(HttpExchange exchange, int status, String body) throws IOException { + private static void respond(HttpServletResponse resp, int status, String body) throws IOException { byte[] bytes = body.getBytes(StandardCharsets.UTF_8); - exchange.getResponseHeaders().set("Content-Type", "application/json"); - exchange.sendResponseHeaders(status, bytes.length); - OutputStream responseBody = exchange.getResponseBody(); - try { - responseBody.write(bytes); - } finally { - responseBody.close(); - } + resp.setStatus(status); + resp.setContentType("application/json"); + resp.setContentLength(bytes.length); + OutputStream responseBody = resp.getOutputStream(); + responseBody.write(bytes); + responseBody.flush(); } }