示例#1
0
  /**
   * handles error from execute command.
   *
   * @param e exception
   * @param request request
   * @param response response
   * @param configuration connector configuration
   * @param currentCommand current command
   * @throws ServletException when error handling fails.
   */
  private void handleError(
      final ConnectorException e,
      final IConfiguration configuration,
      final HttpServletRequest request,
      final HttpServletResponse response,
      final String currentCommand)
      throws ServletException {
    try {
      if (currentCommand != null) {
        Command command = CommandHandlerEnum.valueOf(currentCommand.toUpperCase()).getCommand();
        if (command instanceof XMLCommand) {
          CommandHandlerEnum.XMLERROR.execute(
              request, response, configuration, getServletContext(), e);
        } else {
          CommandHandlerEnum.ERROR.execute(
              request, response, configuration, getServletContext(), e);
        }
      } else {
        CommandHandlerEnum.XMLERROR.execute(
            request, response, configuration, getServletContext(), e);
      }

    } catch (Exception e1) {
      throw new ServletException(e1);
    }
  }
示例#2
0
  /**
   * Creating reponse for every command in request param.
   *
   * @param request request
   * @param response response
   * @param post if it's post command.
   * @throws ServletException when error occurs.
   */
  private void getResponse(
      final HttpServletRequest request, final HttpServletResponse response, final boolean post)
      throws ServletException {
    // 判断文件夹是否存在,不存在则创建,此处控制用户只查看自己有权限的文件
    if (request.getSession().getAttribute("currentFolder") != null) {
      String filesFolder =
          request.getRealPath(
              "/userfiles/files/" + request.getSession().getAttribute("currentFolder").toString());
      File filesFolderFile = new File(filesFolder);
      if (!filesFolderFile.exists()) {
        filesFolderFile.mkdirs();
      }
      String flashFolder =
          request.getRealPath(
              "/userfiles/flash/" + request.getSession().getAttribute("currentFolder").toString());
      File flashFolderFile = new File(flashFolder);
      if (!flashFolderFile.exists()) {
        flashFolderFile.mkdirs();
      }
      String imagesFolder =
          request.getRealPath(
              "/userfiles/images/" + request.getSession().getAttribute("currentFolder").toString());
      File imagesFolderFile = new File(imagesFolder);
      if (!imagesFolderFile.exists()) {
        imagesFolderFile.mkdirs();
      }
    }
    if (startException != null && Boolean.valueOf(getServletConfig().getInitParameter("debug"))) {
      throw new ServletException(startException);
    }
    String command = request.getParameter("command");
    IConfiguration configuration = null;
    try {
      configuration = ConfigurationFactory.getInstace().getConfiguration(request);
      if (configuration == null) {
        throw new Exception("Configuration wasn't initialized correctly. Check server logs.");
      }
    } catch (Exception e) {
      if (Boolean.valueOf(getServletConfig().getInitParameter("debug"))) {
        e.printStackTrace();
      }
      throw new ServletException(e);
    }
    try {

      if (command == null || command.equals("")) {
        throw new ConnectorException(
            Constants.Errors.CKFINDER_CONNECTOR_ERROR_INVALID_COMMAND, false);
      }

      configuration.setDebugMode(Boolean.valueOf(getServletConfig().getInitParameter("debug")));

      CommandHandlerEnum cmd = null;

      try {
        cmd = CommandHandlerEnum.valueOf(command.toUpperCase());
        // checks if command should go via POST request or it's a post request
        // and it's not upload command
        if ((cmd.getCommand() instanceof IPostCommand || post)
            && !CommandHandlerEnum.FILEUPLOAD.equals(cmd)
            && !CommandHandlerEnum.QUICKUPLOAD.equals(cmd)) {
          checkPostRequest(request);
        }
      } catch (IllegalArgumentException e1) {
        // Ignore custom plugins commands
      }

      BeforeExecuteCommandEventArgs args = new BeforeExecuteCommandEventArgs();
      args.setCommand(command);
      args.setRequest(request);
      args.setResponse(response);

      if (configuration.getEvents() != null) {
        if (configuration.getEvents().run(EventTypes.BeforeExecuteCommand, args, configuration)) {
          cmd = CommandHandlerEnum.valueOf(command.toUpperCase());
          cmd.execute(request, response, configuration, getServletContext());
        }
      } else {
        cmd = CommandHandlerEnum.valueOf(command.toUpperCase());
        cmd.execute(request, response, configuration, getServletContext());
      }
    } catch (IllegalArgumentException e) {
      if (Boolean.valueOf(getServletConfig().getInitParameter("debug"))) {
        e.printStackTrace();
        response.reset();
        throw new ServletException(e);
      } else {
        handleError(
            new ConnectorException(
                Constants.Errors.CKFINDER_CONNECTOR_ERROR_INVALID_COMMAND, false),
            configuration,
            request,
            response,
            command);
      }
    } catch (ConnectorException e) {
      if (Boolean.valueOf(getServletConfig().getInitParameter("debug"))
          && e.getException() != null) {
        e.getException().printStackTrace();
        response.reset();
        throw new ServletException(e.getException());
      } else {
        handleError(e, configuration, request, response, command);
      }
    } catch (Exception e) {
      if (Boolean.valueOf(getServletConfig().getInitParameter("debug"))) {
        e.printStackTrace();
        response.reset();
        throw new ServletException(e);
      } else {
        handleError(new ConnectorException(e), configuration, request, response, command);
      }
    }
  }