Update remaining filter with async support

Issue: SPR-9433
This commit is contained in:
Rossen Stoyanchev
2012-08-17 13:04:38 -04:00
parent cdab04a032
commit 4f55518290
10 changed files with 389 additions and 127 deletions

View File

@@ -18,8 +18,8 @@ package org.springframework.web.context.request;
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.async.AsyncWebUtils;
/**
* Request logging interceptor that adds a request context message to the
@@ -60,6 +60,11 @@ public class Log4jNestedDiagnosticContextInterceptor implements WebRequestInterc
* Adds a message the Log4J NDC before the request is processed.
*/
public void preHandle(WebRequest request) throws Exception {
if (AsyncWebUtils.getAsyncManager(request).hasConcurrentResult()) {
return;
}
NDC.push(getNestedDiagnosticContextMessage(request));
}

View File

@@ -198,19 +198,15 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
boolean isAsyncDispatch = isAsyncDispatch(request);
boolean isFirstRequest = !isAsyncDispatch(request);
if (isIncludePayload()) {
if (isAsyncDispatch) {
request = WebUtils.getNativeRequest(request, RequestCachingRequestWrapper.class);
Assert.notNull(request, "Expected wrapped request");
}
else {
if (isFirstRequest) {
request = new RequestCachingRequestWrapper(request);
}
}
if (!isAsyncDispatch) {
if (isFirstRequest) {
beforeRequest(request, getBeforeMessage(request));
}
try {

View File

@@ -66,25 +66,25 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ShallowEtagResponseWrapper responseWrapper;
boolean isFirstRequest = !isAsyncDispatch(request);
if (isAsyncDispatch(request)) {
responseWrapper = WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class);
Assert.notNull(responseWrapper, "Expected wrapped response");
}
else {
responseWrapper = new ShallowEtagResponseWrapper(response);
if (isFirstRequest) {
response = new ShallowEtagResponseWrapper(response);
}
filterChain.doFilter(request, responseWrapper);
filterChain.doFilter(request, response);
if (isLastRequestThread(request)) {
updateResponse(request, response, responseWrapper);
updateResponse(request, response);
}
}
private void updateResponse(HttpServletRequest request, HttpServletResponse response,
ShallowEtagResponseWrapper responseWrapper) throws IOException {
private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
ShallowEtagResponseWrapper responseWrapper = WebUtils.getNativeResponse(response, ShallowEtagResponseWrapper.class);
Assert.notNull(responseWrapper, "ShallowEtagResponseWrapper not found");
response = (HttpServletResponse) responseWrapper.getResponse();
byte[] body = responseWrapper.toByteArray();
int statusCode = responseWrapper.getStatusCode();