Skip to content

Commit b3f039a

Browse files
committed
Servlet/PortletRequestDataBinder perform unwrapping for MultipartRequest as well (SPR-7795)
1 parent 4835be7 commit b3f039a

File tree

6 files changed

+108
-76
lines changed

6 files changed

+108
-76
lines changed

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/bind/PortletRequestDataBinder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -22,6 +22,7 @@
2222
import org.springframework.validation.BindException;
2323
import org.springframework.web.bind.WebDataBinder;
2424
import org.springframework.web.multipart.MultipartRequest;
25+
import org.springframework.web.portlet.util.PortletUtils;
2526

2627
/**
2728
* Special {@link org.springframework.validation.DataBinder} to perform data binding
@@ -105,8 +106,8 @@ public PortletRequestDataBinder(Object target, String objectName) {
105106
*/
106107
public void bind(PortletRequest request) {
107108
MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request);
108-
if (request instanceof MultipartRequest) {
109-
MultipartRequest multipartRequest = (MultipartRequest) request;
109+
MultipartRequest multipartRequest = PortletUtils.getNativeRequest(request, MultipartRequest.class);
110+
if (multipartRequest != null) {
110111
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
111112
}
112113
doBind(mpvs);

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletWebRequest.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -23,13 +23,12 @@
2323
import javax.portlet.PortletRequest;
2424
import javax.portlet.PortletResponse;
2525
import javax.portlet.PortletSession;
26-
import javax.portlet.filter.PortletRequestWrapper;
27-
import javax.portlet.filter.PortletResponseWrapper;
2826

2927
import org.springframework.util.CollectionUtils;
3028
import org.springframework.util.ObjectUtils;
3129
import org.springframework.util.StringUtils;
3230
import org.springframework.web.context.request.NativeWebRequest;
31+
import org.springframework.web.portlet.util.PortletUtils;
3332

3433
/**
3534
* {@link org.springframework.web.context.request.WebRequest} adapter
@@ -79,40 +78,12 @@ public Object getNativeResponse() {
7978

8079
@SuppressWarnings("unchecked")
8180
public <T> T getNativeRequest(Class<T> requiredType) {
82-
if (requiredType != null) {
83-
PortletRequest request = getRequest();
84-
while (request != null) {
85-
if (requiredType.isInstance(request)) {
86-
return (T) request;
87-
}
88-
else if (request instanceof PortletRequestWrapper) {
89-
request = ((PortletRequestWrapper) request).getRequest();
90-
}
91-
else {
92-
request = null;
93-
}
94-
}
95-
}
96-
return null;
81+
return PortletUtils.getNativeRequest(getRequest(), requiredType);
9782
}
9883

9984
@SuppressWarnings("unchecked")
10085
public <T> T getNativeResponse(Class<T> requiredType) {
101-
if (requiredType != null) {
102-
PortletResponse response = getResponse();
103-
while (response != null) {
104-
if (requiredType.isInstance(response)) {
105-
return (T) response;
106-
}
107-
else if (response instanceof PortletResponseWrapper) {
108-
response = ((PortletResponseWrapper) response).getResponse();
109-
}
110-
else {
111-
response = null;
112-
}
113-
}
114-
}
115-
return null;
86+
return PortletUtils.getNativeResponse(getResponse(), requiredType);
11687
}
11788

11889

org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/util/PortletUtils.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -32,6 +32,9 @@
3232
import javax.portlet.PortletSession;
3333
import javax.portlet.ResourceRequest;
3434
import javax.portlet.ResourceResponse;
35+
import javax.portlet.PortletResponse;
36+
import javax.portlet.filter.PortletRequestWrapper;
37+
import javax.portlet.filter.PortletResponseWrapper;
3538
import javax.servlet.http.Cookie;
3639

3740
import org.springframework.util.Assert;
@@ -276,6 +279,48 @@ public static Object getSessionMutex(PortletSession session) {
276279
}
277280

278281

282+
/**
283+
* Return an appropriate request object of the specified type, if available,
284+
* unwrapping the given request as far as necessary.
285+
* @param request the portlet request to introspect
286+
* @param requiredType the desired type of request object
287+
* @return the matching request object, or <code>null</code> if none
288+
* of that type is available
289+
*/
290+
@SuppressWarnings("unchecked")
291+
public static <T> T getNativeRequest(PortletRequest request, Class<T> requiredType) {
292+
if (requiredType != null) {
293+
if (requiredType.isInstance(request)) {
294+
return (T) request;
295+
}
296+
else if (request instanceof PortletRequestWrapper) {
297+
return getNativeRequest(((PortletRequestWrapper) request).getRequest(), requiredType);
298+
}
299+
}
300+
return null;
301+
}
302+
303+
/**
304+
* Return an appropriate response object of the specified type, if available,
305+
* unwrapping the given response as far as necessary.
306+
* @param response the portlet response to introspect
307+
* @param requiredType the desired type of response object
308+
* @return the matching response object, or <code>null</code> if none
309+
* of that type is available
310+
*/
311+
@SuppressWarnings("unchecked")
312+
public static <T> T getNativeResponse(PortletResponse response, Class<T> requiredType) {
313+
if (requiredType != null) {
314+
if (requiredType.isInstance(response)) {
315+
return (T) response;
316+
}
317+
else if (response instanceof PortletResponseWrapper) {
318+
return getNativeResponse(((PortletResponseWrapper) response).getResponse(), requiredType);
319+
}
320+
}
321+
return null;
322+
}
323+
279324
/**
280325
* Expose the given Map as request attributes, using the keys as attribute names
281326
* and the values as corresponding attribute values. Keys must be Strings.
@@ -293,7 +338,7 @@ public static void exposeRequestAttributes(PortletRequest request, Map<String, ?
293338
/**
294339
* Retrieve the first cookie with the given name. Note that multiple
295340
* cookies can have the same name but different paths or domains.
296-
* @param request current servlet request
341+
* @param request current portlet request
297342
* @param name cookie name
298343
* @return the first cookie with the given name, or <code>null</code> if none is found
299344
*/

org.springframework.web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -21,6 +21,7 @@
2121
import org.springframework.beans.MutablePropertyValues;
2222
import org.springframework.validation.BindException;
2323
import org.springframework.web.multipart.MultipartRequest;
24+
import org.springframework.web.util.WebUtils;
2425

2526
/**
2627
* Special {@link org.springframework.validation.DataBinder} to perform data binding
@@ -103,8 +104,8 @@ public ServletRequestDataBinder(Object target, String objectName) {
103104
*/
104105
public void bind(ServletRequest request) {
105106
MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
106-
if (request instanceof MultipartRequest) {
107-
MultipartRequest multipartRequest = (MultipartRequest) request;
107+
MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
108+
if (multipartRequest != null) {
108109
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
109110
}
110111
doBind(mpvs);

org.springframework.web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
import java.util.Iterator;
2121
import java.util.Locale;
2222
import java.util.Map;
23-
import javax.servlet.ServletRequest;
24-
import javax.servlet.ServletRequestWrapper;
25-
import javax.servlet.ServletResponse;
26-
import javax.servlet.ServletResponseWrapper;
2723
import javax.servlet.http.HttpServletRequest;
2824
import javax.servlet.http.HttpServletResponse;
2925
import javax.servlet.http.HttpSession;
3026

3127
import org.springframework.util.CollectionUtils;
3228
import org.springframework.util.ObjectUtils;
3329
import org.springframework.util.StringUtils;
30+
import org.springframework.web.util.WebUtils;
3431

3532
/**
3633
* {@link WebRequest} adapter for an {@link javax.servlet.http.HttpServletRequest}.
@@ -92,40 +89,12 @@ public Object getNativeResponse() {
9289

9390
@SuppressWarnings("unchecked")
9491
public <T> T getNativeRequest(Class<T> requiredType) {
95-
if (requiredType != null) {
96-
ServletRequest request = getRequest();
97-
while (request != null) {
98-
if (requiredType.isInstance(request)) {
99-
return (T) request;
100-
}
101-
else if (request instanceof ServletRequestWrapper) {
102-
request = ((ServletRequestWrapper) request).getRequest();
103-
}
104-
else {
105-
request = null;
106-
}
107-
}
108-
}
109-
return null;
92+
return WebUtils.getNativeRequest(getRequest(), requiredType);
11093
}
11194

11295
@SuppressWarnings("unchecked")
11396
public <T> T getNativeResponse(Class<T> requiredType) {
114-
if (requiredType != null) {
115-
ServletResponse response = getResponse();
116-
while (response != null) {
117-
if (requiredType.isInstance(response)) {
118-
return (T) response;
119-
}
120-
else if (response instanceof ServletResponseWrapper) {
121-
response = ((ServletResponseWrapper) response).getResponse();
122-
}
123-
else {
124-
response = null;
125-
}
126-
}
127-
}
128-
return null;
97+
return WebUtils.getNativeResponse(getResponse(), requiredType);
12998
}
13099

131100

org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -23,6 +23,9 @@
2323
import java.util.TreeMap;
2424
import javax.servlet.ServletContext;
2525
import javax.servlet.ServletRequest;
26+
import javax.servlet.ServletRequestWrapper;
27+
import javax.servlet.ServletResponse;
28+
import javax.servlet.ServletResponseWrapper;
2629
import javax.servlet.http.Cookie;
2730
import javax.servlet.http.HttpServletRequest;
2831
import javax.servlet.http.HttpServletResponse;
@@ -366,6 +369,48 @@ public static Object getSessionMutex(HttpSession session) {
366369
}
367370

368371

372+
/**
373+
* Return an appropriate request object of the specified type, if available,
374+
* unwrapping the given request as far as necessary.
375+
* @param request the servlet request to introspect
376+
* @param requiredType the desired type of request object
377+
* @return the matching request object, or <code>null</code> if none
378+
* of that type is available
379+
*/
380+
@SuppressWarnings("unchecked")
381+
public static <T> T getNativeRequest(ServletRequest request, Class<T> requiredType) {
382+
if (requiredType != null) {
383+
if (requiredType.isInstance(request)) {
384+
return (T) request;
385+
}
386+
else if (request instanceof ServletRequestWrapper) {
387+
return getNativeRequest(((ServletRequestWrapper) request).getRequest(), requiredType);
388+
}
389+
}
390+
return null;
391+
}
392+
393+
/**
394+
* Return an appropriate response object of the specified type, if available,
395+
* unwrapping the given response as far as necessary.
396+
* @param response the servlet response to introspect
397+
* @param requiredType the desired type of response object
398+
* @return the matching response object, or <code>null</code> if none
399+
* of that type is available
400+
*/
401+
@SuppressWarnings("unchecked")
402+
public static <T> T getNativeResponse(ServletResponse response, Class<T> requiredType) {
403+
if (requiredType != null) {
404+
if (requiredType.isInstance(response)) {
405+
return (T) response;
406+
}
407+
else if (response instanceof ServletResponseWrapper) {
408+
return getNativeResponse(((ServletResponseWrapper) response).getResponse(), requiredType);
409+
}
410+
}
411+
return null;
412+
}
413+
369414
/**
370415
* Determine whether the given request is an include request,
371416
* that is, not a top-level HTTP request coming in from the outside.

0 commit comments

Comments
 (0)