Browse Source

Polishing

pull/1160/head
Juergen Hoeller 10 years ago
parent
commit
521c41d75e
  1. 7
      spring-web/src/main/java/org/springframework/web/accept/AbstractMappingContentNegotiationStrategy.java
  2. 12
      spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java
  3. 38
      spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java
  4. 6
      spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationStrategy.java
  5. 5
      spring-web/src/main/java/org/springframework/web/accept/FixedContentNegotiationStrategy.java
  6. 6
      spring-web/src/main/java/org/springframework/web/accept/HeaderContentNegotiationStrategy.java
  7. 4
      spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java
  8. 7
      spring-web/src/main/java/org/springframework/web/accept/MediaTypeFileExtensionResolver.java
  9. 22
      spring-web/src/main/java/org/springframework/web/accept/ParameterContentNegotiationStrategy.java
  10. 3
      spring-web/src/main/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategy.java
  11. 12
      spring-web/src/main/java/org/springframework/web/accept/ServletPathExtensionContentNegotiationStrategy.java
  12. 4
      spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java
  13. 4
      spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java

7
spring-web/src/main/java/org/springframework/web/accept/AbstractMappingContentNegotiationStrategy.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -43,11 +43,9 @@ import org.springframework.web.context.request.NativeWebRequest;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public abstract class AbstractMappingContentNegotiationStrategy public abstract class AbstractMappingContentNegotiationStrategy extends MappingMediaTypeFileExtensionResolver
extends MappingMediaTypeFileExtensionResolver
implements ContentNegotiationStrategy { implements ContentNegotiationStrategy {
/** /**
* Create an instance with the given map of file extensions and media types. * Create an instance with the given map of file extensions and media types.
*/ */
@ -86,6 +84,7 @@ public abstract class AbstractMappingContentNegotiationStrategy
return Collections.emptyList(); return Collections.emptyList();
} }
/** /**
* Extract a key from the request to use to look up media types. * Extract a key from the request to use to look up media types.
* @return the lookup key or {@code null}. * @return the lookup key or {@code null}.

12
spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java

@ -41,18 +41,14 @@ import org.springframework.web.context.request.NativeWebRequest;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public class ContentNegotiationManager implements ContentNegotiationStrategy, public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
MediaTypeFileExtensionResolver {
private static final List<MediaType> MEDIA_TYPE_ALL = private static final List<MediaType> MEDIA_TYPE_ALL = Collections.<MediaType>singletonList(MediaType.ALL);
Collections.<MediaType>singletonList(MediaType.ALL);
private final List<ContentNegotiationStrategy> strategies = private final List<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();
new ArrayList<ContentNegotiationStrategy>();
private final Set<MediaTypeFileExtensionResolver> resolvers = private final Set<MediaTypeFileExtensionResolver> resolvers = new LinkedHashSet<MediaTypeFileExtensionResolver>();
new LinkedHashSet<MediaTypeFileExtensionResolver>();
/** /**

38
spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -40,34 +40,34 @@ import org.springframework.web.context.ServletContextAware;
* *
* <table> * <table>
* <tr> * <tr>
* <th>Property Setter</th> * <th>Property Setter</th>
* <th>Underlying Strategy</th> * <th>Underlying Strategy</th>
* <th>Default Setting</th> * <th>Default Setting</th>
* </tr> * </tr>
* <tr> * <tr>
* <td>{@link #setFavorPathExtension}</td> * <td>{@link #setFavorPathExtension}</td>
* <td>{@link PathExtensionContentNegotiationStrategy Path Extension strategy}</td> * <td>{@link PathExtensionContentNegotiationStrategy Path Extension strategy}</td>
* <td>On</td> * <td>On</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>{@link #setFavorParameter favorParameter}</td> * <td>{@link #setFavorParameter favorParameter}</td>
* <td>{@link ParameterContentNegotiationStrategy Parameter strategy}</td> * <td>{@link ParameterContentNegotiationStrategy Parameter strategy}</td>
* <td>Off</td> * <td>Off</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>{@link #setIgnoreAcceptHeader ignoreAcceptHeader}</td> * <td>{@link #setIgnoreAcceptHeader ignoreAcceptHeader}</td>
* <td>{@link HeaderContentNegotiationStrategy Header strategy}</td> * <td>{@link HeaderContentNegotiationStrategy Header strategy}</td>
* <td>On</td> * <td>On</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>{@link #setDefaultContentType defaultContentType}</td> * <td>{@link #setDefaultContentType defaultContentType}</td>
* <td>{@link FixedContentNegotiationStrategy Fixed content strategy}</td> * <td>{@link FixedContentNegotiationStrategy Fixed content strategy}</td>
* <td>Not set</td> * <td>Not set</td>
* </tr> * </tr>
* <tr> * <tr>
* <td>{@link #setDefaultContentTypeStrategy defaultContentTypeStrategy}</td> * <td>{@link #setDefaultContentTypeStrategy defaultContentTypeStrategy}</td>
* <td>{@link ContentNegotiationStrategy}</td> * <td>{@link ContentNegotiationStrategy}</td>
* <td>Not set</td> * <td>Not set</td>
* </tr> * </tr>
* </table> * </table>
* *

6
spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationStrategy.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -33,10 +33,8 @@ public interface ContentNegotiationStrategy {
/** /**
* Resolve the given request to a list of media types. The returned list is * Resolve the given request to a list of media types. The returned list is
* ordered by specificity first and by quality parameter second. * ordered by specificity first and by quality parameter second.
*
* @param webRequest the current request * @param webRequest the current request
* @return the requested media types or an empty list, never {@code null} * @return the requested media types or an empty list (never {@code null})
*
* @throws HttpMediaTypeNotAcceptableException if the requested media * @throws HttpMediaTypeNotAcceptableException if the requested media
* types cannot be parsed * types cannot be parsed
*/ */

