Add javax.servlet.http.Part support for data binding
Prior to this commit, Multipart databinding would only support MultiPartFile databinding using commons-multipart. Now the WebRequestDataBinder supports Part and List<Part> databinding for Servlet 3.0 compliant containers. Issue: SPR-10591
This commit is contained in:
committed by
Rossen Stoyanchev
parent
41e411a8a5
commit
e91ce23cd0
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -16,11 +16,20 @@
|
||||
|
||||
package org.springframework.web.bind.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.multipart.MultipartRequest;
|
||||
|
||||
/**
|
||||
@@ -50,6 +59,7 @@ import org.springframework.web.multipart.MultipartRequest;
|
||||
* ...</pre>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Brian Clozel
|
||||
* @since 2.5.2
|
||||
* @see #bind(org.springframework.web.context.request.WebRequest)
|
||||
* @see #registerCustomEditor
|
||||
@@ -59,6 +69,7 @@ import org.springframework.web.multipart.MultipartRequest;
|
||||
*/
|
||||
public class WebRequestDataBinder extends WebDataBinder {
|
||||
|
||||
|
||||
/**
|
||||
* Create a new WebRequestDataBinder instance, with default object name.
|
||||
* @param target the target object to bind onto (or {@code null}
|
||||
@@ -89,22 +100,27 @@ public class WebRequestDataBinder extends WebDataBinder {
|
||||
* <p>Multipart files are bound via their parameter name, just like normal
|
||||
* HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
|
||||
* invoking a "setUploadedFile" setter method.
|
||||
* <p>The type of the target property for a multipart file can be MultipartFile,
|
||||
* <p>The type of the target property for a multipart file can be Part, MultipartFile,
|
||||
* byte[], or String. The latter two receive the contents of the uploaded file;
|
||||
* all metadata like original file name, content type, etc are lost in those cases.
|
||||
* @param request request with parameters to bind (can be multipart)
|
||||
* @see org.springframework.web.multipart.MultipartRequest
|
||||
* @see org.springframework.web.multipart.MultipartFile
|
||||
* @see #bindMultipartFiles
|
||||
* @see javax.servlet.http.Part
|
||||
* @see #bind(org.springframework.beans.PropertyValues)
|
||||
*/
|
||||
public void bind(WebRequest request) {
|
||||
MutablePropertyValues mpvs = new MutablePropertyValues(request.getParameterMap());
|
||||
if (request instanceof NativeWebRequest) {
|
||||
|
||||
if(isMultipartRequest(request) && (request instanceof NativeWebRequest)) {
|
||||
MultipartRequest multipartRequest = ((NativeWebRequest) request).getNativeRequest(MultipartRequest.class);
|
||||
if (multipartRequest != null) {
|
||||
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
|
||||
}
|
||||
else if (ClassUtils.hasMethod(HttpServletRequest.class, "getParts")) {
|
||||
HttpServletRequest serlvetRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class);
|
||||
new Servlet3MultipartHelper().bindParts(serlvetRequest, mpvs);
|
||||
}
|
||||
}
|
||||
doBind(mpvs);
|
||||
}
|
||||
@@ -121,4 +137,37 @@ public class WebRequestDataBinder extends WebDataBinder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is a multipart request (by checking its Content-Type header).
|
||||
*
|
||||
* @param request request with parameters to bind
|
||||
*/
|
||||
private boolean isMultipartRequest(WebRequest request) {
|
||||
String contentType = request.getHeader("Content-Type");
|
||||
return ((contentType != null) && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encapsulate Part binding code for Servlet 3.0+ only containers.
|
||||
* @see javax.servlet.http.Part
|
||||
*/
|
||||
private static class Servlet3MultipartHelper {
|
||||
|
||||
public void bindParts(HttpServletRequest request, MutablePropertyValues mpvs) {
|
||||
try {
|
||||
for(Part part : request.getParts()) {
|
||||
mpvs.add(part.getName(), part);
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new MultipartException("Failed to get request parts", ex);
|
||||
}
|
||||
catch(ServletException ex) {
|
||||
throw new MultipartException("Failed to get request parts", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user