@Override
  public void cleanupMultipart(MultipartHttpServletRequest request) {

    if (request != null) {

      DefaultMultipartHttpServletRequest defaultRequest =
          (DefaultMultipartHttpServletRequest) request;
      RequestWrapper requestWrapper = (RequestWrapper) defaultRequest.getRequest();

      // Call to the original method
      this.cleanupFileItems(getMultipartFileElements(requestWrapper.getFileElements()));
    }
  }
  @Override
  public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request)
      throws MultipartException {

    Assert.notNull(request, "Request must not be null");
    if (request instanceof RequestWrapper) {

      final RequestWrapper requestWrapper = (RequestWrapper) request;

      Exception multipartException =
          (Exception) request.getAttribute(IMultipartConfig.FILEUPLOAD_EXCEPTION);
      if (multipartException != null) {

        if (multipartException instanceof MaxUploadSizeExceededException) {
          throw (MaxUploadSizeExceededException) multipartException;
        } else {
          throw new MultipartException(
              "Could not parse multipart servlet request", multipartException);
        }
      }

      if (this.resolveLazily) {

        return new DefaultMultipartHttpServletRequest(requestWrapper) {
          @Override
          protected void initializeMultipart() {
            setMultipartFiles(getMultipartFileElements(requestWrapper.getFileElements()));
            setMultipartParameters(getMultipartTextElements(requestWrapper.getTextElements()));
          }
        };
      } else {
        return new DefaultMultipartHttpServletRequest(
            request,
            getMultipartFileElements(requestWrapper.getFileElements()),
            getMultipartTextElements(requestWrapper.getTextElements()));
      }
    } else {
      // The normal behaviour
      return super.resolveMultipart(request);
    }
  }
  /**
   * Adds a regular text parameter to the set of text parameters for this request. Handles the case
   * of multiple values for the same parameter by using an array for the parameter value.
   *
   * @param request The request in which the parameter was specified.
   * @param item The file item for the parameter to add.
   */
  public void addTextParameter(RequestWrapper request, FileItem item) {

    String name = item.getFieldName();
    String value = null;
    boolean haveValue = false;
    String encoding = request.getCharacterEncoding();

    if (encoding != null) {
      try {
        value = item.getString(encoding);
        haveValue = true;
      } catch (Exception e) {
        // Handled below, since haveValue is false.
      }
    }
    if (!haveValue) {
      try {
        value = item.getString("ISO-8859-1");
      } catch (java.io.UnsupportedEncodingException uee) {
        value = item.getString();
      }
      haveValue = true;
    }

    String[] oldArray = (String[]) request.getParameterValues(name);
    String[] newArray;

    if (oldArray != null) {
      newArray = new String[oldArray.length + 1];
      System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
      newArray[oldArray.length] = value;
    } else {
      newArray = new String[] {value};
    }

    request.addParameter(name, newArray);
  }
  /**
   * Parses the input stream and partitions the parsed items into a set of form fields and a set of
   * file items.
   *
   * @param request The multipart request wrapper.
   * @param servletContext Our ServletContext object
   * @return multipart processed request
   * @throws HdivMultipartException if an unrecoverable error occurs.
   */
  public HttpServletRequest handleMultipartRequest(
      RequestWrapper request, ServletContext servletContext) throws HdivMultipartException {

    DiskFileUpload upload = new DiskFileUpload();

    upload.setHeaderEncoding(request.getCharacterEncoding());
    // Set the maximum size before a FileUploadException will be thrown.
    upload.setSizeMax(getSizeMax());
    // Set the maximum size that will be stored in memory.
    upload.setSizeThreshold((int) getSizeThreshold());
    // Set the the location for saving data on disk.
    upload.setRepositoryPath(getRepositoryPath(servletContext));

    List<FileItem> items = null;
    try {
      items = upload.parseRequest(request);

    } catch (DiskFileUpload.SizeLimitExceededException e) {
      if (log.isErrorEnabled()) {
        log.error("Size limit exceeded exception");
      }
      // Special handling for uploads that are too big.
      throw new HdivMultipartException(e);

    } catch (FileUploadException e) {
      if (log.isErrorEnabled()) {
        log.error("Failed to parse multipart request", e);
      }
      throw new HdivMultipartException(e);
    }

    // Process the uploaded items
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
      FileItem item = iter.next();

      if (item.isFormField()) {
        this.addTextParameter(request, item);
      } else {
        this.addFileParameter(request, item);
      }
    }
    return request;
  }