Example #1
0
  @Override
  @SuppressWarnings("unchecked")
  public final Map<String, Object> parse(final HttpServletRequest request) throws UWSException {
    LinkedHashMap<String, Object> parameters = new LinkedHashMap<String, Object>();
    MultipartRequest multipart = null;

    try {

      // Parse the request body:
      multipart =
          new MultipartRequest(
              request,
              UWSFileManager.TMP_UPLOAD_DIR.getPath(),
              (SIZE_LIMIT < 0 ? DEFAULT_SIZE_LIMIT : SIZE_LIMIT),
              new FileRenamePolicy() {
                @Override
                public File rename(File file) {
                  Object reqID = request.getAttribute(UWS.REQ_ATTRIBUTE_ID);
                  if (reqID == null || !(reqID instanceof String)) reqID = (new Date()).getTime();
                  char uniq = 'A';
                  File f =
                      new File(
                          file.getParentFile(), "UPLOAD_" + reqID + uniq + "_" + file.getName());
                  while (f.exists()) {
                    uniq++;
                    f = new File(file.getParentFile(), "UPLOAD_" + reqID + "_" + file.getName());
                  }
                  return f;
                }
              });

      // Extract all "normal" parameters:
      String param;
      Enumeration<String> e = multipart.getParameterNames();
      while (e.hasMoreElements()) {
        param = e.nextElement();
        for (String occurence : multipart.getParameterValues(param))
          consumeParameter(param, occurence, parameters);
      }

      // Extract all inline files as additional parameters:
      e = multipart.getFileNames();
      if (!allowUpload && e.hasMoreElements())
        throw new UWSException(
            UWSException.BAD_REQUEST, "Uploads are not allowed by this service!");
      while (e.hasMoreElements()) {
        param = e.nextElement();
        if (multipart.getFile(param) == null) continue;

        /*
         * TODO !!!POSSIBLE ISSUE!!!
         * MultipartRequest is not able to deal with multiple files having the same parameter name. However, all files are created/uploaded
         * but only the last one is accessible through this object....so only the last can be deleted, which could be a problem later
         * (hence the usage of the system temporary directory).
         */

        // build its description/pointer:
        UploadFile lob =
            new UploadFile(
                param,
                multipart.getOriginalFileName(param),
                multipart.getFile(param).toURI().toString(),
                fileManager);
        lob.mimeType = multipart.getContentType(param);
        lob.length = multipart.getFile(param).length();
        // add it inside the parameters map:
        consumeParameter(param, lob, parameters);
      }

    } catch (IOException ioe) {
      throw new UWSException(
          UWSException.INTERNAL_SERVER_ERROR,
          ioe,
          "Internal Error => Impossible to extract parameters from the Multipart HTTP request!");
    } catch (IllegalArgumentException iae) {
      String confError = iae.getMessage();
      if (UWSFileManager.TMP_UPLOAD_DIR == null) confError = "Missing upload directory!";
      throw new UWSException(
          UWSException.INTERNAL_SERVER_ERROR,
          iae,
          "Internal Error: Incorrect UPLOAD configuration: " + confError);
    }

    return parameters;
  }