11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2017 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.
3030import org .springframework .http .HttpMethod ;
3131import org .springframework .web .bind .support .WebDataBinderFactory ;
3232import org .springframework .web .context .request .NativeWebRequest ;
33- import org .springframework .web .context .request .ServletWebRequest ;
3433import org .springframework .web .context .request .WebRequest ;
3534import org .springframework .web .method .support .HandlerMethodArgumentResolver ;
3635import org .springframework .web .method .support .ModelAndViewContainer ;
4544 * <li>{@link MultipartRequest}
4645 * <li>{@link HttpSession}
4746 * <li>{@link Principal}
47+ * <li>{@link InputStream}
48+ * <li>{@link Reader}
49+ * <li>{@link HttpMethod} (as of Spring 4.0)</li>
4850 * <li>{@link Locale}
4951 * <li>{@link TimeZone} (as of Spring 4.0)
5052 * <li>{@link java.time.ZoneId} (as of Spring 4.0 and Java 8)</li>
51- * <li>{@link InputStream}
52- * <li>{@link Reader}
53- * <li>{@link org.springframework.http.HttpMethod} (as of Spring 4.0)</li>
5453 * </ul>
5554 *
5655 * @author Arjen Poutsma
@@ -68,12 +67,12 @@ public boolean supportsParameter(MethodParameter parameter) {
6867 MultipartRequest .class .isAssignableFrom (paramType ) ||
6968 HttpSession .class .isAssignableFrom (paramType ) ||
7069 Principal .class .isAssignableFrom (paramType ) ||
71- Locale .class == paramType ||
72- TimeZone .class == paramType ||
73- ZoneId .class == paramType ||
7470 InputStream .class .isAssignableFrom (paramType ) ||
7571 Reader .class .isAssignableFrom (paramType ) ||
76- HttpMethod .class == paramType );
72+ HttpMethod .class == paramType ||
73+ Locale .class == paramType ||
74+ TimeZone .class == paramType ||
75+ ZoneId .class == paramType );
7776 }
7877
7978 @ Override
@@ -82,6 +81,10 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
8281
8382 Class <?> paramType = parameter .getParameterType ();
8483 if (WebRequest .class .isAssignableFrom (paramType )) {
84+ if (!paramType .isInstance (webRequest )) {
85+ throw new IllegalStateException (
86+ "Current request is not of type [" + paramType .getName () + "]: " + webRequest );
87+ }
8588 return webRequest ;
8689 }
8790
@@ -95,13 +98,39 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
9598 return nativeRequest ;
9699 }
97100 else if (HttpSession .class .isAssignableFrom (paramType )) {
98- return request .getSession ();
101+ HttpSession session = request .getSession ();
102+ if (!paramType .isInstance (session )) {
103+ throw new IllegalStateException (
104+ "Current session is not of type [" + paramType .getName () + "]: " + session );
105+ }
106+ return session ;
99107 }
100- else if (HttpMethod .class == paramType ) {
101- return ((ServletWebRequest ) webRequest ).getHttpMethod ();
108+ else if (InputStream .class .isAssignableFrom (paramType )) {
109+ InputStream inputStream = request .getInputStream ();
110+ if (!paramType .isInstance (inputStream )) {
111+ throw new IllegalStateException (
112+ "Request input stream is not of type [" + paramType .getName () + "]: " + inputStream );
113+ }
114+ return inputStream ;
115+ }
116+ else if (Reader .class .isAssignableFrom (paramType )) {
117+ Reader reader = request .getReader ();
118+ if (!paramType .isInstance (reader )) {
119+ throw new IllegalStateException (
120+ "Request body reader is not of type [" + paramType .getName () + "]: " + reader );
121+ }
122+ return reader ;
102123 }
103124 else if (Principal .class .isAssignableFrom (paramType )) {
104- return request .getUserPrincipal ();
125+ Principal userPrincipal = request .getUserPrincipal ();
126+ if (!paramType .isInstance (userPrincipal )) {
127+ throw new IllegalStateException (
128+ "Current user principal is not of type [" + paramType .getName () + "]: " + userPrincipal );
129+ }
130+ return userPrincipal ;
131+ }
132+ else if (HttpMethod .class == paramType ) {
133+ return HttpMethod .resolve (request .getMethod ());
105134 }
106135 else if (Locale .class == paramType ) {
107136 return RequestContextUtils .getLocale (request );
@@ -114,16 +143,10 @@ else if (ZoneId.class == paramType) {
114143 TimeZone timeZone = RequestContextUtils .getTimeZone (request );
115144 return (timeZone != null ? timeZone .toZoneId () : ZoneId .systemDefault ());
116145 }
117- else if (InputStream .class .isAssignableFrom (paramType )) {
118- return request .getInputStream ();
119- }
120- else if (Reader .class .isAssignableFrom (paramType )) {
121- return request .getReader ();
122- }
123146 else {
124- // should never happen...
147+ // Should never happen...
125148 throw new UnsupportedOperationException (
126- "Unknown parameter type: " + paramType + " in method: " + parameter .getMethod ());
149+ "Unknown parameter type [ " + paramType . getName () + "] in " + parameter .getMethod ());
127150 }
128151 }
129152
0 commit comments