Skip to content

Commit 10246a2

Browse files
committed
2.0.3: Create commons module
1 parent a189f8e commit 10246a2

File tree

12 files changed

+344
-7
lines changed

12 files changed

+344
-7
lines changed

logging-spring-commons/pom.xml

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.github.piomin</groupId>
8+
<artifactId>spring-boot-logging</artifactId>
9+
<version>2.0.3</version>
10+
</parent>
11+
12+
<groupId>pl.piomin</groupId>
13+
<artifactId>logging-spring-commons</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework</groupId>
18+
<artifactId>spring-web</artifactId>
19+
<version>${spring.version}</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>jakarta.servlet</groupId>
24+
<artifactId>jakarta.servlet-api</artifactId>
25+
<version>${jakarta-servlet.version}</version>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>commons-io</groupId>
30+
<artifactId>commons-io</artifactId>
31+
<version>${commons-io.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>${slf4j.version}</version>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.13.0</version>
46+
<configuration>
47+
<target>${java.version}</target>
48+
<source>${java.version}</source>
49+
<encoding>UTF-8</encoding>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<profiles>
56+
<profile>
57+
<id>release</id>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-gpg-plugin</artifactId>
63+
<version>3.2.1</version>
64+
<executions>
65+
<execution>
66+
<id>sign-artifacts</id>
67+
<phase>verify</phase>
68+
<goals>
69+
<goal>sign</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-source-plugin</artifactId>
77+
<version>3.3.0</version>
78+
<executions>
79+
<execution>
80+
<id>attach-sources</id>
81+
<goals>
82+
<goal>jar-no-fork</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-javadoc-plugin</artifactId>
90+
<version>3.6.3</version>
91+
<executions>
92+
<execution>
93+
<id>attach-javadocs</id>
94+
<goals>
95+
<goal>jar</goal>
96+
</goals>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
</profile>
103+
</profiles>
104+
105+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.piomin.logging.commons.client;
2+
3+
import org.slf4j.MDC;
4+
import org.springframework.http.HttpRequest;
5+
import org.springframework.http.client.ClientHttpRequestExecution;
6+
import org.springframework.http.client.ClientHttpRequestInterceptor;
7+
import org.springframework.http.client.ClientHttpResponse;
8+
9+
import java.io.IOException;
10+
11+
public class RestTemplateSetHeaderInterceptor implements ClientHttpRequestInterceptor {
12+
13+
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
14+
request.getHeaders().add("X-Correlation-ID", MDC.get("X-Correlation-ID"));
15+
request.getHeaders().add("X-Request-ID", MDC.get("X-Request-ID"));
16+
return execution.execute(request, body);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pl.piomin.logging.commons.util;
2+
3+
import jakarta.servlet.http.HttpServletRequest;
4+
import org.slf4j.MDC;
5+
6+
import java.util.UUID;
7+
8+
public class UniqueIDGenerator {
9+
10+
private final String requestIdHeaderName;
11+
private final String correlationIdHeaderName;
12+
13+
public UniqueIDGenerator(String requestIdHeaderName, String correlationIdHeaderName) {
14+
this.requestIdHeaderName = requestIdHeaderName;
15+
this.correlationIdHeaderName = correlationIdHeaderName;
16+
}
17+
18+
public void generateAndSetMDC(HttpServletRequest request) {
19+
String requestId = request.getHeader(requestIdHeaderName);
20+
if (requestId == null)
21+
requestId = UUID.randomUUID().toString();
22+
MDC.put(requestIdHeaderName, requestId);
23+
24+
String correlationId = request.getHeader(correlationIdHeaderName);
25+
if (correlationId == null)
26+
correlationId = UUID.randomUUID().toString();
27+
MDC.put(correlationIdHeaderName, correlationId);
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package pl.piomin.logging.commons.wrapper;
2+
3+
import jakarta.servlet.ServletOutputStream;
4+
import jakarta.servlet.WriteListener;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
10+
public class ServletOutputStreamWrapper extends ServletOutputStream {
11+
12+
private OutputStream outputStream;
13+
private ByteArrayOutputStream copy;
14+
15+
public ServletOutputStreamWrapper(OutputStream outputStream) {
16+
this.outputStream = outputStream;
17+
this.copy = new ByteArrayOutputStream();
18+
}
19+
20+
@Override
21+
public void write(int b) throws IOException {
22+
outputStream.write(b);
23+
copy.write(b);
24+
}
25+
26+
public byte[] getCopy() {
27+
return copy.toByteArray();
28+
}
29+
30+
@Override
31+
public boolean isReady() {
32+
return true;
33+
}
34+
35+
@Override
36+
public void setWriteListener(WriteListener writeListener) {
37+
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package pl.piomin.logging.commons.wrapper;
2+
3+
import jakarta.servlet.ReadListener;
4+
import jakarta.servlet.ServletInputStream;
5+
import jakarta.servlet.http.HttpServletRequest;
6+
import jakarta.servlet.http.HttpServletRequestWrapper;
7+
import org.apache.commons.io.IOUtils;
8+
9+
import java.io.ByteArrayInputStream;
10+
import java.io.IOException;
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class SpringRequestWrapper extends HttpServletRequestWrapper {
16+
17+
private byte[] body;
18+
private Map<String, String[]> parameterMap;
19+
20+
public SpringRequestWrapper(HttpServletRequest request) {
21+
super(request);
22+
parameterMap = request.getParameterMap();
23+
String query = request.getQueryString();
24+
if (query != null) {
25+
body = query.getBytes();
26+
} else {
27+
try {
28+
body = IOUtils.toByteArray(request.getInputStream());
29+
} catch (IOException ex) {
30+
body = new byte[0];
31+
}
32+
}
33+
}
34+
35+
@Override
36+
public Map<String, String[]> getParameterMap() {
37+
return parameterMap;
38+
}
39+
40+
@Override
41+
public ServletInputStream getInputStream() throws IOException {
42+
return new ServletInputStream() {
43+
public boolean isFinished() {
44+
return false;
45+
}
46+
47+
public boolean isReady() {
48+
return true;
49+
}
50+
51+
public void setReadListener(ReadListener readListener) {
52+
53+
}
54+
55+
ByteArrayInputStream byteArray = new ByteArrayInputStream(body);
56+
57+
@Override
58+
public int read() throws IOException {
59+
return byteArray.read();
60+
}
61+
};
62+
}
63+
64+
public Map<String, String> getAllHeaders() {
65+
final Map<String, String> headers = new HashMap<>();
66+
Collections.list(getHeaderNames()).forEach(it -> headers.put(it, getHeader(it)));
67+
return headers;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package pl.piomin.logging.commons.wrapper;
2+
3+
import jakarta.servlet.ServletOutputStream;
4+
import jakarta.servlet.http.HttpServletResponse;
5+
import jakarta.servlet.http.HttpServletResponseWrapper;
6+
7+
import java.io.IOException;
8+
import java.io.OutputStreamWriter;
9+
import java.io.PrintWriter;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
public class SpringResponseWrapper extends HttpServletResponseWrapper {
14+
15+
private ServletOutputStream outputStream;
16+
private PrintWriter writer;
17+
private ServletOutputStreamWrapper copier;
18+
19+
public SpringResponseWrapper(HttpServletResponse response) throws IOException {
20+
super(response);
21+
}
22+
23+
@Override
24+
public ServletOutputStream getOutputStream() throws IOException {
25+
if (writer != null) {
26+
throw new IllegalStateException("getWriter() has already been called on this response.");
27+
}
28+
29+
if (outputStream == null) {
30+
outputStream = getResponse().getOutputStream();
31+
copier = new ServletOutputStreamWrapper(outputStream);
32+
}
33+
34+
return copier;
35+
}
36+
37+
@Override
38+
public PrintWriter getWriter() throws IOException {
39+
if (outputStream != null) {
40+
throw new IllegalStateException("getOutputStream() has already been called on this response.");
41+
}
42+
43+
if (writer == null) {
44+
copier = new ServletOutputStreamWrapper(getResponse().getOutputStream());
45+
writer = new PrintWriter(new OutputStreamWriter(copier, getResponse().getCharacterEncoding()), true);
46+
}
47+
48+
return writer;
49+
}
50+
51+
@Override
52+
public void flushBuffer() throws IOException {
53+
if (writer != null) {
54+
writer.flush();
55+
} else if (outputStream != null) {
56+
copier.flush();
57+
}
58+
}
59+
60+
public byte[] getContentAsByteArray() {
61+
if (copier != null) {
62+
return copier.getCopy();
63+
} else {
64+
return new byte[0];
65+
}
66+
}
67+
68+
public Map<String, String> getAllHeaders() {
69+
final Map<String, String> headers = new HashMap<>();
70+
getHeaderNames().forEach(it -> headers.put(it, getHeader(it)));
71+
return headers;
72+
}
73+
74+
}

logging-spring-tests/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.piomin</groupId>
88
<artifactId>spring-boot-logging</artifactId>
9-
<version>2.0.3-SNAPSHOT</version>
9+
<version>2.0.3</version>
1010
</parent>
1111

1212
<artifactId>logging-spring-tests</artifactId>

logstash-logging-spring-boot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.piomin</groupId>
77
<artifactId>logstash-logging-spring-boot-starter</artifactId>
8-
<version>2.0.3-SNAPSHOT</version>
8+
<version>2.0.3</version>
99

1010
<name>logstash-logging-spring-boot-starter</name>
1111
<description>Library for HTTP logging with Spring Boot</description>

loki-logging-spring-boot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.piomin</groupId>
88
<artifactId>loki-logging-spring-boot-starter</artifactId>
9-
<version>2.0.3-SNAPSHOT</version>
9+
<version>2.0.3</version>
1010

1111
<name>loki-logging-spring-boot-starter</name>
1212
<description>Library for HTTP logging with Spring Boot</description>

pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
<groupId>com.github.piomin</groupId>
77
<artifactId>spring-boot-logging</artifactId>
8-
<version>2.0.3-SNAPSHOT</version>
8+
<version>2.0.3</version>
99
<packaging>pom</packaging>
1010
<name>spring-boot-logging</name>
1111
<description>Library for HTTP logging with Spring Boot</description>
1212
<url>https://github.com/piomin/spring-boot-logging</url>
1313

1414
<properties>
15-
15+
<jakarta-servlet.version>6.0.0</jakarta-servlet.version>
1616
<java.version>17</java.version>
1717
<commons-io.version>2.13.0</commons-io.version>
1818
<javax-servlet.version>4.0.1</javax-servlet.version>
@@ -33,6 +33,7 @@
3333
<module>logging-spring-tests</module>
3434
<module>reactive-logging-spring-tests</module>
3535
<module>loki-logging-spring-boot-starter</module>
36+
<module>logging-spring-commons</module>
3637
</modules>
3738

3839
</project>

0 commit comments

Comments
 (0)