-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix getRequest and getResponse calls after Response commit
Signed-off-by: Karsten Schnitter <[email protected]>
- Loading branch information
1 parent
abaee6a
commit 711de83
Showing
10 changed files
with
367 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ng-support-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.sap.hcp.cf.logging.servlet.filter; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
|
||
import com.sap.hcp.cf.logging.common.Defaults; | ||
import com.sap.hcp.cf.logging.common.Fields; | ||
import com.sap.hcp.cf.logging.common.HttpHeaders; | ||
import com.sap.hcp.cf.logging.common.LongValue; | ||
import com.sap.hcp.cf.logging.common.Markers; | ||
import com.sap.hcp.cf.logging.common.RequestRecord; | ||
|
||
public class RequestLogger { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RequestLogger.class); | ||
|
||
private HttpServletRequest httpRequest; | ||
private HttpServletResponse httpResponse; | ||
private RequestRecord requestRecord; | ||
|
||
public RequestLogger(RequestRecord requestRecord, HttpServletRequest httpRequest, | ||
HttpServletResponse httpResponse) { | ||
this.requestRecord = requestRecord; | ||
this.httpRequest = httpRequest; | ||
this.httpResponse = httpResponse; | ||
} | ||
|
||
public void logRequest() { | ||
requestRecord.stop(); | ||
addRequestHandlingParameters(); | ||
generateLog(); | ||
} | ||
|
||
private void addRequestHandlingParameters() { | ||
requestRecord.addValue(Fields.REQUEST_SIZE_B, new LongValue(httpRequest.getContentLength())); | ||
LongValue responseSize = getResponseSize(httpResponse); | ||
if (responseSize != null) { | ||
requestRecord.addValue(Fields.RESPONSE_SIZE_B, responseSize); | ||
} | ||
requestRecord.addTag(Fields.RESPONSE_CONTENT_TYPE, getValue(httpResponse.getHeader(HttpHeaders.CONTENT_TYPE))); | ||
requestRecord.addValue(Fields.RESPONSE_STATUS, new LongValue(httpResponse.getStatus())); | ||
} | ||
|
||
private LongValue getResponseSize(HttpServletResponse httpResponse) { | ||
String headerValue = httpResponse.getHeader(HttpHeaders.CONTENT_LENGTH); | ||
if (headerValue != null) { | ||
return new LongValue(Long.valueOf(headerValue)); | ||
} | ||
if (httpResponse != null && httpResponse instanceof ContentLengthTrackingResponseWrapper) { | ||
ContentLengthTrackingResponseWrapper wrapper = (ContentLengthTrackingResponseWrapper) httpResponse; | ||
return new LongValue(wrapper.getContentLength()); | ||
} | ||
return null; | ||
} | ||
|
||
private String getValue(String value) { | ||
return value != null ? value : Defaults.UNKNOWN; | ||
} | ||
|
||
private void generateLog() { | ||
Map<String, String> contextMap = getContextMap(); | ||
Map<String, String> currentContextMap = MDC.getCopyOfContextMap(); | ||
try { | ||
MDC.setContextMap(contextMap); | ||
LOG.info(Markers.REQUEST_MARKER, "{}", requestRecord); | ||
} finally { | ||
if (currentContextMap != null) { | ||
MDC.setContextMap(currentContextMap); | ||
} | ||
} | ||
} | ||
|
||
private Map<String, String> getContextMap() { | ||
try { | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> fromRequest = (Map<String, String>) httpRequest.getAttribute(MDC.class.getName()); | ||
return fromRequest != null ? fromRequest : Collections.emptyMap(); | ||
} catch (ClassCastException ignored) { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
...rt-servlet/src/main/java/com/sap/hcp/cf/logging/servlet/filter/RequestLoggingVisitor.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.