Exemplo n.º 1
0
  /**
   * Called by the connector servlet to handle a {@code GET} request. In particular, it handles the
   * {@link Command#GET_FOLDERS GetFolders}, {@link Command#GET_FOLDERS_AND_FILES
   * GetFoldersAndFiles} and {@link Command#CREATE_FOLDER CreateFolder} commands.
   *
   * @param request the current request instance
   * @return the get response instance associated with this request
   */
  GetResponse doGet(final HttpServletRequest request) {
    Dispatcher.logger.debug("Entering Dispatcher#doGet");

    final Context context = ThreadLocalData.getContext();
    context.logBaseParameters();

    GetResponse getResponse = null;
    // check parameters
    if (!Command.isValidForGet(context.getCommandStr())) {
      getResponse = GetResponse.getInvalidCommandError();
    } else if (!ResourceType.isValidType(context.getTypeStr())) {
      getResponse = GetResponse.getInvalidResourceTypeError();
    } else if (!UtilsFile.isValidPath(context.getCurrentFolderStr())) {
      getResponse = GetResponse.getInvalidCurrentFolderError();
    } else {

      // in contrast to doPost the referrer has to send an explicit type
      final ResourceType type = context.getResourceType();
      final Command command = context.getCommand();

      // check permissions for user action
      if ((command.equals(Command.GET_FOLDERS) || command.equals(Command.GET_FOLDERS_AND_FILES))
          && !RequestCycleHandler.isGetResourcesEnabled(request)) {
        getResponse = GetResponse.getGetResourcesDisabledError();
      } else if (command.equals(Command.CREATE_FOLDER)
          && !RequestCycleHandler.isCreateFolderEnabled(request)) {
        getResponse = GetResponse.getCreateFolderDisabledError();
      } else {
        // make the connector calls, catch its exceptions and generate
        // the proper response object
        try {
          if (command.equals(Command.CREATE_FOLDER)) {
            final String newFolderNameStr = request.getParameter("NewFolderName");
            Dispatcher.logger.debug("Parameter NewFolderName: {}", newFolderNameStr);
            final String sanitizedNewFolderNameStr = UtilsFile.sanitizeFolderName(newFolderNameStr);
            if (Utils.isEmpty(sanitizedNewFolderNameStr)) {
              getResponse = GetResponse.getInvalidNewFolderNameError();
            } else {
              Dispatcher.logger.debug(
                  "Parameter NewFolderName (sanitized): {}", sanitizedNewFolderNameStr);
              this.connector.createFolder(
                  type, context.getCurrentFolderStr(), sanitizedNewFolderNameStr);
              getResponse = GetResponse.getOK();
            }
          } else if (command.equals(Command.GET_FOLDERS)
              || command.equals(Command.GET_FOLDERS_AND_FILES)) {
            final String url =
                UtilsResponse.getUrl(
                    RequestCycleHandler.getUserFilesPath(request),
                    type,
                    context.getCurrentFolderStr());
            getResponse =
                this.getFoldersAndOrFiles(command, type, context.getCurrentFolderStr(), url);
          }
        } catch (final InvalidCurrentFolderException e) {
          getResponse = GetResponse.getInvalidCurrentFolderError();
        } catch (final InvalidNewFolderNameException e) {
          getResponse = GetResponse.getInvalidNewFolderNameError();
        } catch (final FolderAlreadyExistsException e) {
          getResponse = GetResponse.getFolderAlreadyExistsError();
        } catch (final WriteException e) {
          getResponse = GetResponse.getCreateFolderWriteError();
        } catch (final ReadException e) {
          getResponse = GetResponse.getGetResourcesReadError();
        }
      }
    }

    Dispatcher.logger.debug("Exiting Dispatcher#doGet");
    return getResponse;
  }
Exemplo n.º 2
0
  /**
   * Called by the connector servlet to handle a {@code POST} request. In particular, it handles the
   * {@link Command#FILE_UPLOAD FileUpload} and {@link Command#QUICK_UPLOAD QuickUpload} commands.
   *
   * @param request the current request instance
   * @return the upload response instance associated with this request
   */
  @SuppressWarnings("unchecked")
  UploadResponse doPost(final HttpServletRequest request) {
    Dispatcher.logger.debug("Entering Dispatcher#doPost");

    final Context context = ThreadLocalData.getContext();
    context.logBaseParameters();

    UploadResponse uploadResponse = null;
    // check permissions for user actions
    if (!RequestCycleHandler.isFileUploadEnabled(request)) {
      uploadResponse = UploadResponse.getFileUploadDisabledError();
    } else if (!Command.isValidForPost(context.getCommandStr())) {
      uploadResponse = UploadResponse.getInvalidCommandError();
    } else if (!ResourceType.isValidType(context.getTypeStr())) {
      uploadResponse = UploadResponse.getInvalidResourceTypeError();
    } else if (!UtilsFile.isValidPath(context.getCurrentFolderStr())) {
      uploadResponse = UploadResponse.getInvalidCurrentFolderError();
    } else {

      // call the Connector#fileUpload
      final ResourceType type = context.getDefaultResourceType();
      final FileItemFactory factory = new DiskFileItemFactory();
      final ServletFileUpload upload = new ServletFileUpload(factory);
      try {
        final List<FileItem> items = upload.parseRequest(request);
        // We upload just one file at the same time
        final FileItem uplFile = items.get(0);
        // Some browsers transfer the entire source path not just the
        // filename
        final String fileName = FilenameUtils.getName(uplFile.getName());
        Dispatcher.logger.debug("Parameter NewFile: {}", fileName);
        // check the extension
        if (type.isDeniedExtension(FilenameUtils.getExtension(fileName))) {
          uploadResponse = UploadResponse.getInvalidFileTypeError();
        } else if (type.equals(ResourceType.IMAGE)
            && PropertiesLoader.isSecureImageUploads()
            && !UtilsFile.isImage(uplFile.getInputStream())) {
          uploadResponse = UploadResponse.getInvalidFileTypeError();
        } else {
          final String sanitizedFileName = UtilsFile.sanitizeFileName(fileName);
          Dispatcher.logger.debug("Parameter NewFile (sanitized): {}", sanitizedFileName);
          final String newFileName =
              this.connector.fileUpload(
                  type, context.getCurrentFolderStr(), sanitizedFileName, uplFile.getInputStream());
          final String fileUrl =
              UtilsResponse.fileUrl(
                  RequestCycleHandler.getUserFilesPath(request),
                  type,
                  context.getCurrentFolderStr(),
                  newFileName);

          if (sanitizedFileName.equals(newFileName)) {
            uploadResponse = UploadResponse.getOK(fileUrl);
          } else {
            uploadResponse = UploadResponse.getFileRenamedWarning(fileUrl, newFileName);
            Dispatcher.logger.debug("Parameter NewFile (renamed): {}", newFileName);
          }
        }

        uplFile.delete();
      } catch (final InvalidCurrentFolderException e) {
        uploadResponse = UploadResponse.getInvalidCurrentFolderError();
      } catch (final WriteException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      } catch (final IOException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      } catch (final FileUploadException e) {
        uploadResponse = UploadResponse.getFileUploadWriteError();
      }
    }

    Dispatcher.logger.debug("Exiting Dispatcher#doPost");
    return uploadResponse;
  }