Skip to content

Commit 6da3eb4

Browse files
authored
Jetty: Request log (#83)
* Jetty: Request log * Add explanation
1 parent 6e5d3e4 commit 6da3eb4

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ IO | Java File I/O.
2323
Immutables | Generate simple, safe and consistent value objects. <https://immutables.github.io/>
2424
Jackson | Jackson, a high-performance JSON processor for Java.
2525
Java 8 | New functionality of Java 8, including filter, map, stream.
26+
Jetty | Jetty Server.
2627
JGit | Basic usages of [JGit][jgit].
2728
JMH | Java Microbenchmark Harness (JMH).
2829
JSON | JSON conversion libraries in Java.

jetty/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-examples-parent</artifactId>
7+
<groupId>io.mincongh</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-examples-jetty</artifactId>
13+
<name>Java Examples - Jetty</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.eclipse.jetty</groupId>
18+
<artifactId>jetty-server</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.eclipse.jetty</groupId>
23+
<artifactId>jetty-servlet</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.junit.jupiter</groupId>
29+
<artifactId>junit-jupiter-api</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-engine</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.assertj</groupId>
40+
<artifactId>assertj-core</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package io.mincong.jetty;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
import java.net.URI;
7+
import java.net.http.HttpClient;
8+
import java.net.http.HttpRequest;
9+
import java.net.http.HttpResponse.BodyHandlers;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import javax.servlet.http.HttpServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
import org.eclipse.jetty.server.CustomRequestLog;
16+
import org.eclipse.jetty.server.Server;
17+
import org.eclipse.jetty.server.handler.RequestLogHandler;
18+
import org.eclipse.jetty.servlet.ServletContextHandler;
19+
import org.eclipse.jetty.servlet.ServletHolder;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.io.TempDir;
24+
25+
class RequestLogTest {
26+
27+
private Server server;
28+
private Path logPath;
29+
30+
@BeforeEach
31+
void setUp(@TempDir Path tempDir) throws Exception {
32+
// Create servlet handler
33+
var servletHolder = new ServletHolder(MyServlet.class);
34+
var context = new ServletContextHandler(ServletContextHandler.SESSIONS);
35+
context.setContextPath("/");
36+
context.addServlet(servletHolder, "/");
37+
38+
// Create log handler
39+
logPath = tempDir.resolve("request.log").toAbsolutePath();
40+
var logHandler = new RequestLogHandler();
41+
logHandler.setRequestLog(new CustomRequestLog(logPath.toString()));
42+
logHandler.setHandler(context);
43+
44+
// Start server
45+
server = new Server(8080);
46+
server.setHandler(logHandler);
47+
server.start();
48+
}
49+
50+
@AfterEach
51+
void tearDown() throws Exception {
52+
server.stop();
53+
}
54+
55+
/**
56+
* Log all HTTP requests into file "request.log", such as:
57+
*
58+
* <pre>
59+
* 127.0.0.1 - - [04/Jul/2020:08:38:59 +0000] "GET / HTTP/1.1" 200 31 "-" "Java-http-client/11.0.2"
60+
* </pre>
61+
*
62+
* where:
63+
*
64+
* <ul>
65+
* <li>{@code 127.0.0.1} is the IP address of the client (remote host) which made the request to
66+
* the server.
67+
* <li>{@code -} is the user-identifier is the RFC 1413 identity of the client. Usually "-".
68+
* <li>{@code -} is the user-id of the person requesting the document. Usually "-" unless
69+
* .htaccess has requested authentication.
70+
* <li>{@code [04/Jul/2020:08:38:59 +0000]} is the date, time, and time zone that the request
71+
* was received, by default in strftime format "%d/%b/%Y:%H:%M:%S %z".
72+
* <li>{@code "GET / HTTP/1.1"} is the request line from the client. The method GET, / the
73+
* resource requested, and HTTP/1.1 the HTTP protocol.
74+
* <li>{@code 200} is the HTTP status code returned to the client. 2xx is a successful response,
75+
* 3xx a redirection, 4xx a client error, and 5xx a server error.
76+
* <li>{@code 31} is the size of the object returned to the client, measured in bytes.
77+
* <li>{@code "Java-http-client/11.0.2"} is the user-agent.
78+
* </ul>
79+
*
80+
* @see <a href="https://en.wikipedia.org/wiki/Common_Log_Format">Common Log Format |
81+
* Wikipedia</a>
82+
* @see CustomRequestLog
83+
*/
84+
@Test
85+
void requestLog() throws Exception {
86+
// When sending a HTTP request
87+
var httpClient = HttpClient.newHttpClient();
88+
var request = HttpRequest.newBuilder(URI.create("http://localhost:8080")).build();
89+
var response = httpClient.send(request, BodyHandlers.ofString());
90+
91+
// Then the response is OK
92+
assertThat(response.statusCode()).isEqualTo(200);
93+
94+
// And the request is logged
95+
assertThat(Files.readString(logPath))
96+
.startsWith("127.0.0.1 - - [")
97+
.contains("] \"GET / HTTP/1.1\" 200 31 \"-\" \"Java-http-client/");
98+
}
99+
100+
public static class MyServlet extends HttpServlet {
101+
@Override
102+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
103+
throws IOException {
104+
response.getWriter().write("{\"message\":\"Welcome to Jetty!\"}");
105+
}
106+
}
107+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<module>vavr</module>
8181
<module>xml</module>
8282
<module>guava</module>
83+
<module>jetty</module>
8384
</modules>
8485

8586
<dependencyManagement>

0 commit comments

Comments
 (0)