Browse Source

Polishing

pull/510/head
Juergen Hoeller 12 years ago
parent
commit
623b1fc0d5
  1. 9
      spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java
  2. 19
      spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java
  3. 102
      spring-web/src/main/java/org/springframework/web/util/UriUtils.java
  4. 7
      spring-webmvc-tiles3/src/main/java/org/springframework/web/servlet/view/tiles3/TilesConfigurer.java
  5. 82
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java
  6. 4
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java
  7. 25
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

9
spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -90,7 +90,7 @@ public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean<O
pf.copyFrom(this); pf.copyFrom(this);
pf.setTargetSource(this.scopedTargetSource); pf.setTargetSource(this.scopedTargetSource);
Class beanType = beanFactory.getType(this.targetBeanName); Class<?> beanType = beanFactory.getType(this.targetBeanName);
if (beanType == null) { if (beanType == null) {
throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName + throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
"': Target type could not be determined at the time of proxy creation."); "': Target type could not be determined at the time of proxy creation.");
@ -122,10 +122,7 @@ public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean<O
if (this.proxy != null) { if (this.proxy != null) {
return this.proxy.getClass(); return this.proxy.getClass();
} }
if (this.scopedTargetSource != null) { return this.scopedTargetSource.getTargetClass();
return this.scopedTargetSource.getTargetClass();
}
return null;
} }
public boolean isSingleton() { public boolean isSingleton() {

19
spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -176,13 +176,14 @@ final class HierarchicalUriComponents extends UriComponents {
*/ */
@Override @Override
public HierarchicalUriComponents encode(String encoding) throws UnsupportedEncodingException { public HierarchicalUriComponents encode(String encoding) throws UnsupportedEncodingException {
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
if (this.encoded) { if (this.encoded) {
return this; return this;
} }
String encodedScheme = encodeUriComponent(this.getScheme(), encoding, Type.SCHEME); String encodedScheme = encodeUriComponent(getScheme(), encoding, Type.SCHEME);
String encodedUserInfo = encodeUriComponent(this.userInfo, encoding, Type.USER_INFO); String encodedUserInfo = encodeUriComponent(this.userInfo, encoding, Type.USER_INFO);
String encodedHost = encodeUriComponent(this.host, encoding, Type.HOST); String encodedHost = encodeUriComponent(this.host, encoding, Type.HOST);
PathComponent encodedPath = this.path.encode(encoding); PathComponent encodedPath = this.path.encode(encoding);
MultiValueMap<String, String> encodedQueryParams = MultiValueMap<String, String> encodedQueryParams =
new LinkedMultiValueMap<String, String>(this.queryParams.size()); new LinkedMultiValueMap<String, String>(this.queryParams.size());
@ -213,14 +214,14 @@ final class HierarchicalUriComponents extends UriComponents {
if (source == null) { if (source == null) {
return null; return null;
} }
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
byte[] bytes = encodeBytes(source.getBytes(encoding), type); byte[] bytes = encodeBytes(source.getBytes(encoding), type);
return new String(bytes, "US-ASCII"); return new String(bytes, "US-ASCII");
} }
private static byte[] encodeBytes(byte[] source, Type type) { private static byte[] encodeBytes(byte[] source, Type type) {
Assert.notNull(source, "'source' must not be null"); Assert.notNull(source, "Source must not be null");
Assert.notNull(type, "'type' must not be null"); Assert.notNull(type, "Type must not be null");
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length); ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length);
for (byte b : source) { for (byte b : source) {
if (b < 0) { if (b < 0) {
@ -253,8 +254,8 @@ final class HierarchicalUriComponents extends UriComponents {
return; return;
} }
verifyUriComponent(getScheme(), Type.SCHEME); verifyUriComponent(getScheme(), Type.SCHEME);
verifyUriComponent(userInfo, Type.USER_INFO); verifyUriComponent(this.userInfo, Type.USER_INFO);
verifyUriComponent(host, Type.HOST); verifyUriComponent(this.host, Type.HOST);
this.path.verify(); this.path.verify();
for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) { for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
verifyUriComponent(entry.getKey(), Type.QUERY_PARAM); verifyUriComponent(entry.getKey(), Type.QUERY_PARAM);
@ -300,7 +301,7 @@ final class HierarchicalUriComponents extends UriComponents {
@Override @Override
protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVariables) { protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVariables) {
Assert.state(!this.encoded, "Cannot expand an already encoded UriComponents object"); Assert.state(!this.encoded, "Cannot expand an already encoded UriComponents object");
String expandedScheme = expandUriComponent(this.getScheme(), uriVariables); String expandedScheme = expandUriComponent(getScheme(), uriVariables);
String expandedUserInfo = expandUriComponent(this.userInfo, uriVariables); String expandedUserInfo = expandUriComponent(this.userInfo, uriVariables);
String expandedHost = expandUriComponent(this.host, uriVariables); String expandedHost = expandUriComponent(this.host, uriVariables);
PathComponent expandedPath = this.path.expand(uriVariables); PathComponent expandedPath = this.path.expand(uriVariables);

102
spring-web/src/main/java/org/springframework/web/util/UriUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,14 +24,15 @@ import java.util.regex.Pattern;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for the various URI * Utility class for URI encoding and decoding based on RFC 3986.
* components. * Offers encoding methods for the various URI components.
* *
* <p>All {@code encode*(String, String} methods in this class operate in a similar way: * <p>All {@code encode*(String, String} methods in this class operate in a similar way:
* <ul> * <ul>
* <li>Valid characters for the specific URI component as defined in RFC 3986 stay the same.</li> * <li>Valid characters for the specific URI component as defined in RFC 3986 stay the same.</li>
* <li>All other characters are converted into one or more bytes in the given encoding scheme. Each of the * <li>All other characters are converted into one or more bytes in the given encoding scheme.
* resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}" format.</li> * Each of the resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}"
* format.</li>
* </ul> * </ul>
* *
* @author Arjen Poutsma * @author Arjen Poutsma
@ -65,6 +66,7 @@ public abstract class UriUtils {
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" + "^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" +
PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?"); PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
// encoding // encoding
/** /**
@ -73,7 +75,7 @@ public abstract class UriUtils {
* <p><strong>Note</strong> that this method does not attempt to encode "=" and "&" * <p><strong>Note</strong> that this method does not attempt to encode "=" and "&"
* characters in query parameter names and query parameter values because they cannot * characters in query parameter names and query parameter values because they cannot
* be parsed in a reliable way. Instead use: * be parsed in a reliable way. Instead use:
* <pre> * <pre class="code">
* UriComponents uriComponents = UriComponentsBuilder.fromUri("/path?name={value}").buildAndExpand("a=b"); * UriComponents uriComponents = UriComponentsBuilder.fromUri("/path?name={value}").buildAndExpand("a=b");
* String encodedUri = uriComponents.encode().toUriString(); * String encodedUri = uriComponents.encode().toUriString();
* </pre> * </pre>
@ -86,19 +88,18 @@ public abstract class UriUtils {
*/ */
@Deprecated @Deprecated
public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException { public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException {
Assert.notNull(uri, "'uri' must not be null"); Assert.notNull(uri, "URI must not be null");
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
Matcher m = URI_PATTERN.matcher(uri); Matcher matcher = URI_PATTERN.matcher(uri);
if (m.matches()) { if (matcher.matches()) {
String scheme = m.group(2); String scheme = matcher.group(2);
String authority = m.group(3); String authority = matcher.group(3);
String userinfo = m.group(5); String userinfo = matcher.group(5);
String host = m.group(6); String host = matcher.group(6);
String port = m.group(8); String port = matcher.group(8);
String path = m.group(9); String path = matcher.group(9);
String query = m.group(11); String query = matcher.group(11);
String fragment = m.group(13); String fragment = matcher.group(13);
return encodeUriComponents(scheme, authority, userinfo, host, port, path, query, fragment, encoding); return encodeUriComponents(scheme, authority, userinfo, host, port, path, query, fragment, encoding);
} }
else { else {
@ -114,7 +115,7 @@ public abstract class UriUtils {
* <p><strong>Note</strong> that this method does not attempt to encode "=" and "&" * <p><strong>Note</strong> that this method does not attempt to encode "=" and "&"
* characters in query parameter names and query parameter values because they cannot * characters in query parameter names and query parameter values because they cannot
* be parsed in a reliable way. Instead use: * be parsed in a reliable way. Instead use:
* <pre> * <pre class="code">
* UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl("/path?name={value}").buildAndExpand("a=b"); * UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl("/path?name={value}").buildAndExpand("a=b");
* String encodedUri = uriComponents.encode().toUriString(); * String encodedUri = uriComponents.encode().toUriString();
* </pre> * </pre>
@ -127,18 +128,17 @@ public abstract class UriUtils {
*/ */
@Deprecated @Deprecated
public static String encodeHttpUrl(String httpUrl, String encoding) throws UnsupportedEncodingException { public static String encodeHttpUrl(String httpUrl, String encoding) throws UnsupportedEncodingException {
Assert.notNull(httpUrl, "'httpUrl' must not be null"); Assert.notNull(httpUrl, "HTTP URL must not be null");
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
Matcher m = HTTP_URL_PATTERN.matcher(httpUrl); Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
if (m.matches()) { if (matcher.matches()) {
String scheme = m.group(1); String scheme = matcher.group(1);
String authority = m.group(2); String authority = matcher.group(2);
String userinfo = m.group(4); String userinfo = matcher.group(4);
String host = m.group(5); String host = matcher.group(5);
String portString = m.group(7); String portString = matcher.group(7);
String path = m.group(8); String path = matcher.group(8);
String query = m.group(10); String query = matcher.group(10);
return encodeUriComponents(scheme, authority, userinfo, host, portString, path, query, null, encoding); return encodeUriComponents(scheme, authority, userinfo, host, portString, path, query, null, encoding);
} }
else { else {
@ -168,7 +168,7 @@ public abstract class UriUtils {
String host, String port, String path, String query, String fragment, String encoding) String host, String port, String path, String query, String fragment, String encoding)
throws UnsupportedEncodingException { throws UnsupportedEncodingException {
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (scheme != null) { if (scheme != null) {
@ -217,8 +217,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException { public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(scheme, encoding, return HierarchicalUriComponents.encodeUriComponent(scheme, encoding, HierarchicalUriComponents.Type.SCHEME);
HierarchicalUriComponents.Type.SCHEME);
} }
/** /**
@ -229,8 +228,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException { public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(authority, encoding, return HierarchicalUriComponents.encodeUriComponent(authority, encoding, HierarchicalUriComponents.Type.AUTHORITY);
HierarchicalUriComponents.Type.AUTHORITY);
} }
/** /**
@ -241,8 +239,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException { public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding, return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding, HierarchicalUriComponents.Type.USER_INFO);
HierarchicalUriComponents.Type.USER_INFO);
} }
/** /**
@ -253,8 +250,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException { public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents return HierarchicalUriComponents.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST);
.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST);
} }
/** /**
@ -265,8 +261,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodePort(String port, String encoding) throws UnsupportedEncodingException { public static String encodePort(String port, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents return HierarchicalUriComponents.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
} }
/** /**
@ -277,8 +272,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodePath(String path, String encoding) throws UnsupportedEncodingException { public static String encodePath(String path, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents return HierarchicalUriComponents.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
} }
/** /**
@ -289,8 +283,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException { public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(segment, encoding, return HierarchicalUriComponents.encodeUriComponent(segment, encoding, HierarchicalUriComponents.Type.PATH_SEGMENT);
HierarchicalUriComponents.Type.PATH_SEGMENT);
} }
/** /**
@ -301,8 +294,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException { public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents return HierarchicalUriComponents.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
} }
/** /**
@ -313,8 +305,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException { public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding, return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding, HierarchicalUriComponents.Type.QUERY_PARAM);
HierarchicalUriComponents.Type.QUERY_PARAM);
} }
/** /**
@ -325,8 +316,7 @@ public abstract class UriUtils {
* @throws UnsupportedEncodingException when the given encoding parameter is not supported * @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/ */
public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException { public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException {
return HierarchicalUriComponents.encodeUriComponent(fragment, encoding, return HierarchicalUriComponents.encodeUriComponent(fragment, encoding, HierarchicalUriComponents.Type.FRAGMENT);
HierarchicalUriComponents.Type.FRAGMENT);
} }
@ -348,8 +338,8 @@ public abstract class UriUtils {
* @see java.net.URLDecoder#decode(String, String) * @see java.net.URLDecoder#decode(String, String)
*/ */
public static String decode(String source, String encoding) throws UnsupportedEncodingException { public static String decode(String source, String encoding) throws UnsupportedEncodingException {
Assert.notNull(source, "'source' must not be null"); Assert.notNull(source, "Source must not be null");
Assert.hasLength(encoding, "'encoding' must not be empty"); Assert.hasLength(encoding, "Encoding must not be empty");
int length = source.length(); int length = source.length();
ByteArrayOutputStream bos = new ByteArrayOutputStream(length); ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
boolean changed = false; boolean changed = false;
@ -376,7 +366,7 @@ public abstract class UriUtils {
bos.write(ch); bos.write(ch);
} }
} }
return changed ? new String(bos.toByteArray(), encoding) : source; return (changed ? new String(bos.toByteArray(), encoding) : source);
} }
} }

7
spring-webmvc-tiles3/src/main/java/org/springframework/web/servlet/view/tiles3/TilesConfigurer.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -299,7 +299,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
LocaleResolver resolver) { LocaleResolver resolver) {
BaseLocaleUrlDefinitionDAO dao = super.instantiateLocaleDefinitionDao(applicationContext, resolver); BaseLocaleUrlDefinitionDAO dao = super.instantiateLocaleDefinitionDao(applicationContext, resolver);
if (checkRefresh && dao instanceof CachingLocaleUrlDefinitionDAO) { if (checkRefresh && dao instanceof CachingLocaleUrlDefinitionDAO) {
((CachingLocaleUrlDefinitionDAO) dao).setCheckRefresh(checkRefresh); ((CachingLocaleUrlDefinitionDAO) dao).setCheckRefresh(true);
} }
return dao; return dao;
} }
@ -338,7 +338,8 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
protected PreparerFactory createPreparerFactory(ApplicationContext context) { protected PreparerFactory createPreparerFactory(ApplicationContext context) {
if (preparerFactoryClass != null) { if (preparerFactoryClass != null) {
return BeanUtils.instantiate(preparerFactoryClass); return BeanUtils.instantiate(preparerFactoryClass);
} else { }
else {
return super.createPreparerFactory(context); return super.createPreparerFactory(context);
} }
} }

82
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.handler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
@ -203,29 +202,28 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* <p>Will be invoked before {@link #initInterceptors()} adapts the specified * <p>Will be invoked before {@link #initInterceptors()} adapts the specified
* interceptors into {@link HandlerInterceptor} instances. * interceptors into {@link HandlerInterceptor} instances.
* <p>The default implementation is empty. * <p>The default implementation is empty.
* @param interceptors the configured interceptor List (never {@code null}), * @param interceptors the configured interceptor List (never {@code null}), allowing
* allowing to add further interceptors before as well as after the existing * to add further interceptors before as well as after the existing interceptors
* interceptors
*/ */
protected void extendInterceptors(List<Object> interceptors) { protected void extendInterceptors(List<Object> interceptors) {
} }
/** /**
* Detects beans of type {@link MappedInterceptor} and adds them to the list of mapped interceptors. * Detect beans of type {@link MappedInterceptor} and add them to the list of mapped interceptors.
* This is done in addition to any {@link MappedInterceptor}s that may have been provided via * <p>This is called in addition to any {@link MappedInterceptor}s that may have been provided
* {@link #setInterceptors(Object[])}. Subclasses can override this method to change that. * via {@link #setInterceptors}, by default adding all beans of type {@link MappedInterceptor}
* * from the current context and its ancestors. Subclasses can override and refine this policy.
* @param mappedInterceptors an empty list to add MappedInterceptor types to * @param mappedInterceptors an empty list to add {@link MappedInterceptor} instances to
*/ */
protected void detectMappedInterceptors(List<MappedInterceptor> mappedInterceptors) { protected void detectMappedInterceptors(List<MappedInterceptor> mappedInterceptors) {
mappedInterceptors.addAll( mappedInterceptors.addAll(
BeanFactoryUtils.beansOfTypeIncludingAncestors( BeanFactoryUtils.beansOfTypeIncludingAncestors(
getApplicationContext(),MappedInterceptor.class, true, false).values()); getApplicationContext(), MappedInterceptor.class, true, false).values());
} }
/** /**
* Initialize the specified interceptors, checking for {@link MappedInterceptor}s and adapting * Initialize the specified interceptors, checking for {@link MappedInterceptor}s and
* HandlerInterceptors where necessary. * adapting {@link HandlerInterceptor}s and {@link WebRequestInterceptor}s if necessary.
* @see #setInterceptors * @see #setInterceptors
* @see #adaptInterceptor * @see #adaptInterceptor
*/ */
@ -237,19 +235,20 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null"); throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
} }
if (interceptor instanceof MappedInterceptor) { if (interceptor instanceof MappedInterceptor) {
mappedInterceptors.add((MappedInterceptor) interceptor); this.mappedInterceptors.add((MappedInterceptor) interceptor);
} }
else { else {
adaptedInterceptors.add(adaptInterceptor(interceptor)); this.adaptedInterceptors.add(adaptInterceptor(interceptor));
} }
} }
} }
} }
/** /**
* Adapt the given interceptor object to the HandlerInterceptor interface. * Adapt the given interceptor object to the {@link HandlerInterceptor} interface.
* <p>Supported interceptor types are HandlerInterceptor and WebRequestInterceptor. * <p>By default, the supported interceptor types are {@link HandlerInterceptor}
* Each given WebRequestInterceptor will be wrapped in a WebRequestHandlerInterceptorAdapter. * and {@link WebRequestInterceptor}. Each given {@link WebRequestInterceptor}
* will be wrapped in a {@link WebRequestHandlerInterceptorAdapter}.
* Can be overridden in subclasses. * Can be overridden in subclasses.
* @param interceptor the specified interceptor object * @param interceptor the specified interceptor object
* @return the interceptor wrapped as HandlerInterceptor * @return the interceptor wrapped as HandlerInterceptor
@ -270,12 +269,12 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
} }
/** /**
* Return the adapted interceptors as HandlerInterceptor array. * Return the adapted interceptors as {@link HandlerInterceptor} array.
* @return the array of HandlerInterceptors, or {@code null} if none * @return the array of {@link HandlerInterceptor}s, or {@code null} if none
*/ */
protected final HandlerInterceptor[] getAdaptedInterceptors() { protected final HandlerInterceptor[] getAdaptedInterceptors() {
int count = adaptedInterceptors.size(); int count = this.adaptedInterceptors.size();
return (count > 0) ? adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null; return (count > 0 ? this.adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null);
} }
/** /**
@ -283,8 +282,8 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* @return the array of {@link MappedInterceptor}s, or {@code null} if none * @return the array of {@link MappedInterceptor}s, or {@code null} if none
*/ */
protected final MappedInterceptor[] getMappedInterceptors() { protected final MappedInterceptor[] getMappedInterceptors() {
int count = mappedInterceptors.size(); int count = this.mappedInterceptors.size();
return (count > 0) ? mappedInterceptors.toArray(new MappedInterceptor[count]) : null; return (count > 0 ? this.mappedInterceptors.toArray(new MappedInterceptor[count]) : null);
} }
/** /**
@ -324,31 +323,32 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception; protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception;
/** /**
* Build a HandlerExecutionChain for the given handler, including applicable interceptors. * Build a {@link HandlerExecutionChain} for the given handler, including
* <p>The default implementation simply builds a standard HandlerExecutionChain with * applicable interceptors.
* the given handler, the handler mapping's common interceptors, and any {@link MappedInterceptor}s * <p>The default implementation builds a standard {@link HandlerExecutionChain}
* matching to the current request URL. Subclasses may * with the given handler, the handler mapping's common interceptors, and any
* override this in order to extend/rearrange the list of interceptors. * {@link MappedInterceptor}s matching to the current request URL. Subclasses
* <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a pre-built * may override this in order to extend/rearrange the list of interceptors.
* HandlerExecutionChain. This method should handle those two cases explicitly, * <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a
* either building a new HandlerExecutionChain or extending the existing chain. * pre-built {@link HandlerExecutionChain}. This method should handle those
* <p>For simply adding an interceptor, consider calling {@code super.getHandlerExecutionChain} * two cases explicitly, either building a new {@link HandlerExecutionChain}
* and invoking {@link HandlerExecutionChain#addInterceptor} on the returned chain object. * or extending the existing chain.
* <p>For simply adding an interceptor in a custom subclass, consider calling
* {@code super.getHandlerExecutionChain(handler, request)} and invoking
* {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
* @param handler the resolved handler instance (never {@code null}) * @param handler the resolved handler instance (never {@code null})
* @param request current HTTP request * @param request current HTTP request
* @return the HandlerExecutionChain (never {@code null}) * @return the HandlerExecutionChain (never {@code null})
* @see #getAdaptedInterceptors() * @see #getAdaptedInterceptors()
*/ */
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) { protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
HandlerExecutionChain chain = HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
(handler instanceof HandlerExecutionChain) ? (HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler);
chain.addInterceptors(getAdaptedInterceptors()); chain.addInterceptors(getAdaptedInterceptors());
String lookupPath = urlPathHelper.getLookupPathForRequest(request); String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
for (MappedInterceptor mappedInterceptor : mappedInterceptors) { for (MappedInterceptor mappedInterceptor : this.mappedInterceptors) {
if (mappedInterceptor.matches(lookupPath, pathMatcher)) { if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
chain.addInterceptor(mappedInterceptor.getInterceptor()); chain.addInterceptor(mappedInterceptor.getInterceptor());
} }
} }

4
spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -439,7 +439,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
BaseLocaleUrlDefinitionDAO dao = super.instantiateLocaleDefinitionDao( BaseLocaleUrlDefinitionDAO dao = super.instantiateLocaleDefinitionDao(
applicationContext, contextFactory, resolver); applicationContext, contextFactory, resolver);
if (checkRefresh && dao instanceof CachingLocaleUrlDefinitionDAO) { if (checkRefresh && dao instanceof CachingLocaleUrlDefinitionDAO) {
((CachingLocaleUrlDefinitionDAO) dao).setCheckRefresh(checkRefresh); ((CachingLocaleUrlDefinitionDAO) dao).setCheckRefresh(true);
} }
return dao; return dao;
} }

25
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,15 +16,13 @@
package org.springframework.web.servlet.mvc.method.annotation; package org.springframework.web.servlet.mvc.method.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -41,6 +39,8 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap; import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import static org.junit.Assert.*;
/** /**
* Unit tests for {@link RequestMappingHandlerAdapter}. * Unit tests for {@link RequestMappingHandlerAdapter}.
* *
@ -66,6 +66,7 @@ public class RequestMappingHandlerAdapterTests {
private StaticWebApplicationContext webAppContext; private StaticWebApplicationContext webAppContext;
@BeforeClass @BeforeClass
public static void setupOnce() { public static void setupOnce() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter(); RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
@ -86,23 +87,24 @@ public class RequestMappingHandlerAdapterTests {
this.response = new MockHttpServletResponse(); this.response = new MockHttpServletResponse();
} }
@Test @Test
public void cacheControlWithoutSessionAttributes() throws Exception { public void cacheControlWithoutSessionAttributes() throws Exception {
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle"); HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
this.handlerAdapter.afterPropertiesSet();
this.handlerAdapter.setCacheSeconds(100); this.handlerAdapter.setCacheSeconds(100);
this.handlerAdapter.handle(this.request, this.response, handlerMethod); this.handlerAdapter.afterPropertiesSet();
assertTrue(response.getHeader("Cache-Control").toString().contains("max-age")); this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertTrue(response.getHeader("Cache-Control").contains("max-age"));
} }
@Test @Test
public void cacheControlWithSessionAttributes() throws Exception { public void cacheControlWithSessionAttributes() throws Exception {
SessionAttributeController handler = new SessionAttributeController(); SessionAttributeController handler = new SessionAttributeController();
this.handlerAdapter.afterPropertiesSet();
this.handlerAdapter.setCacheSeconds(100); this.handlerAdapter.setCacheSeconds(100);
this.handlerAdapter.handle(this.request, this.response, handlerMethod(handler, "handle")); this.handlerAdapter.afterPropertiesSet();
this.handlerAdapter.handle(this.request, this.response, handlerMethod(handler, "handle"));
assertEquals("no-cache", this.response.getHeader("Cache-Control")); assertEquals("no-cache", this.response.getHeader("Cache-Control"));
} }
@ -211,6 +213,7 @@ public class RequestMappingHandlerAdapterTests {
} }
} }
@SessionAttributes("attr1") @SessionAttributes("attr1")
private static class SessionAttributeController { private static class SessionAttributeController {
@ -219,6 +222,7 @@ public class RequestMappingHandlerAdapterTests {
} }
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class RedirectAttributeController { private static class RedirectAttributeController {
@ -228,6 +232,7 @@ public class RequestMappingHandlerAdapterTests {
} }
} }
@ControllerAdvice @ControllerAdvice
private static class ModelAttributeAdvice { private static class ModelAttributeAdvice {
@ -238,4 +243,4 @@ public class RequestMappingHandlerAdapterTests {
} }
} }
} }

Loading…
Cancel
Save