Uploaded image for project: 'EJBCA'
  1. EJBCA
  2. ECA-3051

Replace deprecated use of Commons FileUpload API

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Cosmetic
    • Resolution: Won't Do
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: CA GUI, Public Web UI
    • Labels:
      None

      Description

      The following code was written in an attempt to get JSF 1.2 to handle file-uploads, but I ended up using t:inputFileUpload. For non-JSF code we could still use this (e.g. org.ejbca.ui.web.pub.RequestInstance.setParameters)

      /** Handles multipart content and transforms file uploads to B64 String values */
      public static Map<String,List<String>> getFormPostAsMap(final HttpServletRequest request, final long maxMemUsage) throws IOException, FileUploadException {
      long usedMemory = 0;
      final Map<String,List<String>> parameters = new HashMap<String,List<String>>();
      if (ServletFileUpload.isMultipartContent(request)) {
      final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
      upload.setSizeMax(maxMemUsage);
      upload.setFileSizeMax(0L);
      @SuppressWarnings("unchecked")
      final List<FileItem> fileItems = upload.parseRequest(request);
      for (final FileItem fileItem : fileItems) {
      final String fieldName = fileItem.getFieldName();
      final String value;
      if (fileItem.isFormField())

      { value = fileItem.getString(); }

      else {
      final long sizeInBytes = fileItem.getSize();
      if (log.isDebugEnabled())

      { final String fileName = fileItem.getName(); final String contentType = fileItem.getContentType(); final boolean isInMemory = fileItem.isInMemory(); log.debug("File upload. fileName="+fileName + " contentType="+contentType + " isInMemory="+isInMemory + " sizeInBytes="+sizeInBytes); }

      usedMemory += sizeInBytes *4/3; //B64 takes more space
      if (usedMemory > maxMemUsage)

      { throw new FileUploadException("Maximum allowed total size is " + maxMemUsage); }

      value = new String(Base64.encode(FileTools.readInputStreamtoBuffer(fileItem.getInputStream())));
      }
      List<String> currentParameterValue = parameters.get(fieldName);
      if (currentParameterValue == null)

      { currentParameterValue = new ArrayList<String>(); }

      currentParameterValue.add(value);
      parameters.put(fieldName, currentParameterValue);
      }
      } else {
      @SuppressWarnings("unchecked")
      final Map<String, String[]> parameterMap = request.getParameterMap();
      for (final String key : parameterMap.keySet())

      { parameters.put(key, Arrays.asList(parameterMap.get(key))); }

      }
      return parameters;
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              johan Johan Eklund
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: