Servlet/PortletRequestDataBinder perform unwrapping for MultipartRequest as well (SPR-7795)

This commit is contained in:
Juergen Hoeller 2011-01-26 20:09:42 +00:00
parent 4835be78b7
commit b3f039ae5f
6 changed files with 108 additions and 76 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.validation.BindException;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.portlet.util.PortletUtils;
/** /**
* Special {@link org.springframework.validation.DataBinder} to perform data binding * Special {@link org.springframework.validation.DataBinder} to perform data binding
@ -105,8 +106,8 @@ public class PortletRequestDataBinder extends WebDataBinder {
*/ */
public void bind(PortletRequest request) { public void bind(PortletRequest request) {
MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request); MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request);
if (request instanceof MultipartRequest) { MultipartRequest multipartRequest = PortletUtils.getNativeRequest(request, MultipartRequest.class);
MultipartRequest multipartRequest = (MultipartRequest) request; if (multipartRequest != null) {
bindMultipart(multipartRequest.getMultiFileMap(), mpvs); bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
} }
doBind(mpvs); doBind(mpvs);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.PortletRequest;
import javax.portlet.PortletResponse; import javax.portlet.PortletResponse;
import javax.portlet.PortletSession; import javax.portlet.PortletSession;
import javax.portlet.filter.PortletRequestWrapper;
import javax.portlet.filter.PortletResponseWrapper;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.portlet.util.PortletUtils;
/** /**
* {@link org.springframework.web.context.request.WebRequest} adapter * {@link org.springframework.web.context.request.WebRequest} adapter
@ -79,40 +78,12 @@ public class PortletWebRequest extends PortletRequestAttributes implements Nativ
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getNativeRequest(Class<T> requiredType) { public <T> T getNativeRequest(Class<T> requiredType) {
if (requiredType != null) { return PortletUtils.getNativeRequest(getRequest(), requiredType);
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;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getNativeResponse(Class<T> requiredType) { public <T> T getNativeResponse(Class<T> requiredType) {
if (requiredType != null) { return PortletUtils.getNativeResponse(getResponse(), requiredType);
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;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.PortletSession;
import javax.portlet.ResourceRequest; import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse; import javax.portlet.ResourceResponse;
import javax.portlet.PortletResponse;
import javax.portlet.filter.PortletRequestWrapper;
import javax.portlet.filter.PortletResponseWrapper;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import org.springframework.util.Assert; 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 <code>null</code> if none
* of that type is available
*/
@SuppressWarnings("unchecked")
public static <T> T getNativeRequest(PortletRequest request, Class<T> 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 <code>null</code> if none
* of that type is available
*/
@SuppressWarnings("unchecked")
public static <T> T getNativeResponse(PortletResponse response, Class<T> 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 * Expose the given Map as request attributes, using the keys as attribute names
* and the values as corresponding attribute values. Keys must be Strings. * 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 * Retrieve the first cookie with the given name. Note that multiple
* cookies can have the same name but different paths or domains. * 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 * @param name cookie name
* @return the first cookie with the given name, or <code>null</code> if none is found * @return the first cookie with the given name, or <code>null</code> if none is found
*/ */

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.beans.MutablePropertyValues;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.util.WebUtils;
/** /**
* Special {@link org.springframework.validation.DataBinder} to perform data binding * Special {@link org.springframework.validation.DataBinder} to perform data binding
@ -103,8 +104,8 @@ public class ServletRequestDataBinder extends WebDataBinder {
*/ */
public void bind(ServletRequest request) { public void bind(ServletRequest request) {
MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
if (request instanceof MultipartRequest) { MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
MultipartRequest multipartRequest = (MultipartRequest) request; if (multipartRequest != null) {
bindMultipart(multipartRequest.getMultiFileMap(), mpvs); bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
} }
doBind(mpvs); doBind(mpvs);

View File

@ -20,10 +20,6 @@ import java.security.Principal;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Map; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -31,6 +27,7 @@ import javax.servlet.http.HttpSession;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.util.WebUtils;
/** /**
* {@link WebRequest} adapter for an {@link javax.servlet.http.HttpServletRequest}. * {@link WebRequest} adapter for an {@link javax.servlet.http.HttpServletRequest}.
@ -92,40 +89,12 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getNativeRequest(Class<T> requiredType) { public <T> T getNativeRequest(Class<T> requiredType) {
if (requiredType != null) { return WebUtils.getNativeRequest(getRequest(), requiredType);
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;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getNativeResponse(Class<T> requiredType) { public <T> T getNativeResponse(Class<T> requiredType) {
if (requiredType != null) { return WebUtils.getNativeResponse(getResponse(), requiredType);
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;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.util.TreeMap;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletRequest; 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.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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 <code>null</code> if none
* of that type is available
*/
@SuppressWarnings("unchecked")
public static <T> T getNativeRequest(ServletRequest request, Class<T> 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 <code>null</code> if none
* of that type is available
*/
@SuppressWarnings("unchecked")
public static <T> T getNativeResponse(ServletResponse response, Class<T> 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, * Determine whether the given request is an include request,
* that is, not a top-level HTTP request coming in from the outside. * that is, not a top-level HTTP request coming in from the outside.