From ec7d80b8519332ffb5f0595dddc9ef52de532730 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 23 Apr 2014 23:03:47 +0200 Subject: [PATCH] Polishing --- .../web/filter/OncePerRequestFilter.java | 14 ++--- ...stractMessageConverterMethodProcessor.java | 53 +++++++++---------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/filter/OncePerRequestFilter.java b/spring-web/src/main/java/org/springframework/web/filter/OncePerRequestFilter.java index 5a0c4ddba1..e159a328db 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/OncePerRequestFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/OncePerRequestFilter.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"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ package org.springframework.web.filter; import java.io.IOException; - import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; @@ -35,8 +34,8 @@ import org.springframework.web.util.WebUtils; * method with HttpServletRequest and HttpServletResponse arguments. * *

As of Servlet 3.0, a filter may be invoked as part of a - * {@link javax.servlet.DispatcherType.REQUEST REQUEST} or - * {@link javax.servlet.DispatcherType.ASYNC ASYNC} dispatches that occur in + * {@link javax.servlet.DispatcherType#REQUEST REQUEST} or + * {@link javax.servlet.DispatcherType#ASYNC ASYNC} dispatches that occur in * separate threads. A filter can be configured in {@code web.xml} whether it * should be involved in async dispatches. However, in some cases servlet * containers assume different default configuration. Therefore sub-classes can @@ -53,7 +52,7 @@ import org.springframework.web.util.WebUtils; * won't be the last one. * *

Yet another dispatch type that also occurs in its own thread is - * {@link javax.servlet.DispatcherType.ERROR ERROR}. Sub-classes can override + * {@link javax.servlet.DispatcherType#ERROR ERROR}. Sub-classes can override * {@link #shouldNotFilterErrorDispatch()} if they wish to declare statically * if they should be invoked once during error dispatches. * @@ -129,7 +128,6 @@ public abstract class OncePerRequestFilter extends GenericFilterBean { * in Servlet 3.0 means a filter can be invoked in more than one thread over * the course of a single request. This method returns {@code true} if the * filter is currently executing within an asynchronous dispatch. - * * @param request the current request * @see WebAsyncManager#hasConcurrentResult() */ @@ -140,7 +138,6 @@ public abstract class OncePerRequestFilter extends GenericFilterBean { /** * Whether request processing is in asynchronous mode meaning that the * response will not be committed after the current thread is exited. - * * @param request the current request * @see WebAsyncManager#isConcurrentHandlingStarted() */ @@ -151,7 +148,7 @@ public abstract class OncePerRequestFilter extends GenericFilterBean { /** * Return the name of the request attribute that identifies that a request * is already filtered. - *

Default implementation takes the configured name of the concrete filter + *

The default implementation takes the configured name of the concrete filter * instance and appends ".FILTERED". If the filter is not fully initialized, * it falls back to its class name. * @see #getFilterName @@ -188,7 +185,6 @@ public abstract class OncePerRequestFilter extends GenericFilterBean { * types via {@code web.xml} or in Java through the {@code ServletContext}, * servlet containers may enforce different defaults with regards to * dispatcher types. This flag enforces the design intent of the filter. - * *

The default return value is "true", which means the filter will not be * invoked during subsequent async dispatches. If "false", the filter will * be invoked during async dispatches with the same guarantees of being diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java index 48cff20fe5..83c564bc23 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.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"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; - import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -41,8 +40,8 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerMapping; /** - * Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle method return - * values by writing to the response with {@link HttpMessageConverter}s. + * Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle + * method return values by writing to the response with {@link HttpMessageConverter}s. * * @author Arjen Poutsma * @author Rossen Stoyanchev @@ -55,6 +54,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe private final ContentNegotiationManager contentNegotiationManager; + protected AbstractMessageConverterMethodProcessor(List> messageConverters) { this(messageConverters, null); } @@ -63,12 +63,12 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe ContentNegotiationManager manager) { super(messageConverters); - this.contentNegotiationManager = (manager != null) ? manager : new ContentNegotiationManager(); + this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager()); } + /** * Creates a new {@link HttpOutputMessage} from the given {@link NativeWebRequest}. - * * @param webRequest the web request to create an output message from * @return the output message */ @@ -81,10 +81,9 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe * Writes the given return value to the given web request. Delegates to * {@link #writeWithMessageConverters(Object, MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)} */ - protected void writeWithMessageConverters(T returnValue, - MethodParameter returnType, - NativeWebRequest webRequest) + protected void writeWithMessageConverters(T returnValue, MethodParameter returnType, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException { + ServletServerHttpRequest inputMessage = createInputMessage(webRequest); ServletServerHttpResponse outputMessage = createOutputMessage(webRequest); writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage); @@ -92,7 +91,6 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe /** * Writes the given return type to the given output message. - * * @param returnValue the value to write to the output message * @param returnType the type of the value * @param inputMessage the input messages. Used to inspect the {@code Accept} header. @@ -102,23 +100,20 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe * the request cannot be met by the message converters */ @SuppressWarnings("unchecked") - protected void writeWithMessageConverters(T returnValue, - MethodParameter returnType, - ServletServerHttpRequest inputMessage, - ServletServerHttpResponse outputMessage) + protected void writeWithMessageConverters(T returnValue, MethodParameter returnType, + ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) throws IOException, HttpMediaTypeNotAcceptableException { Class returnValueClass = returnValue.getClass(); - HttpServletRequest servletRequest = inputMessage.getServletRequest(); List requestedMediaTypes = getAcceptableMediaTypes(servletRequest); List producibleMediaTypes = getProducibleMediaTypes(servletRequest, returnValueClass); Set compatibleMediaTypes = new LinkedHashSet(); - for (MediaType r : requestedMediaTypes) { - for (MediaType p : producibleMediaTypes) { - if (r.isCompatibleWith(p)) { - compatibleMediaTypes.add(getMostSpecificMediaType(r, p)); + for (MediaType requestedType : requestedMediaTypes) { + for (MediaType producibleType : producibleMediaTypes) { + if (requestedType.isCompatibleWith(producibleType)) { + compatibleMediaTypes.add(getMostSpecificMediaType(requestedType, producibleType)); } } } @@ -143,7 +138,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe if (selectedMediaType != null) { selectedMediaType = selectedMediaType.removeQualityValue(); - for (HttpMessageConverter messageConverter : messageConverters) { + for (HttpMessageConverter messageConverter : this.messageConverters) { if (messageConverter.canWrite(returnValueClass, selectedMediaType)) { ((HttpMessageConverter) messageConverter).write(returnValue, selectedMediaType, outputMessage); if (logger.isDebugEnabled()) { @@ -154,15 +149,15 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe } } } - throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes); + throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes); } /** * Returns the media types that can be produced: *

*/ @SuppressWarnings("unchecked") @@ -171,9 +166,9 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe if (!CollectionUtils.isEmpty(mediaTypes)) { return new ArrayList(mediaTypes); } - else if (!allSupportedMediaTypes.isEmpty()) { + else if (!this.allSupportedMediaTypes.isEmpty()) { List result = new ArrayList(); - for (HttpMessageConverter converter : messageConverters) { + for (HttpMessageConverter converter : this.messageConverters) { if (converter.canWrite(returnValueClass, null)) { result.addAll(converter.getSupportedMediaTypes()); } @@ -187,7 +182,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe private List getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException { List mediaTypes = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request)); - return mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes; + return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes); } /** @@ -195,8 +190,8 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe * with the q-value of the former. */ private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) { - produceType = produceType.copyQualityValue(acceptType); - return MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) <= 0 ? acceptType : produceType; + MediaType produceTypeToUse = produceType.copyQualityValue(acceptType); + return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceTypeToUse) <= 0 ? acceptType : produceTypeToUse); } }