Ejemplo n.º 1
0
  /**
   * 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;
  }
Ejemplo n.º 2
0
  /**
   * 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);
  }