Skip to content

Commit 50ece39

Browse files
KarstenSchnitterGitHub Enterprise
authored andcommitted
Merge pull request #3 from perfx/tomcat-fix
Fix late getRequest and getResponse calls in Tomcat
2 parents 588889a + 711de83 commit 50ece39

File tree

24 files changed

+534
-419
lines changed

24 files changed

+534
-419
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ All in all, you should do the following:
2727
* adjust your logging configuration accordingly.
2828

2929

30-
Say, you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `2.2.0`):
30+
Say, you want to make use of the *servlet filter* feature, then you need to add the following dependency to your POM with property `cf-logging-version` referring to the latest nexus version (currently `2.2.1`):
3131

3232
```xml
3333
<properties>

cf-java-logging-support-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<parent>
3333
<groupId>com.sap.hcp.cf.logging</groupId>
3434
<artifactId>cf-java-logging-support-parent</artifactId>
35-
<version>2.2.0</version>
35+
<version>2.2.1-SNAPSHOT</version>
3636
<relativePath>../pom.xml</relativePath>
3737
</parent>
3838
<build>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.sap.hcp.cf.logging.common;
2+
3+
import com.sap.hcp.cf.logging.common.RequestRecord.Direction;
4+
5+
public class RequestRecordBuilder {
6+
7+
private final RequestRecord requestRecord;
8+
9+
private RequestRecordBuilder(String layerKey) {
10+
this.requestRecord = new RequestRecord(layerKey);
11+
}
12+
13+
private RequestRecordBuilder(String layerKey, Direction direction) {
14+
this.requestRecord = new RequestRecord(layerKey, direction);
15+
}
16+
17+
public static RequestRecordBuilder requestRecord(String layerKey) {
18+
return new RequestRecordBuilder(layerKey);
19+
}
20+
21+
public static RequestRecordBuilder requestRecord(String layerKey, Direction direction) {
22+
return new RequestRecordBuilder(layerKey, direction);
23+
}
24+
25+
public RequestRecord build() {
26+
return requestRecord;
27+
}
28+
29+
public RequestRecordBuilder addTag(String fieldKey, String tag) {
30+
requestRecord.addTag(fieldKey, tag);
31+
return this;
32+
}
33+
34+
public RequestRecordBuilder addOptionalTag(boolean optionalFieldCanBeLogged, String fieldKey, String tag) {
35+
36+
if (!optionalFieldCanBeLogged && tag != null) {
37+
requestRecord.addTag(fieldKey, Defaults.REDACTED);
38+
}
39+
40+
if (!optionalFieldCanBeLogged && tag.equals(Defaults.UNKNOWN)) {
41+
requestRecord.addTag(fieldKey, tag);
42+
}
43+
44+
if (optionalFieldCanBeLogged) {
45+
requestRecord.addTag(fieldKey, tag);
46+
}
47+
return this;
48+
}
49+
50+
public RequestRecordBuilder addContextTag(String fieldKey, String tag) {
51+
requestRecord.addContextTag(fieldKey, tag);
52+
return this;
53+
}
54+
55+
public RequestRecordBuilder addValue(String fieldKey, Value value) {
56+
requestRecord.addValue(fieldKey, value);
57+
return this;
58+
}
59+
}

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/RequestRecordConfigurator.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sap.hcp.cf.logging.common;
22

3-
import static com.sap.hcp.cf.logging.common.RequestRecordConfigurator.to;
3+
import static com.sap.hcp.cf.logging.common.RequestRecordBuilder.requestRecord;
44
import static org.junit.Assert.assertEquals;
55

66
import java.io.IOException;
@@ -10,52 +10,48 @@
1010
import com.fasterxml.jackson.jr.ob.JSON;
1111
import com.fasterxml.jackson.jr.ob.JSONObjectException;
1212

13-
public class RequestRecordConfiguratorTest {
13+
public class RequestRecordBuilderTest {
1414

1515
@Test
1616
public void testAddingSingleActivatedOptionalTagToRequestRecord() throws JSONObjectException, IOException {
17-
RequestRecord requestRecord = new RequestRecord("TEST");
1817
boolean canBeLogged = true;
1918
String key = "TestKey";
2019
String tag = "TestTag";
2120

22-
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
21+
RequestRecord requestRecord = requestRecord("TEST").addOptionalTag(canBeLogged, key, tag).build();
2322

2423
assertEquals(tag, getFieldFromRequestRecord(requestRecord, key));
2524
}
2625

2726
@Test
2827
public void testAddingSingleForbiddenOptionalTagToRequestRecord() throws JSONObjectException, IOException {
29-
RequestRecord requestRecord = new RequestRecord("TEST");
3028
boolean canBeLogged = false;
3129
String key = "TestKey";
3230
String tag = "TestTag";
3331

34-
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
32+
RequestRecord requestRecord = requestRecord("TEST").addOptionalTag(canBeLogged, key, tag).build();
3533

3634
assertEquals(Defaults.REDACTED, getFieldFromRequestRecord(requestRecord, key));
3735
}
3836

3937
@Test
4038
public void testAddingSingleForbiddenOptionalNullTagToRequestRecord() throws JSONObjectException, IOException {
41-
RequestRecord requestRecord = new RequestRecord("TEST");
4239
boolean canBeLogged = false;
4340
String key = "TestKey";
4441
String tag = Defaults.UNKNOWN;
4542

46-
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
43+
RequestRecord requestRecord = requestRecord("TEST").addOptionalTag(canBeLogged, key, tag).build();
4744

4845
assertEquals(Defaults.UNKNOWN, getFieldFromRequestRecord(requestRecord, key));
4946
}
5047

5148
@Test
5249
public void testAddingSingleActivatedOptionalNullTagToRequestRecord() throws JSONObjectException, IOException {
53-
RequestRecord requestRecord = new RequestRecord("TEST");
5450
boolean canBeLogged = true;
5551
String key = "TestKey";
5652
String tag = Defaults.UNKNOWN;
5753

58-
to(requestRecord).addOptionalTag(canBeLogged, key, tag);
54+
RequestRecord requestRecord = requestRecord("TEST").addOptionalTag(canBeLogged, key, tag).build();
5955

6056
assertEquals(Defaults.UNKNOWN, getFieldFromRequestRecord(requestRecord, key));
6157
}

cf-java-logging-support-jersey/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath>../pom.xml</relativePath>
1010
<groupId>com.sap.hcp.cf.logging</groupId>
1111
<artifactId>cf-java-logging-support-parent</artifactId>
12-
<version>2.2.0</version>
12+
<version>2.2.1-SNAPSHOT</version>
1313
</parent>
1414

1515
<name>cf-java-logging-support-jersey</name>

cf-java-logging-support-jersey/src/main/java/com/sap/hcp/cf/logging/jersey/filter/RequestHandler.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.sap.hcp.cf.logging.jersey.filter;
22

3-
import static com.sap.hcp.cf.logging.common.RequestRecordConfigurator.to;
4-
53
import java.net.URI;
64

75
import com.sap.hcp.cf.logging.common.Defaults;
@@ -11,6 +9,7 @@
119
import com.sap.hcp.cf.logging.common.LogOptionalFieldsSettings;
1210
import com.sap.hcp.cf.logging.common.LongValue;
1311
import com.sap.hcp.cf.logging.common.RequestRecord;
12+
import com.sap.hcp.cf.logging.common.RequestRecordBuilder;
1413

1514
public class RequestHandler {
1615
final LogOptionalFieldsSettings logOptionalFieldsSettings;
@@ -38,38 +37,28 @@ public RequestRecord handle(RequestContextAdapter adapter) {
3837
}
3938
}
4039

41-
RequestRecord lrec = new RequestRecord(adapter.getName(), adapter.getDirection());
42-
lrec.start();
43-
44-
addHeaders(adapter, lrec);
45-
40+
boolean isSensitiveConnectionData = logOptionalFieldsSettings.isLogSensitiveConnectionData();
41+
boolean isLogRemoteUserField = logOptionalFieldsSettings.isLogRemoteUserField();
42+
boolean isLogRefererField = logOptionalFieldsSettings.isLogRefererField();
43+
RequestRecord lrec = RequestRecordBuilder.requestRecord(adapter.getName(), adapter.getDirection())
44+
.addTag(Fields.REQUEST, getValue(getRequestUri(adapter)))
45+
.addTag(Fields.METHOD, getValue(adapter.getMethod()))
46+
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_IP, getValue(adapter.getUri().getAuthority()))
47+
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_HOST, getValue(adapter.getUri().getHost()))
48+
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_PORT,
49+
Integer.toString(adapter.getUri().getPort()))
50+
.addOptionalTag(isSensitiveConnectionData, Fields.X_FORWARDED_FOR,
51+
getHeader(adapter, HttpHeaders.X_FORWARDED_FOR))
52+
.addOptionalTag(isLogRemoteUserField, Fields.REMOTE_USER, getValue(adapter.getUser()))
53+
.addOptionalTag(isLogRefererField, Fields.REFERER, getHeader(adapter, HttpHeaders.REFERER))
54+
.addContextTag(Fields.REQUEST_ID, getHeader(adapter, HttpHeaders.X_VCAP_REQUEST_ID))
55+
.addValue(Fields.REQUEST_SIZE_B, new LongValue(adapter.getRequestSize())).build();
56+
57+
lrec.start();
4658
return lrec;
4759

4860
}
4961

50-
private void addHeaders(RequestContextAdapter adapter, RequestRecord lrec) {
51-
52-
lrec.addTag(Fields.REQUEST, getValue(getRequestUri(adapter)));
53-
lrec.addTag(Fields.METHOD, getValue(adapter.getMethod()));
54-
55-
boolean isSensitiveConnectionData = logOptionalFieldsSettings.isLogSensitiveConnectionData();
56-
57-
to(lrec).addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_IP, getValue(adapter.getUri().getAuthority()))
58-
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_HOST, getValue(adapter.getUri().getHost()))
59-
.addOptionalTag(isSensitiveConnectionData, Fields.REMOTE_PORT, Integer.toString(adapter.getUri()
60-
.getPort()))
61-
.addOptionalTag(isSensitiveConnectionData, Fields.X_FORWARDED_FOR, getHeader(adapter,
62-
HttpHeaders.X_FORWARDED_FOR))
63-
.addOptionalTag(logOptionalFieldsSettings.isLogRemoteUserField(), Fields.REMOTE_USER, getValue(adapter
64-
.getUser()))
65-
.addOptionalTag(logOptionalFieldsSettings.isLogRefererField(), Fields.REFERER, getHeader(adapter,
66-
HttpHeaders.REFERER));
67-
lrec.addContextTag(Fields.REQUEST_ID, getHeader(adapter, HttpHeaders.X_VCAP_REQUEST_ID));
68-
69-
lrec.addValue(Fields.REQUEST_SIZE_B, new LongValue(adapter.getRequestSize()));
70-
71-
}
72-
7362
private String getValue(String value) {
7463
return value != null ? value : Defaults.UNKNOWN;
7564
}

cf-java-logging-support-log4j2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<relativePath>../pom.xml</relativePath>
1212
<groupId>com.sap.hcp.cf.logging</groupId>
1313
<artifactId>cf-java-logging-support-parent</artifactId>
14-
<version>2.2.0</version>
14+
<version>2.2.1-SNAPSHOT</version>
1515
</parent>
1616

1717
<dependencies>

cf-java-logging-support-logback/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<relativePath>../pom.xml</relativePath>
1111
<groupId>com.sap.hcp.cf.logging</groupId>
1212
<artifactId>cf-java-logging-support-parent</artifactId>
13-
<version>2.2.0</version>
13+
<version>2.2.1-SNAPSHOT</version>
1414
</parent>
1515

1616
<dependencies>

cf-java-logging-support-servlet/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>com.sap.hcp.cf.logging</groupId>
1111
<artifactId>cf-java-logging-support-parent</artifactId>
12-
<version>2.2.0</version>
12+
<version>2.2.1-SNAPSHOT</version>
1313
<relativePath>../pom.xml</relativePath>
1414
</parent>
1515

0 commit comments

Comments
 (0)