/**
   * Parse request parameters and files.
   *
   * @param request
   * @param response
   */
  protected void parseRequest(HttpServletRequest request, HttpServletResponse response) {
    requestParams = new HashMap<String, Object>();
    listFiles = new ArrayList<FileItemStream>();
    listFileStreams = new ArrayList<ByteArrayOutputStream>();

    // Parse the request
    if (ServletFileUpload.isMultipartContent(request)) {
      // multipart request
      try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
          FileItemStream item = iter.next();
          String name = item.getFieldName();
          InputStream stream = item.openStream();
          if (item.isFormField()) {
            requestParams.put(name, Streams.asString(stream));
          } else {
            String fileName = item.getName();
            if (fileName != null && !"".equals(fileName.trim())) {
              listFiles.add(item);

              ByteArrayOutputStream os = new ByteArrayOutputStream();
              IOUtils.copy(stream, os);
              listFileStreams.add(os);
            }
          }
        }
      } catch (Exception e) {
        logger.error("Unexpected error parsing multipart content", e);
      }
    } else {
      // not a multipart
      for (Object mapKey : request.getParameterMap().keySet()) {
        String mapKeyString = (String) mapKey;

        if (mapKeyString.endsWith("[]")) {
          // multiple values
          String values[] = request.getParameterValues(mapKeyString);
          List<String> listeValues = new ArrayList<String>();
          for (String value : values) {
            listeValues.add(value);
          }
          requestParams.put(mapKeyString, listeValues);
        } else {
          // single value
          String value = request.getParameter(mapKeyString);
          requestParams.put(mapKeyString, value);
        }
      }
    }
  }
  /**
   * Processing a new request from ElFinder client.
   *
   * @param request
   * @param response
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
    parseRequest(request, response);
    IDiagramProfile profile = ServletUtil.getProfile(request, "jbpm", getServletContext());
    Repository repository = profile.getRepository();
    if (!initialized) {
      try {
        initializeDefaultRepo(profile, repository, request);
        initialized = true;
      } catch (Exception e) {
        logger.error("Unable to initialize repository: " + e.getMessage());
      }
    }
    JSONObject returnJson = new JSONObject();
    try {
      System.out.println(
          "*********************************** COMMAND: " + requestParams.get("cmd"));
      Iterator<String> keys = requestParams.keySet().iterator();
      while (keys.hasNext()) {
        String key = keys.next();
        System.out.println(
            "******************************* PARAM: " + key + " -- val: " + requestParams.get(key));
      }

      String cmd = (String) requestParams.get("cmd");
      if (cmd != null && cmd.equals("open")) {
        OpenCommand command = new OpenCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("mkdir")) {
        MakeDirCommand command = new MakeDirCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("mkfile")) {
        MakeFileCommand command = new MakeFileCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("rm")) {
        RemoveAssetCommand command = new RemoveAssetCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("rename")) {
        RenameCommand command = new RenameCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("paste")) {
        PasteCommand command = new PasteCommand();
        command.init(request, response, profile, repository, requestParams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("upload")) {
        UploadCommand command = new UploadCommand();
        command.init(
            request, response, profile, repository, requestParams, listFiles, listFileStreams);
        output(response, false, command.execute());
      } else if (cmd != null && cmd.equals("getsvg")) {
        try {
          Asset asset =
              profile.getRepository().loadAssetFromPath((String) requestParams.get("current"));
          if (asset != null && asset.getAssetContent() != null) {
            outputPlain(response, false, (String) asset.getAssetContent(), "image/svg+xml");
          } else {
            outputPlain(
                response,
                true,
                "<p><b>Process image not available.</p><p>You can generate the process image in the process editor.</b></p>",
                null);
          }
        } catch (AssetNotFoundException e) {
          logger.warn("Error loading process image: " + e.getMessage());
          outputPlain(
              response,
              true,
              "<p><b>Could not find process image.</p><p>You can generate the process image in the process editor.</b></p>",
              null);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      putResponse(returnJson, "error", e.getMessage());

      // output the error
      try {
        output(response, false, returnJson);
      } catch (Exception ee) {
        logger.error("", ee);
      }
    }
  }