1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.bind;
  17. import java.util.Iterator;
  18. import java.util.Map;
  19. import javax.servlet.ServletRequest;
  20. import org.springframework.beans.MutablePropertyValues;
  21. import org.springframework.validation.DataBinder;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import org.springframework.web.multipart.MultipartHttpServletRequest;
  24. /**
  25. * Use this class to perform manual data binding from servlet request
  26. * parameters to JavaBeans, including support for multipart files.
  27. * @author Rod Johnson
  28. * @author Juergen Hoeller
  29. */
  30. public class ServletRequestDataBinder extends DataBinder {
  31. private boolean bindEmptyMultipartFiles = true;
  32. /**
  33. * Create a new DataBinder instance.
  34. * @param target target object to bind onto
  35. * @param name name of the target object
  36. */
  37. public ServletRequestDataBinder(Object target, String name) {
  38. super(target, name);
  39. }
  40. /**
  41. * Set whether to bind empty MultipartFile parameters. Default is true.
  42. * <p>Turn this off if you want to keep an already bound MultipartFile
  43. * when the user resubmits the form without choosing a different file.
  44. * Else, the already bound MultipartFile will be replaced by an empty
  45. * MultipartFile holder.
  46. */
  47. public void setBindEmptyMultipartFiles(boolean bindEmptyMultipartFiles) {
  48. this.bindEmptyMultipartFiles = bindEmptyMultipartFiles;
  49. }
  50. /**
  51. * Bind the parameters of the given request to this binder's target,
  52. * also binding multipart files in case of a multipart request.
  53. * <p>This call can create field errors, representing basic binding
  54. * errors like a required field (code "required"), or type mismatch
  55. * between value and bean property (code "typeMismatch").
  56. * <p>Multipart files are bound via their parameter name, just like normal
  57. * HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
  58. * invoking a "setUploadedFile" setter method.
  59. * <p>The type of the target property for a multipart file can be MultipartFile,
  60. * byte[], or String. The latter two receive the contents of the uploaded file;
  61. * all metadata like original file name, content type, etc are lost in those cases.
  62. * @param request request with parameters to bind (can be multipart)
  63. * @see org.springframework.web.multipart.MultipartHttpServletRequest
  64. * @see org.springframework.web.multipart.MultipartFile
  65. */
  66. public void bind(ServletRequest request) {
  67. // bind normal HTTP parameters
  68. MutablePropertyValues pvs = new ServletRequestParameterPropertyValues(request);
  69. // bind multipart files
  70. if (request instanceof MultipartHttpServletRequest) {
  71. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  72. Map fileMap = multipartRequest.getFileMap();
  73. for (Iterator it = fileMap.keySet().iterator(); it.hasNext();) {
  74. String key = (String) it.next();
  75. MultipartFile value = (MultipartFile) fileMap.get(key);
  76. if (this.bindEmptyMultipartFiles || !value.isEmpty()) {
  77. pvs.addPropertyValue(key, value);
  78. }
  79. }
  80. }
  81. bind(pvs);
  82. }
  83. /**
  84. * Treats errors as fatal. Use this method only if
  85. * it's an error if the input isn't valid.
  86. * This might be appropriate if all input is from dropdowns, for example.
  87. * @throws ServletRequestBindingException subclass of ServletException on any binding problem
  88. */
  89. public void closeNoCatch() throws ServletRequestBindingException {
  90. if (getErrors().hasErrors()) {
  91. throw new ServletRequestBindingException("Errors binding onto object '" + getErrors().getObjectName() + "'",
  92. getErrors());
  93. }
  94. }
  95. }