11/*
2- * Copyright 2002-2012 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.
2222import java .util .LinkedHashSet ;
2323import java .util .List ;
2424import java .util .Set ;
25-
2625import javax .servlet .http .HttpServletRequest ;
2726import javax .servlet .http .HttpServletResponse ;
2827
4140import org .springframework .web .servlet .HandlerMapping ;
4241
4342/**
44- * Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle method return
45- * values by writing to the response with {@link HttpMessageConverter}s.
43+ * Extends {@link AbstractMessageConverterMethodArgumentResolver} with the ability to handle
44+ * method return values by writing to the response with {@link HttpMessageConverter}s.
4645 *
4746 * @author Arjen Poutsma
4847 * @author Rossen Stoyanchev
@@ -55,6 +54,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
5554
5655 private final ContentNegotiationManager contentNegotiationManager ;
5756
57+
5858 protected AbstractMessageConverterMethodProcessor (List <HttpMessageConverter <?>> messageConverters ) {
5959 this (messageConverters , null );
6060 }
@@ -63,12 +63,12 @@ protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>>
6363 ContentNegotiationManager manager ) {
6464
6565 super (messageConverters );
66- this .contentNegotiationManager = (manager != null ) ? manager : new ContentNegotiationManager ();
66+ this .contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager () );
6767 }
6868
69+
6970 /**
7071 * Creates a new {@link HttpOutputMessage} from the given {@link NativeWebRequest}.
71- *
7272 * @param webRequest the web request to create an output message from
7373 * @return the output message
7474 */
@@ -81,18 +81,16 @@ protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequ
8181 * Writes the given return value to the given web request. Delegates to
8282 * {@link #writeWithMessageConverters(Object, MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)}
8383 */
84- protected <T > void writeWithMessageConverters (T returnValue ,
85- MethodParameter returnType ,
86- NativeWebRequest webRequest )
84+ protected <T > void writeWithMessageConverters (T returnValue , MethodParameter returnType , NativeWebRequest webRequest )
8785 throws IOException , HttpMediaTypeNotAcceptableException {
86+
8887 ServletServerHttpRequest inputMessage = createInputMessage (webRequest );
8988 ServletServerHttpResponse outputMessage = createOutputMessage (webRequest );
9089 writeWithMessageConverters (returnValue , returnType , inputMessage , outputMessage );
9190 }
9291
9392 /**
9493 * Writes the given return type to the given output message.
95- *
9694 * @param returnValue the value to write to the output message
9795 * @param returnType the type of the value
9896 * @param inputMessage the input messages. Used to inspect the {@code Accept} header.
@@ -102,23 +100,20 @@ protected <T> void writeWithMessageConverters(T returnValue,
102100 * the request cannot be met by the message converters
103101 */
104102 @ SuppressWarnings ("unchecked" )
105- protected <T > void writeWithMessageConverters (T returnValue ,
106- MethodParameter returnType ,
107- ServletServerHttpRequest inputMessage ,
108- ServletServerHttpResponse outputMessage )
103+ protected <T > void writeWithMessageConverters (T returnValue , MethodParameter returnType ,
104+ ServletServerHttpRequest inputMessage , ServletServerHttpResponse outputMessage )
109105 throws IOException , HttpMediaTypeNotAcceptableException {
110106
111107 Class <?> returnValueClass = returnValue .getClass ();
112-
113108 HttpServletRequest servletRequest = inputMessage .getServletRequest ();
114109 List <MediaType > requestedMediaTypes = getAcceptableMediaTypes (servletRequest );
115110 List <MediaType > producibleMediaTypes = getProducibleMediaTypes (servletRequest , returnValueClass );
116111
117112 Set <MediaType > compatibleMediaTypes = new LinkedHashSet <MediaType >();
118- for (MediaType r : requestedMediaTypes ) {
119- for (MediaType p : producibleMediaTypes ) {
120- if (r .isCompatibleWith (p )) {
121- compatibleMediaTypes .add (getMostSpecificMediaType (r , p ));
113+ for (MediaType requestedType : requestedMediaTypes ) {
114+ for (MediaType producibleType : producibleMediaTypes ) {
115+ if (requestedType .isCompatibleWith (producibleType )) {
116+ compatibleMediaTypes .add (getMostSpecificMediaType (requestedType , producibleType ));
122117 }
123118 }
124119 }
@@ -143,7 +138,7 @@ else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICAT
143138
144139 if (selectedMediaType != null ) {
145140 selectedMediaType = selectedMediaType .removeQualityValue ();
146- for (HttpMessageConverter <?> messageConverter : messageConverters ) {
141+ for (HttpMessageConverter <?> messageConverter : this . messageConverters ) {
147142 if (messageConverter .canWrite (returnValueClass , selectedMediaType )) {
148143 ((HttpMessageConverter <T >) messageConverter ).write (returnValue , selectedMediaType , outputMessage );
149144 if (logger .isDebugEnabled ()) {
@@ -154,15 +149,15 @@ else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICAT
154149 }
155150 }
156151 }
157- throw new HttpMediaTypeNotAcceptableException (allSupportedMediaTypes );
152+ throw new HttpMediaTypeNotAcceptableException (this . allSupportedMediaTypes );
158153 }
159154
160155 /**
161156 * Returns the media types that can be produced:
162157 * <ul>
163- * <li>The producible media types specified in the request mappings, or
164- * <li>Media types of configured converters that can write the specific return value, or
165- * <li>{@link MediaType#ALL}
158+ * <li>The producible media types specified in the request mappings, or
159+ * <li>Media types of configured converters that can write the specific return value, or
160+ * <li>{@link MediaType#ALL}
166161 * </ul>
167162 */
168163 @ SuppressWarnings ("unchecked" )
@@ -171,9 +166,9 @@ protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Cl
171166 if (!CollectionUtils .isEmpty (mediaTypes )) {
172167 return new ArrayList <MediaType >(mediaTypes );
173168 }
174- else if (!allSupportedMediaTypes .isEmpty ()) {
169+ else if (!this . allSupportedMediaTypes .isEmpty ()) {
175170 List <MediaType > result = new ArrayList <MediaType >();
176- for (HttpMessageConverter <?> converter : messageConverters ) {
171+ for (HttpMessageConverter <?> converter : this . messageConverters ) {
177172 if (converter .canWrite (returnValueClass , null )) {
178173 result .addAll (converter .getSupportedMediaTypes ());
179174 }
@@ -187,16 +182,16 @@ else if (!allSupportedMediaTypes.isEmpty()) {
187182
188183 private List <MediaType > getAcceptableMediaTypes (HttpServletRequest request ) throws HttpMediaTypeNotAcceptableException {
189184 List <MediaType > mediaTypes = this .contentNegotiationManager .resolveMediaTypes (new ServletWebRequest (request ));
190- return mediaTypes .isEmpty () ? Collections .singletonList (MediaType .ALL ) : mediaTypes ;
185+ return ( mediaTypes .isEmpty () ? Collections .singletonList (MediaType .ALL ) : mediaTypes ) ;
191186 }
192187
193188 /**
194189 * Return the more specific of the acceptable and the producible media types
195190 * with the q-value of the former.
196191 */
197192 private MediaType getMostSpecificMediaType (MediaType acceptType , MediaType produceType ) {
198- produceType = produceType .copyQualityValue (acceptType );
199- return MediaType .SPECIFICITY_COMPARATOR .compare (acceptType , produceType ) <= 0 ? acceptType : produceType ;
193+ MediaType produceTypeToUse = produceType .copyQualityValue (acceptType );
194+ return ( MediaType .SPECIFICITY_COMPARATOR .compare (acceptType , produceTypeToUse ) <= 0 ? acceptType : produceTypeToUse ) ;
200195 }
201196
202197}
0 commit comments