5
spring-web/src/main/java/org/springframework/web/accept/FixedContentNegotiationStrategy.java

@ -33,8 +33,7 @@ import org.springframework.web.context.request.NativeWebRequest;
*/ */
public class FixedContentNegotiationStrategy implements ContentNegotiationStrategy { public class FixedContentNegotiationStrategy implements ContentNegotiationStrategy {
private static final Log logger = LogFactory.getLog( private static final Log logger = LogFactory.getLog(FixedContentNegotiationStrategy.class);
FixedContentNegotiationStrategy.class);
private final List<MediaType> contentType; private final List<MediaType> contentType;
@ -50,7 +49,7 @@ public class FixedContentNegotiationStrategy implements ContentNegotiationStrate
@Override @Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request) { public List<MediaType> resolveMediaTypes(NativeWebRequest request) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Requested media types is " + this.contentType + "."); logger.debug("Requested media types: " + this.contentType);
} }
return this.contentType; return this.contentType;
} }

6
spring-web/src/main/java/org/springframework/web/accept/HeaderContentNegotiationStrategy.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -34,11 +34,9 @@ import org.springframework.web.context.request.NativeWebRequest;
*/ */
public class HeaderContentNegotiationStrategy implements ContentNegotiationStrategy { public class HeaderContentNegotiationStrategy implements ContentNegotiationStrategy {
/** /**
* {@inheritDoc} * {@inheritDoc}
* @throws HttpMediaTypeNotAcceptableException if the 'Accept' header * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
* cannot be parsed.
*/ */
@Override @Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request) public List<MediaType> resolveMediaTypes(NativeWebRequest request)

4
spring-web/src/main/java/org/springframework/web/accept/MappingMediaTypeFileExtensionResolver.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -35,7 +35,7 @@ import org.springframework.util.MultiValueMap;
* lookups between file extensions and MediaTypes in both directions. * lookups between file extensions and MediaTypes in both directions.
* *
* <p>Initially created with a map of file extensions and media types. * <p>Initially created with a map of file extensions and media types.
* Subsequently sub-classes can use {@link #addMapping} to add more mappings. * Subsequently subclasses can use {@link #addMapping} to add more mappings.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2

7
spring-web/src/main/java/org/springframework/web/accept/MediaTypeFileExtensionResolver.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -31,15 +31,14 @@ public interface MediaTypeFileExtensionResolver {
/** /**
* Resolve the given media type to a list of path extensions. * Resolve the given media type to a list of path extensions.
*
* @param mediaType the media type to resolve * @param mediaType the media type to resolve
* @return a list of extensions or an empty list, never {@code null} * @return a list of extensions or an empty list (never {@code null})
*/ */
List<String> resolveFileExtensions(MediaType mediaType); List<String> resolveFileExtensions(MediaType mediaType);
/** /**
* Return all registered file extensions. * Return all registered file extensions.
* @return a list of extensions or an empty list, never {@code null} * @return a list of extensions or an empty list (never {@code null})
*/ */
List<String> getAllFileExtensions(); List<String> getAllFileExtensions();

22
spring-web/src/main/java/org/springframework/web/accept/ParameterContentNegotiationStrategy.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 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.
@ -27,18 +27,15 @@ import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
/** /**
* A {@code ContentNegotiationStrategy} that resolves a query parameter to a * A {@code ContentNegotiationStrategy} that resolves a query parameter to a key
* key to be used to look up a media type. The default parameter name is * to be used to look up a media type. The default parameter name is {@code format}.
* {@code format}. *
*s
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public class ParameterContentNegotiationStrategy public class ParameterContentNegotiationStrategy extends AbstractMappingContentNegotiationStrategy {
extends AbstractMappingContentNegotiationStrategy {
private static final Log logger = LogFactory.getLog( private static final Log logger = LogFactory.getLog(ParameterContentNegotiationStrategy.class);
ParameterContentNegotiationStrategy.class);
private String parameterName = "format"; private String parameterName = "format";
@ -56,7 +53,7 @@ public class ParameterContentNegotiationStrategy
* <p>By default this is set to {@code "format"}. * <p>By default this is set to {@code "format"}.
*/ */
public void setParameterName(String parameterName) { public void setParameterName(String parameterName) {
Assert.notNull(parameterName, "parameterName is required"); Assert.notNull(parameterName, "'parameterName' is required");
this.parameterName = parameterName; this.parameterName = parameterName;
} }
@ -64,6 +61,7 @@ public class ParameterContentNegotiationStrategy
return this.parameterName; return this.parameterName;
} }
@Override @Override
protected String getMediaTypeKey(NativeWebRequest request) { protected String getMediaTypeKey(NativeWebRequest request) {
return request.getParameter(getParameterName()); return request.getParameter(getParameterName());
@ -72,8 +70,8 @@ public class ParameterContentNegotiationStrategy
@Override @Override
protected void handleMatch(String mediaTypeKey, MediaType mediaType) { protected void handleMatch(String mediaTypeKey, MediaType mediaType) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Requested media type is '" + mediaType + logger.debug("Requested media type: '" + mediaType + "' based on '" +
"' based on '" + getParameterName() + "'='" + mediaTypeKey + "'."); getParameterName() + "'='" + mediaTypeKey + "'");
} }
} }

