Skip to content

Commit 465ca24

Browse files
committed
DispatcherServlet logs request URI in encoded form only
Issue: SPR-11591
1 parent 5335288 commit 465ca24

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,6 @@
5757
import org.springframework.web.multipart.MultipartHttpServletRequest;
5858
import org.springframework.web.multipart.MultipartResolver;
5959
import org.springframework.web.util.NestedServletException;
60-
import org.springframework.web.util.UrlPathHelper;
6160
import org.springframework.web.util.WebUtils;
6261

6362
/**
@@ -246,8 +245,6 @@ public class DispatcherServlet extends FrameworkServlet {
246245
/** Additional logger to use when no mapped handler is found for a request. */
247246
protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);
248247

249-
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
250-
251248
private static final Properties defaultStrategies;
252249

253250
static {
@@ -838,17 +835,15 @@ protected Object createDefaultStrategy(ApplicationContext context, Class<?> claz
838835
@Override
839836
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
840837
if (logger.isDebugEnabled()) {
841-
String requestUri = urlPathHelper.getRequestUri(request);
842838
String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : "";
843839
logger.debug("DispatcherServlet with name '" + getServletName() + "'" + resumed +
844-
" processing " + request.getMethod() + " request for [" + requestUri + "]");
840+
" processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]");
845841
}
846842

847843
// Keep a snapshot of the request attributes in case of an include,
848844
// to be able to restore the original attributes after the include.
849845
Map<String, Object> attributesSnapshot = null;
850846
if (WebUtils.isIncludeRequest(request)) {
851-
logger.debug("Taking snapshot of request attributes before include");
852847
attributesSnapshot = new HashMap<String, Object>();
853848
Enumeration<?> attrNames = request.getAttributeNames();
854849
while (attrNames.hasMoreElements()) {
@@ -928,8 +923,7 @@ protected void doDispatch(HttpServletRequest request, HttpServletResponse respon
928923
if (isGet || "HEAD".equals(method)) {
929924
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
930925
if (logger.isDebugEnabled()) {
931-
String requestUri = urlPathHelper.getRequestUri(request);
932-
logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
926+
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
933927
}
934928
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
935929
return;
@@ -1114,15 +1108,15 @@ protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Ex
11141108
*/
11151109
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
11161110
if (pageNotFoundLogger.isWarnEnabled()) {
1117-
String requestUri = urlPathHelper.getRequestUri(request);
1118-
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
1111+
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) +
11191112
"] in DispatcherServlet with name '" + getServletName() + "'");
11201113
}
1121-
if(throwExceptionIfNoHandlerFound) {
1114+
if (throwExceptionIfNoHandlerFound) {
11221115
ServletServerHttpRequest req = new ServletServerHttpRequest(request);
11231116
throw new NoHandlerFoundException(req.getMethod().name(),
11241117
req.getServletRequest().getRequestURI(),req.getHeaders());
1125-
} else {
1118+
}
1119+
else {
11261120
response.sendError(HttpServletResponse.SC_NOT_FOUND);
11271121
}
11281122
}
@@ -1203,9 +1197,8 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
12031197
// We need to resolve the view name.
12041198
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
12051199
if (view == null) {
1206-
throw new ServletException(
1207-
"Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" +
1208-
getServletName() + "'");
1200+
throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
1201+
"' in servlet with name '" + getServletName() + "'");
12091202
}
12101203
}
12111204
else {
@@ -1226,8 +1219,8 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
12261219
}
12271220
catch (Exception ex) {
12281221
if (logger.isDebugEnabled()) {
1229-
logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '"
1230-
+ getServletName() + "'", ex);
1222+
logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" +
1223+
getServletName() + "'", ex);
12311224
}
12321225
throw ex;
12331226
}
@@ -1295,8 +1288,6 @@ private void triggerAfterCompletionWithError(HttpServletRequest request, HttpSer
12951288
*/
12961289
@SuppressWarnings("unchecked")
12971290
private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?,?> attributesSnapshot) {
1298-
logger.debug("Restoring snapshot of request attributes after include");
1299-
13001291
// Need to copy into separate Collection here, to avoid side effects
13011292
// on the Enumeration when removing attributes.
13021293
Set<String> attrsToCheck = new HashSet<String>();
@@ -1316,18 +1307,20 @@ private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?,?>
13161307
for (String attrName : attrsToCheck) {
13171308
Object attrValue = attributesSnapshot.get(attrName);
13181309
if (attrValue == null){
1319-
if (logger.isDebugEnabled()) {
1320-
logger.debug("Removing attribute [" + attrName + "] after include");
1321-
}
13221310
request.removeAttribute(attrName);
13231311
}
13241312
else if (attrValue != request.getAttribute(attrName)) {
1325-
if (logger.isDebugEnabled()) {
1326-
logger.debug("Restoring original value of attribute [" + attrName + "] after include");
1327-
}
13281313
request.setAttribute(attrName, attrValue);
13291314
}
13301315
}
13311316
}
13321317

1318+
private static String getRequestUri(HttpServletRequest request) {
1319+
String uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
1320+
if (uri == null) {
1321+
uri = request.getRequestURI();
1322+
}
1323+
return uri;
1324+
}
1325+
13331326
}

0 commit comments

Comments
 (0)