Skip to content

Commit 711de83

Browse files
Fix getRequest and getResponse calls after Response commit
Signed-off-by: Karsten Schnitter <[email protected]>
1 parent abaee6a commit 711de83

File tree

10 files changed

+367
-296
lines changed

10 files changed

+367
-296
lines changed

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/LoggingAsyncContextImpl.java

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,34 @@
1111
import javax.servlet.ServletException;
1212
import javax.servlet.ServletRequest;
1313
import javax.servlet.ServletResponse;
14-
import javax.servlet.http.HttpServletRequest;
15-
import javax.servlet.http.HttpServletResponse;
1614

1715
import org.slf4j.MDC;
1816

1917
public class LoggingAsyncContextImpl implements AsyncContext {
2018

2119
private AsyncContext asyncContext;
2220

23-
public LoggingAsyncContextImpl(AsyncContext asyncContext, final RequestLoggingVisitor loggingVisitor) {
21+
public LoggingAsyncContextImpl(AsyncContext asyncContext, final RequestLogger requestLogger) {
2422
this.asyncContext = asyncContext;
2523
asyncContext.addListener(new AsyncListener() {
2624

2725
@Override
2826
public void onTimeout(AsyncEvent event) throws IOException {
29-
generateLog(loggingVisitor);
27+
requestLogger.logRequest();
3028
}
3129

32-
private void generateLog(final RequestLoggingVisitor loggingVisitor) {
33-
ServletRequest request = getRequest();
34-
ServletResponse response = getResponse();
35-
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
36-
HttpServletRequest httpRequest = (HttpServletRequest) request;
37-
HttpServletResponse httpResponse = (HttpServletResponse) response;
38-
Map<String, String> contextMap = getContextMap();
39-
Map<String, String> currentContextMap = MDC.getCopyOfContextMap();
40-
try {
41-
MDC.setContextMap(contextMap);
42-
loggingVisitor.logRequest(httpRequest, httpResponse);
43-
} finally {
44-
if (currentContextMap != null) {
45-
MDC.setContextMap(currentContextMap);
46-
}
47-
}
48-
}
49-
}
50-
51-
5230
@Override
5331
public void onStartAsync(AsyncEvent event) throws IOException {
5432
}
5533

5634
@Override
5735
public void onError(AsyncEvent event) throws IOException {
58-
generateLog(loggingVisitor);
36+
requestLogger.logRequest();
5937
}
6038

6139
@Override
6240
public void onComplete(AsyncEvent event) throws IOException {
63-
generateLog(loggingVisitor);
41+
requestLogger.logRequest();
6442
}
6543
});
6644
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/LoggingContextRequestWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public class LoggingContextRequestWrapper extends HttpServletRequestWrapper {
1010

11-
private RequestLoggingVisitor loggingVisitor;
11+
private RequestLogger loggingVisitor;
1212

13-
public LoggingContextRequestWrapper(HttpServletRequest request, RequestLoggingVisitor loggingVisitor) {
13+
public LoggingContextRequestWrapper(HttpServletRequest request, RequestLogger loggingVisitor) {
1414
super(request);
1515
this.loggingVisitor = loggingVisitor;
1616
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.sap.hcp.cf.logging.servlet.filter;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.slf4j.MDC;
12+
13+
import com.sap.hcp.cf.logging.common.Defaults;
14+
import com.sap.hcp.cf.logging.common.Fields;
15+
import com.sap.hcp.cf.logging.common.HttpHeaders;
16+
import com.sap.hcp.cf.logging.common.LongValue;
17+
import com.sap.hcp.cf.logging.common.Markers;
18+
import com.sap.hcp.cf.logging.common.RequestRecord;
19+
20+
public class RequestLogger {
21+
22+
private static final Logger LOG = LoggerFactory.getLogger(RequestLogger.class);
23+
24+
private HttpServletRequest httpRequest;
25+
private HttpServletResponse httpResponse;
26+
private RequestRecord requestRecord;
27+
28+
public RequestLogger(RequestRecord requestRecord, HttpServletRequest httpRequest,
29+
HttpServletResponse httpResponse) {
30+
this.requestRecord = requestRecord;
31+
this.httpRequest = httpRequest;
32+
this.httpResponse = httpResponse;
33+
}
34+
35+
public void logRequest() {
36+
requestRecord.stop();
37+
addRequestHandlingParameters();
38+
generateLog();
39+
}
40+
41+
private void addRequestHandlingParameters() {
42+
requestRecord.addValue(Fields.REQUEST_SIZE_B, new LongValue(httpRequest.getContentLength()));
43+
LongValue responseSize = getResponseSize(httpResponse);
44+
if (responseSize != null) {
45+
requestRecord.addValue(Fields.RESPONSE_SIZE_B, responseSize);
46+
}
47+
requestRecord.addTag(Fields.RESPONSE_CONTENT_TYPE, getValue(httpResponse.getHeader(HttpHeaders.CONTENT_TYPE)));
48+
requestRecord.addValue(Fields.RESPONSE_STATUS, new LongValue(httpResponse.getStatus()));
49+
}
50+
51+
private LongValue getResponseSize(HttpServletResponse httpResponse) {
52+
String headerValue = httpResponse.getHeader(HttpHeaders.CONTENT_LENGTH);
53+
if (headerValue != null) {
54+
return new LongValue(Long.valueOf(headerValue));
55+
}
56+
if (httpResponse != null && httpResponse instanceof ContentLengthTrackingResponseWrapper) {
57+
ContentLengthTrackingResponseWrapper wrapper = (ContentLengthTrackingResponseWrapper) httpResponse;
58+
return new LongValue(wrapper.getContentLength());
59+
}
60+
return null;
61+
}
62+
63+
private String getValue(String value) {
64+
return value != null ? value : Defaults.UNKNOWN;
65+
}
66+
67+
private void generateLog() {
68+
Map<String, String> contextMap = getContextMap();
69+
Map<String, String> currentContextMap = MDC.getCopyOfContextMap();
70+
try {
71+
MDC.setContextMap(contextMap);
72+
LOG.info(Markers.REQUEST_MARKER, "{}", requestRecord);
73+
} finally {
74+
if (currentContextMap != null) {
75+
MDC.setContextMap(currentContextMap);
76+
}
77+
}
78+
}
79+
80+
private Map<String, String> getContextMap() {
81+
try {
82+
@SuppressWarnings("unchecked")
83+
Map<String, String> fromRequest = (Map<String, String>) httpRequest.getAttribute(MDC.class.getName());
84+
return fromRequest != null ? fromRequest : Collections.emptyMap();
85+
} catch (ClassCastException ignored) {
86+
return Collections.emptyMap();
87+
}
88+
}
89+
90+
}

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingFilter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void doFilterRequest(HttpServletRequest httpRequest, HttpServletResponse
9595
try {
9696

9797
RequestRecord rr = requestRecordFactory.create(httpRequest);
98-
ContentLengthTrackingResponseWrapper responseWrapper = null;
98+
httpRequest.setAttribute(MDC.class.getName(), MDC.getCopyOfContextMap());
9999

100100
/*
101101
* -- we essentially do three things here: -- a) we create a log
@@ -105,17 +105,16 @@ private void doFilterRequest(HttpServletRequest httpRequest, HttpServletResponse
105105
* content length (hopefully)
106106
*/
107107
if (wrapResponse) {
108-
responseWrapper = new ContentLengthTrackingResponseWrapper(httpResponse);
108+
httpResponse = new ContentLengthTrackingResponseWrapper(httpResponse);
109109
}
110110

111-
RequestLoggingVisitor loggingVisitor = new RequestLoggingVisitor(rr, responseWrapper);
112-
113111
if (wrapRequest) {
114112
httpRequest = new ContentLengthTrackingRequestWrapper(httpRequest);
115-
httpRequest = new LoggingContextRequestWrapper(httpRequest, loggingVisitor);
116113
}
117114

118-
httpRequest.setAttribute(MDC.class.getName(), MDC.getCopyOfContextMap());
115+
RequestLogger loggingVisitor = new RequestLogger(rr, httpRequest, httpResponse);
116+
httpRequest = new LoggingContextRequestWrapper(httpRequest, loggingVisitor);
117+
119118

120119

121120
/* -- start measuring right before calling up the filter chain -- */
@@ -125,7 +124,7 @@ private void doFilterRequest(HttpServletRequest httpRequest, HttpServletResponse
125124
}
126125

127126
if (!httpRequest.isAsyncStarted()) {
128-
loggingVisitor.logRequest(httpRequest, httpResponse);
127+
loggingVisitor.logRequest();
129128
}
130129
/*
131130
* -- close this

cf-java-logging-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingVisitor.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/LoggingAsyncContextImplTest.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import static org.junit.Assert.assertThat;
1111
import static org.mockito.Matchers.any;
1212
import static org.mockito.Mockito.doAnswer;
13-
import static org.mockito.Mockito.mock;
1413
import static org.mockito.Mockito.reset;
1514
import static org.mockito.Mockito.verify;
1615
import static org.mockito.Mockito.when;
@@ -21,7 +20,6 @@
2120
import java.util.concurrent.Future;
2221

2322
import javax.servlet.AsyncContext;
24-
import javax.servlet.AsyncEvent;
2523
import javax.servlet.AsyncListener;
2624
import javax.servlet.http.HttpServletRequest;
2725
import javax.servlet.http.HttpServletResponse;
@@ -45,7 +43,7 @@ public class LoggingAsyncContextImplTest {
4543
private AsyncContext wrappedContext;
4644

4745
@Mock
48-
private RequestLoggingVisitor loggingVisitor;
46+
private RequestLogger requestLogger;
4947

5048
@Mock
5149
private HttpServletRequest request;
@@ -147,24 +145,4 @@ public void run() {
147145
assertThat(finalContextMap, hasEntry("initial-key", "initial-value"));
148146
}
149147

150-
@Test
151-
public void writesRequestLogWithMDCEntries() throws Exception {
152-
Map<String, String> mdcAttributes = new HashMap<>();
153-
mdcAttributes.put("this-key", "this-value");
154-
mdcAttributes.put("that-key", "that-value");
155-
when(request.getAttribute(MDC.class.getName())).thenReturn(mdcAttributes);
156-
Map<String, String> contextMap = new HashMap<>();
157-
doAnswer(new Answer<Void>() {
158-
159-
@Override
160-
public Void answer(InvocationOnMock invocation) throws Throwable {
161-
contextMap.putAll(MDC.getCopyOfContextMap());
162-
return null;
163-
}
164-
}).when(loggingVisitor).logRequest(any(HttpServletRequest.class), any(HttpServletResponse.class));
165-
asyncListener.getValue().onComplete(mock(AsyncEvent.class));
166-
167-
assertThat(contextMap, both(hasEntry("that-key", "that-value")).and(hasEntry("this-key", "this-value")));
168-
169-
}
170148
}

0 commit comments

Comments
 (0)