3
spring-web/src/main/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategy.java

@ -57,8 +57,7 @@ public class PathExtensionContentNegotiationStrategy
private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class); private static final Log logger = LogFactory.getLog(PathExtensionContentNegotiationStrategy.class);
private static final boolean JAF_PRESENT = ClassUtils.isPresent( private static final boolean JAF_PRESENT = ClassUtils.isPresent("javax.activation.FileTypeMap",
"javax.activation.FileTypeMap",
PathExtensionContentNegotiationStrategy.class.getClassLoader()); PathExtensionContentNegotiationStrategy.class.getClassLoader());
private static final UrlPathHelper PATH_HELPER = new UrlPathHelper(); private static final UrlPathHelper PATH_HELPER = new UrlPathHelper();

12
spring-web/src/main/java/org/springframework/web/accept/ServletPathExtensionContentNegotiationStrategy.java

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.accept; package org.springframework.web.accept;
import java.util.Map; import java.util.Map;
@ -32,8 +33,7 @@ import org.springframework.web.context.request.NativeWebRequest;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.2 * @since 3.2
*/ */
public class ServletPathExtensionContentNegotiationStrategy public class ServletPathExtensionContentNegotiationStrategy extends PathExtensionContentNegotiationStrategy {
extends PathExtensionContentNegotiationStrategy {
private final ServletContext servletContext; private final ServletContext servletContext;
@ -41,12 +41,12 @@ public class ServletPathExtensionContentNegotiationStrategy
/** /**
* Create an instance with the given extension-to-MediaType lookup. * Create an instance with the given extension-to-MediaType lookup.
*/ */
public ServletPathExtensionContentNegotiationStrategy(ServletContext context, public ServletPathExtensionContentNegotiationStrategy(
Map<String, MediaType> mediaTypes) { ServletContext servletContext, Map<String, MediaType> mediaTypes) {
super(mediaTypes); super(mediaTypes);
Assert.notNull(context, "ServletContext is required!"); Assert.notNull(servletContext, "ServletContext is required");
this.servletContext = context; this.servletContext = servletContext;
} }
/** /**

4
spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 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.
@ -288,7 +288,7 @@ public abstract class WebApplicationContextUtils {
public static void initServletPropertySources( public static void initServletPropertySources(
MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) { MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
Assert.notNull(propertySources, "propertySources must not be null"); Assert.notNull(propertySources, "'propertySources' must not be null");
if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) && if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) { propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,

4
spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 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.
@ -211,7 +211,7 @@ public abstract class PortletApplicationContextUtils {
public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext, public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext,
PortletContext portletContext, PortletConfig portletConfig) { PortletContext portletContext, PortletConfig portletConfig) {
Assert.notNull(propertySources, "propertySources must not be null"); Assert.notNull(propertySources, "'propertySources' must not be null");
WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext); WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext);
if (portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) { if (portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) {

Loading…
Cancel
Save