Skip to content

Commit a2bdc28

Browse files
committed
DispatcherServlet logs request URI in encoded form only
Issue: SPR-11591 (cherry picked from commit 465ca24)
1 parent dbd5f67 commit a2bdc28

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

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

Lines changed: 24 additions & 23 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.
@@ -56,7 +56,6 @@
5656
import org.springframework.web.multipart.MultipartHttpServletRequest;
5757
import org.springframework.web.multipart.MultipartResolver;
5858
import org.springframework.web.util.NestedServletException;
59-
import org.springframework.web.util.UrlPathHelper;
6059
import org.springframework.web.util.WebUtils;
6160

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

248-
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
249-
250247
private static final Properties defaultStrategies;
251248

252249
static {
@@ -818,17 +815,15 @@ protected Object createDefaultStrategy(ApplicationContext context, Class<?> claz
818815
@Override
819816
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
820817
if (logger.isDebugEnabled()) {
821-
String requestUri = urlPathHelper.getRequestUri(request);
822818
String resumed = WebAsyncUtils.getAsyncManager(request).hasConcurrentResult() ? " resumed" : "";
823819
logger.debug("DispatcherServlet with name '" + getServletName() + "'" + resumed +
824-
" processing " + request.getMethod() + " request for [" + requestUri + "]");
820+
" processing " + request.getMethod() + " request for [" + getRequestUri(request) + "]");
825821
}
826822

827823
// Keep a snapshot of the request attributes in case of an include,
828824
// to be able to restore the original attributes after the include.
829825
Map<String, Object> attributesSnapshot = null;
830826
if (WebUtils.isIncludeRequest(request)) {
831-
logger.debug("Taking snapshot of request attributes before include");
832827
attributesSnapshot = new HashMap<String, Object>();
833828
Enumeration<?> attrNames = request.getAttributeNames();
834829
while (attrNames.hasMoreElements()) {
@@ -908,8 +903,7 @@ protected void doDispatch(HttpServletRequest request, HttpServletResponse respon
908903
if (isGet || "HEAD".equals(method)) {
909904
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
910905
if (logger.isDebugEnabled()) {
911-
String requestUri = urlPathHelper.getRequestUri(request);
912-
logger.debug("Last-Modified value for [" + requestUri + "] is: " + lastModified);
906+
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
913907
}
914908
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
915909
return;
@@ -1104,8 +1098,7 @@ protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Ex
11041098
*/
11051099
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
11061100
if (pageNotFoundLogger.isWarnEnabled()) {
1107-
String requestUri = urlPathHelper.getRequestUri(request);
1108-
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
1101+
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + getRequestUri(request) +
11091102
"] in DispatcherServlet with name '" + getServletName() + "'");
11101103
}
11111104
response.sendError(HttpServletResponse.SC_NOT_FOUND);
@@ -1187,9 +1180,8 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
11871180
// We need to resolve the view name.
11881181
view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
11891182
if (view == null) {
1190-
throw new ServletException(
1191-
"Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" +
1192-
getServletName() + "'");
1183+
throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
1184+
"' in servlet with name '" + getServletName() + "'");
11931185
}
11941186
}
11951187
else {
@@ -1205,7 +1197,16 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
12051197
if (logger.isDebugEnabled()) {
12061198
logger.debug("Rendering view [" + view + "] in DispatcherServlet with name '" + getServletName() + "'");
12071199
}
1208-
view.render(mv.getModelInternal(), request, response);
1200+
try {
1201+
view.render(mv.getModelInternal(), request, response);
1202+
}
1203+
catch (Exception ex) {
1204+
if (logger.isDebugEnabled()) {
1205+
logger.debug("Error rendering view [" + view + "] in DispatcherServlet with name '" +
1206+
getServletName() + "'", ex);
1207+
}
1208+
throw ex;
1209+
}
12091210
}
12101211

12111212
/**
@@ -1270,8 +1271,6 @@ private void triggerAfterCompletionWithError(HttpServletRequest request, HttpSer
12701271
*/
12711272
@SuppressWarnings("unchecked")
12721273
private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?,?> attributesSnapshot) {
1273-
logger.debug("Restoring snapshot of request attributes after include");
1274-
12751274
// Need to copy into separate Collection here, to avoid side effects
12761275
// on the Enumeration when removing attributes.
12771276
Set<String> attrsToCheck = new HashSet<String>();
@@ -1291,18 +1290,20 @@ private void restoreAttributesAfterInclude(HttpServletRequest request, Map<?,?>
12911290
for (String attrName : attrsToCheck) {
12921291
Object attrValue = attributesSnapshot.get(attrName);
12931292
if (attrValue == null){
1294-
if (logger.isDebugEnabled()) {
1295-
logger.debug("Removing attribute [" + attrName + "] after include");
1296-
}
12971293
request.removeAttribute(attrName);
12981294
}
12991295
else if (attrValue != request.getAttribute(attrName)) {
1300-
if (logger.isDebugEnabled()) {
1301-
logger.debug("Restoring original value of attribute [" + attrName + "] after include");
1302-
}
13031296
request.setAttribute(attrName, attrValue);
13041297
}
13051298
}
13061299
}
13071300

1301+
private static String getRequestUri(HttpServletRequest request) {
1302+
String uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
1303+
if (uri == null) {
1304+
uri = request.getRequestURI();
1305+
}
1306+
return uri;
1307+
}
1308+
13081309
}

0 commit comments

Comments
 (0)