diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/bind/PortletRequestDataBinder.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/bind/PortletRequestDataBinder.java index 26c3a5b55fa..a63b894d17c 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/bind/PortletRequestDataBinder.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/bind/PortletRequestDataBinder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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,6 +22,7 @@ import org.springframework.beans.MutablePropertyValues; import org.springframework.validation.BindException; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.multipart.MultipartRequest; +import org.springframework.web.portlet.util.PortletUtils; /** * Special {@link org.springframework.validation.DataBinder} to perform data binding @@ -105,8 +106,8 @@ public class PortletRequestDataBinder extends WebDataBinder { */ public void bind(PortletRequest request) { MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request); - if (request instanceof MultipartRequest) { - MultipartRequest multipartRequest = (MultipartRequest) request; + MultipartRequest multipartRequest = PortletUtils.getNativeRequest(request, MultipartRequest.class); + if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } doBind(mpvs); diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletWebRequest.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletWebRequest.java index 3eedfb651b6..d407f40fa9e 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletWebRequest.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/context/PortletWebRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -23,13 +23,12 @@ import java.util.Map; import javax.portlet.PortletRequest; import javax.portlet.PortletResponse; import javax.portlet.PortletSession; -import javax.portlet.filter.PortletRequestWrapper; -import javax.portlet.filter.PortletResponseWrapper; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.portlet.util.PortletUtils; /** * {@link org.springframework.web.context.request.WebRequest} adapter @@ -79,40 +78,12 @@ public class PortletWebRequest extends PortletRequestAttributes implements Nativ @SuppressWarnings("unchecked") public T getNativeRequest(Class requiredType) { - if (requiredType != null) { - PortletRequest request = getRequest(); - while (request != null) { - if (requiredType.isInstance(request)) { - return (T) request; - } - else if (request instanceof PortletRequestWrapper) { - request = ((PortletRequestWrapper) request).getRequest(); - } - else { - request = null; - } - } - } - return null; + return PortletUtils.getNativeRequest(getRequest(), requiredType); } @SuppressWarnings("unchecked") public T getNativeResponse(Class requiredType) { - if (requiredType != null) { - PortletResponse response = getResponse(); - while (response != null) { - if (requiredType.isInstance(response)) { - return (T) response; - } - else if (response instanceof PortletResponseWrapper) { - response = ((PortletResponseWrapper) response).getResponse(); - } - else { - response = null; - } - } - } - return null; + return PortletUtils.getNativeResponse(getResponse(), requiredType); } diff --git a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/util/PortletUtils.java b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/util/PortletUtils.java index cd9040b1b54..968229c7538 100644 --- a/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/util/PortletUtils.java +++ b/org.springframework.web.portlet/src/main/java/org/springframework/web/portlet/util/PortletUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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. @@ -32,6 +32,9 @@ import javax.portlet.PortletRequestDispatcher; import javax.portlet.PortletSession; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; +import javax.portlet.PortletResponse; +import javax.portlet.filter.PortletRequestWrapper; +import javax.portlet.filter.PortletResponseWrapper; import javax.servlet.http.Cookie; import org.springframework.util.Assert; @@ -276,6 +279,48 @@ public abstract class PortletUtils { } + /** + * Return an appropriate request object of the specified type, if available, + * unwrapping the given request as far as necessary. + * @param request the portlet request to introspect + * @param requiredType the desired type of request object + * @return the matching request object, or null if none + * of that type is available + */ + @SuppressWarnings("unchecked") + public static T getNativeRequest(PortletRequest request, Class requiredType) { + if (requiredType != null) { + if (requiredType.isInstance(request)) { + return (T) request; + } + else if (request instanceof PortletRequestWrapper) { + return getNativeRequest(((PortletRequestWrapper) request).getRequest(), requiredType); + } + } + return null; + } + + /** + * Return an appropriate response object of the specified type, if available, + * unwrapping the given response as far as necessary. + * @param response the portlet response to introspect + * @param requiredType the desired type of response object + * @return the matching response object, or null if none + * of that type is available + */ + @SuppressWarnings("unchecked") + public static T getNativeResponse(PortletResponse response, Class requiredType) { + if (requiredType != null) { + if (requiredType.isInstance(response)) { + return (T) response; + } + else if (response instanceof PortletResponseWrapper) { + return getNativeResponse(((PortletResponseWrapper) response).getResponse(), requiredType); + } + } + return null; + } + /** * Expose the given Map as request attributes, using the keys as attribute names * and the values as corresponding attribute values. Keys must be Strings. @@ -293,7 +338,7 @@ public abstract class PortletUtils { /** * Retrieve the first cookie with the given name. Note that multiple * cookies can have the same name but different paths or domains. - * @param request current servlet request + * @param request current portlet request * @param name cookie name * @return the first cookie with the given name, or null if none is found */ diff --git a/org.springframework.web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java b/org.springframework.web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java index 2edc899cfa9..f488024d9b6 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java +++ b/org.springframework.web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -21,6 +21,7 @@ import javax.servlet.ServletRequest; import org.springframework.beans.MutablePropertyValues; import org.springframework.validation.BindException; import org.springframework.web.multipart.MultipartRequest; +import org.springframework.web.util.WebUtils; /** * Special {@link org.springframework.validation.DataBinder} to perform data binding @@ -103,8 +104,8 @@ public class ServletRequestDataBinder extends WebDataBinder { */ public void bind(ServletRequest request) { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); - if (request instanceof MultipartRequest) { - MultipartRequest multipartRequest = (MultipartRequest) request; + MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); + if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } doBind(mpvs); diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java b/org.springframework.web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java index 1caf21e48db..9cb56566ed1 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java +++ b/org.springframework.web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java @@ -20,10 +20,6 @@ import java.security.Principal; import java.util.Iterator; import java.util.Locale; import java.util.Map; -import javax.servlet.ServletRequest; -import javax.servlet.ServletRequestWrapper; -import javax.servlet.ServletResponse; -import javax.servlet.ServletResponseWrapper; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -31,6 +27,7 @@ import javax.servlet.http.HttpSession; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import org.springframework.web.util.WebUtils; /** * {@link WebRequest} adapter for an {@link javax.servlet.http.HttpServletRequest}. @@ -92,40 +89,12 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ @SuppressWarnings("unchecked") public T getNativeRequest(Class requiredType) { - if (requiredType != null) { - ServletRequest request = getRequest(); - while (request != null) { - if (requiredType.isInstance(request)) { - return (T) request; - } - else if (request instanceof ServletRequestWrapper) { - request = ((ServletRequestWrapper) request).getRequest(); - } - else { - request = null; - } - } - } - return null; + return WebUtils.getNativeRequest(getRequest(), requiredType); } @SuppressWarnings("unchecked") public T getNativeResponse(Class requiredType) { - if (requiredType != null) { - ServletResponse response = getResponse(); - while (response != null) { - if (requiredType.isInstance(response)) { - return (T) response; - } - else if (response instanceof ServletResponseWrapper) { - response = ((ServletResponseWrapper) response).getResponse(); - } - else { - response = null; - } - } - } - return null; + return WebUtils.getNativeResponse(getResponse(), requiredType); } diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java b/org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java index c11df93a05f..8e19d709266 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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. @@ -23,6 +23,9 @@ import java.util.Map; import java.util.TreeMap; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; +import javax.servlet.ServletRequestWrapper; +import javax.servlet.ServletResponse; +import javax.servlet.ServletResponseWrapper; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -366,6 +369,48 @@ public abstract class WebUtils { } + /** + * Return an appropriate request object of the specified type, if available, + * unwrapping the given request as far as necessary. + * @param request the servlet request to introspect + * @param requiredType the desired type of request object + * @return the matching request object, or null if none + * of that type is available + */ + @SuppressWarnings("unchecked") + public static T getNativeRequest(ServletRequest request, Class requiredType) { + if (requiredType != null) { + if (requiredType.isInstance(request)) { + return (T) request; + } + else if (request instanceof ServletRequestWrapper) { + return getNativeRequest(((ServletRequestWrapper) request).getRequest(), requiredType); + } + } + return null; + } + + /** + * Return an appropriate response object of the specified type, if available, + * unwrapping the given response as far as necessary. + * @param response the servlet response to introspect + * @param requiredType the desired type of response object + * @return the matching response object, or null if none + * of that type is available + */ + @SuppressWarnings("unchecked") + public static T getNativeResponse(ServletResponse response, Class requiredType) { + if (requiredType != null) { + if (requiredType.isInstance(response)) { + return (T) response; + } + else if (response instanceof ServletResponseWrapper) { + return getNativeResponse(((ServletResponseWrapper) response).getResponse(), requiredType); + } + } + return null; + } + /** * Determine whether the given request is an include request, * that is, not a top-level HTTP request coming in from the outside.