@Override
  public void processAction(
      ActionMapping actionMapping,
      ActionForm actionForm,
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse)
      throws Exception {

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException();
        } else if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }

        throw new PortalException(uploadException.getCause());
      } else {
        addFileEntry(actionRequest);
      }

      String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

      if (Validator.isNotNull(redirect)) {
        actionResponse.sendRedirect(redirect);
      }
    } catch (Exception e) {
      handleUploadException(actionRequest, actionResponse, e);
    }
  }
Exemplo n.º 2
0
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    PortletConfig portletConfig = getPortletConfig(actionRequest);

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }

        throw new PortalException(uploadException.getCause());
      } else if (cmd.equals(Constants.ADD)) {
        addAttachment(actionRequest);
      } else if (cmd.equals(Constants.ADD_MULTIPLE)) {
        addMultipleFileEntries(portletConfig, actionRequest, actionResponse);
      } else if (cmd.equals(Constants.ADD_TEMP)) {
        addTempAttachment(actionRequest, actionResponse);
      } else if (cmd.equals(Constants.CHECK)) {
        JSONObject jsonObject = RestoreEntryUtil.checkEntry(actionRequest);

        JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);

        return;
      } else if (cmd.equals(Constants.DELETE)) {
        deleteAttachment(actionRequest, false);
      } else if (cmd.equals(Constants.DELETE_TEMP)) {
        deleteTempAttachment(actionRequest, actionResponse);
      } else if (cmd.equals(Constants.EMPTY_TRASH)) {
        emptyTrash(actionRequest);
      } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
        deleteAttachment(actionRequest, true);
      } else if (cmd.equals(Constants.RENAME)) {
        restoreRename(actionRequest);
      } else if (cmd.equals(Constants.RESTORE)) {
        restoreEntries(actionRequest);
      } else if (cmd.equals(Constants.OVERRIDE)) {
        restoreOverride(actionRequest);
      }

      if (cmd.equals(Constants.ADD_TEMP) || cmd.equals(Constants.DELETE_TEMP)) {

        actionResponse.setRenderParameter("mvcPath", "/null.jsp");
      }
    } catch (NoSuchNodeException | NoSuchPageException | PrincipalException e) {

      SessionErrors.add(actionRequest, e.getClass());

      actionResponse.setRenderParameter("mvcPath", "/wiki/error.jsp");
    } catch (Exception e) {
      handleUploadException(portletConfig, actionRequest, actionResponse, cmd, e);
    }
  }
  @Override
  public void processAction(
      ActionMapping actionMapping,
      ActionForm actionForm,
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse)
      throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    long maxFileSize = ParamUtil.getLong(actionRequest, "maxFileSize");

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }

        throw new PortalException(uploadException.getCause());
      } else if (cmd.equals(Constants.ADD_TEMP)) {
        FileEntry tempImageFileEntry = addTempImageFileEntry(actionRequest);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

        jsonObject.put("tempImageFileName", tempImageFileEntry.getTitle());

        writeJSON(actionRequest, actionResponse, jsonObject);
      } else {
        FileEntry fileEntry = null;

        boolean imageUploaded = ParamUtil.getBoolean(actionRequest, "imageUploaded");

        if (imageUploaded) {
          fileEntry = saveTempImageFileEntry(actionRequest);

          if (fileEntry.getSize() > maxFileSize) {
            throw new FileSizeException();
          }
        }

        SessionMessages.add(actionRequest, "imageUploaded", fileEntry);

        sendRedirect(actionRequest, actionResponse);
      }
    } catch (Exception e) {
      handleUploadException(actionRequest, actionResponse, cmd, maxFileSize, e);
    }
  }
Exemplo n.º 4
0
  protected void checkExceededSizeLimit(HttpServletRequest request) throws PortalException {

    UploadException uploadException =
        (UploadException) request.getAttribute(WebKeys.UPLOAD_EXCEPTION);

    if (uploadException != null) {
      if (uploadException.isExceededSizeLimit()) {
        throw new FileSizeException(uploadException.getCause());
      }

      throw new PortalException(uploadException.getCause());
    }
  }
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        Throwable cause = uploadException.getCause();

        if (uploadException.isExceededFileSizeLimit()) {
          throw new FileSizeException(cause);
        }

        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException(cause);
        }

        if (uploadException.isExceededUploadRequestSizeLimit()) {
          throw new UploadRequestSizeException(cause);
        }

        throw new PortalException(cause);
      }

      updateFileEntry(actionRequest, actionResponse);
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    } catch (PortalException pe) {

      // LPS-52675

      if (_log.isDebugEnabled()) {
        _log.debug(pe, pe);
      }

      handleUploadException(actionRequest, actionResponse);
    }
  }
  protected FileEntry addFileEntry(ActionRequest actionRequest) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    String sourceFileName = uploadPortletRequest.getFileName("file");
    String title = ParamUtil.getString(uploadPortletRequest, "title");
    String description = ParamUtil.getString(uploadPortletRequest, "description");
    String changeLog = ParamUtil.getString(uploadPortletRequest, "changeLog");

    if (folderId > 0) {
      Folder folder = DLAppServiceUtil.getFolder(folderId);

      if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
        throw new NoSuchFolderException("{folderId=" + folderId + "}");
      }
    }

    InputStream inputStream = null;

    try {
      String contentType = uploadPortletRequest.getContentType("file");

      long size = uploadPortletRequest.getSize("file");

      if (size == 0) {
        contentType = MimeTypesUtil.getContentType(title);
      }

      if (size > 0) {
        HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

        String[] mimeTypes = DocumentSelectorUtil.getMimeTypes(request);

        if (ArrayUtil.isNotEmpty(mimeTypes) && !ArrayUtil.contains(mimeTypes, contentType)) {

          throw new FileMimeTypeException(contentType);
        }
      }

      inputStream = uploadPortletRequest.getFileAsStream("file");

      ServiceContext serviceContext =
          ServiceContextFactory.getInstance(DLFileEntry.class.getName(), uploadPortletRequest);

      FileEntry fileEntry =
          DLAppServiceUtil.addFileEntry(
              themeDisplay.getScopeGroupId(),
              folderId,
              sourceFileName,
              contentType,
              title,
              description,
              changeLog,
              inputStream,
              size,
              serviceContext);

      AssetPublisherUtil.addRecentFolderId(actionRequest, DLFileEntry.class.getName(), folderId);

      return fileEntry;
    } catch (Exception e) {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException();
        } else if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }
      }

      throw e;
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    MBMessage message = null;

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        Throwable cause = uploadException.getCause();

        if (uploadException.isExceededFileSizeLimit()) {
          throw new FileSizeException(cause);
        }

        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException(cause);
        }

        if (uploadException.isExceededUploadRequestSizeLimit()) {
          throw new UploadRequestSizeException(cause);
        }

        throw new PortalException(cause);
      } else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {

        message = updateMessage(actionRequest, actionResponse);
      } else if (cmd.equals(Constants.ADD_ANSWER)) {
        addAnswer(actionRequest);
      } else if (cmd.equals(Constants.DELETE)) {
        deleteMessage(actionRequest);
      } else if (cmd.equals(Constants.DELETE_ANSWER)) {
        deleteAnswer(actionRequest);
      } else if (cmd.equals(Constants.LOCK)) {
        lockThreads(actionRequest);
      } else if (cmd.equals(Constants.SUBSCRIBE)) {
        subscribeMessage(actionRequest);
      } else if (cmd.equals(Constants.UNLOCK)) {
        unlockThreads(actionRequest);
      } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
        unsubscribeMessage(actionRequest);
      }

      if (Validator.isNotNull(cmd)) {
        WindowState windowState = actionRequest.getWindowState();

        if (!windowState.equals(LiferayWindowState.POP_UP)) {
          String redirect = getRedirect(actionRequest, actionResponse, message);

          sendRedirect(actionRequest, actionResponse, redirect);
        } else {
          String redirect =
              PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

          if (Validator.isNotNull(redirect)) {
            actionResponse.sendRedirect(redirect);
          }
        }
      }
    } catch (NoSuchMessageException | PrincipalException | RequiredMessageException e) {

      SessionErrors.add(actionRequest, e.getClass());

      actionResponse.setRenderParameter("mvcPath", "/message_boards/error.jsp");
    } catch (AntivirusScannerException
        | CaptchaConfigurationException
        | CaptchaMaxChallengesException
        | CaptchaTextException
        | DuplicateFileEntryException
        | FileExtensionException
        | FileNameException
        | FileSizeException
        | LiferayFileItemException
        | LockedThreadException
        | MessageBodyException
        | MessageSubjectException
        | SanitizerException
        | UploadRequestSizeException e) {

      if (e instanceof AntivirusScannerException) {
        SessionErrors.add(actionRequest, e.getClass(), e);
      } else {
        SessionErrors.add(actionRequest, e.getClass());
      }
    } catch (AssetCategoryException | AssetTagException e) {
      SessionErrors.add(actionRequest, e.getClass(), e);
    } catch (Exception e) {
      Throwable cause = e.getCause();

      if (cause instanceof SanitizerException) {
        SessionErrors.add(actionRequest, SanitizerException.class);
      } else {
        throw e;
      }
    }
  }
  public void updateArticle(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadException uploadException =
        (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

    if (uploadException != null) {
      if (uploadException.isExceededLiferayFileItemSizeLimit()) {
        throw new LiferayFileItemException();
      } else if (uploadException.isExceededSizeLimit()) {
        throw new ArticleContentSizeException();
      }

      throw new PortalException(uploadException.getCause());
    }

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    if (_log.isDebugEnabled()) {
      _log.debug("Updating article " + MapUtil.toString(uploadPortletRequest.getParameterMap()));
    }

    String actionName = ParamUtil.getString(actionRequest, ActionRequest.ACTION_NAME);

    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    long classNameId = ParamUtil.getLong(uploadPortletRequest, "classNameId");
    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");

    String articleId = ParamUtil.getString(uploadPortletRequest, "articleId");

    boolean autoArticleId = ParamUtil.getBoolean(uploadPortletRequest, "autoArticleId");
    double version = ParamUtil.getDouble(uploadPortletRequest, "version");

    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(actionRequest, "title");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "description");

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(JournalArticle.class.getName(), uploadPortletRequest);

    String ddmStructureKey = ParamUtil.getString(uploadPortletRequest, "ddmStructureKey");

    DDMStructure ddmStructure =
        _ddmStructureLocalService.getStructure(
            PortalUtil.getSiteGroupId(groupId),
            PortalUtil.getClassNameId(JournalArticle.class),
            ddmStructureKey,
            true);

    Object[] contentAndImages = ActionUtil.getContentAndImages(ddmStructure, serviceContext);

    String content = (String) contentAndImages[0];
    Map<String, byte[]> images = (HashMap<String, byte[]>) contentAndImages[1];

    Boolean fileItemThresholdSizeExceeded =
        (Boolean) uploadPortletRequest.getAttribute(WebKeys.FILE_ITEM_THRESHOLD_SIZE_EXCEEDED);

    if ((fileItemThresholdSizeExceeded != null) && fileItemThresholdSizeExceeded.booleanValue()) {

      throw new ArticleContentSizeException();
    }

    String ddmTemplateKey = ParamUtil.getString(uploadPortletRequest, "ddmTemplateKey");
    String layoutUuid = ParamUtil.getString(uploadPortletRequest, "layoutUuid");

    Layout targetLayout = JournalUtil.getArticleLayout(layoutUuid, groupId);

    if (targetLayout == null) {
      layoutUuid = null;
    }

    int displayDateMonth = ParamUtil.getInteger(uploadPortletRequest, "displayDateMonth");
    int displayDateDay = ParamUtil.getInteger(uploadPortletRequest, "displayDateDay");
    int displayDateYear = ParamUtil.getInteger(uploadPortletRequest, "displayDateYear");
    int displayDateHour = ParamUtil.getInteger(uploadPortletRequest, "displayDateHour");
    int displayDateMinute = ParamUtil.getInteger(uploadPortletRequest, "displayDateMinute");
    int displayDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "displayDateAmPm");

    if (displayDateAmPm == Calendar.PM) {
      displayDateHour += 12;
    }

    int expirationDateMonth = ParamUtil.getInteger(uploadPortletRequest, "expirationDateMonth");
    int expirationDateDay = ParamUtil.getInteger(uploadPortletRequest, "expirationDateDay");
    int expirationDateYear = ParamUtil.getInteger(uploadPortletRequest, "expirationDateYear");
    int expirationDateHour = ParamUtil.getInteger(uploadPortletRequest, "expirationDateHour");
    int expirationDateMinute = ParamUtil.getInteger(uploadPortletRequest, "expirationDateMinute");
    int expirationDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "expirationDateAmPm");
    boolean neverExpire = ParamUtil.getBoolean(uploadPortletRequest, "neverExpire");

    if (expirationDateAmPm == Calendar.PM) {
      expirationDateHour += 12;
    }

    int reviewDateMonth = ParamUtil.getInteger(uploadPortletRequest, "reviewDateMonth");
    int reviewDateDay = ParamUtil.getInteger(uploadPortletRequest, "reviewDateDay");
    int reviewDateYear = ParamUtil.getInteger(uploadPortletRequest, "reviewDateYear");
    int reviewDateHour = ParamUtil.getInteger(uploadPortletRequest, "reviewDateHour");
    int reviewDateMinute = ParamUtil.getInteger(uploadPortletRequest, "reviewDateMinute");
    int reviewDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "reviewDateAmPm");
    boolean neverReview = ParamUtil.getBoolean(uploadPortletRequest, "neverReview");

    if (reviewDateAmPm == Calendar.PM) {
      reviewDateHour += 12;
    }

    boolean indexable = ParamUtil.getBoolean(uploadPortletRequest, "indexable");

    boolean smallImage = ParamUtil.getBoolean(uploadPortletRequest, "smallImage");
    String smallImageURL = ParamUtil.getString(uploadPortletRequest, "smallImageURL");
    File smallFile = uploadPortletRequest.getFile("smallFile");

    String articleURL = ParamUtil.getString(uploadPortletRequest, "articleURL");

    JournalArticle article = null;
    String oldUrlTitle = StringPool.BLANK;

    if (actionName.equals("addArticle")) {

      // Add article

      article =
          _journalArticleService.addArticle(
              groupId,
              folderId,
              classNameId,
              classPK,
              articleId,
              autoArticleId,
              titleMap,
              descriptionMap,
              content,
              ddmStructureKey,
              ddmTemplateKey,
              layoutUuid,
              displayDateMonth,
              displayDateDay,
              displayDateYear,
              displayDateHour,
              displayDateMinute,
              expirationDateMonth,
              expirationDateDay,
              expirationDateYear,
              expirationDateHour,
              expirationDateMinute,
              neverExpire,
              reviewDateMonth,
              reviewDateDay,
              reviewDateYear,
              reviewDateHour,
              reviewDateMinute,
              neverReview,
              indexable,
              smallImage,
              smallImageURL,
              smallFile,
              images,
              articleURL,
              serviceContext);
    } else {

      // Update article

      article = _journalArticleService.getArticle(groupId, articleId, version);

      String tempOldUrlTitle = article.getUrlTitle();

      if (actionName.equals("previewArticle") || actionName.equals("updateArticle")) {

        article =
            _journalArticleService.updateArticle(
                groupId,
                folderId,
                articleId,
                version,
                titleMap,
                descriptionMap,
                content,
                ddmStructureKey,
                ddmTemplateKey,
                layoutUuid,
                displayDateMonth,
                displayDateDay,
                displayDateYear,
                displayDateHour,
                displayDateMinute,
                expirationDateMonth,
                expirationDateDay,
                expirationDateYear,
                expirationDateHour,
                expirationDateMinute,
                neverExpire,
                reviewDateMonth,
                reviewDateDay,
                reviewDateYear,
                reviewDateHour,
                reviewDateMinute,
                neverReview,
                indexable,
                smallImage,
                smallImageURL,
                smallFile,
                images,
                articleURL,
                serviceContext);
      }

      if (!tempOldUrlTitle.equals(article.getUrlTitle())) {
        oldUrlTitle = tempOldUrlTitle;
      }
    }

    // Recent articles

    JournalUtil.addRecentArticle(actionRequest, article);

    // Journal content

    String portletResource = ParamUtil.getString(actionRequest, "portletResource");

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    PortletPreferences portletPreferences =
        PortletPreferencesFactoryUtil.getStrictPortletSetup(
            themeDisplay.getLayout(), portletResource);

    if (portletPreferences != null) {
      portletPreferences.setValue("groupId", String.valueOf(article.getGroupId()));
      portletPreferences.setValue("articleId", article.getArticleId());

      portletPreferences.store();

      updateContentSearch(actionRequest, portletResource, article.getArticleId());
    }

    sendEditArticleRedirect(actionRequest, actionResponse, article, oldUrlTitle);
  }
  protected FileEntry updateFileEntry(
      PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);

    long fileEntryId = ParamUtil.getLong(uploadPortletRequest, "fileEntryId");

    long repositoryId = ParamUtil.getLong(uploadPortletRequest, "repositoryId");
    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    String sourceFileName = uploadPortletRequest.getFileName("file");
    String title = ParamUtil.getString(uploadPortletRequest, "title");
    String description = ParamUtil.getString(uploadPortletRequest, "description");
    String changeLog = ParamUtil.getString(uploadPortletRequest, "changeLog");
    boolean majorVersion = ParamUtil.getBoolean(uploadPortletRequest, "majorVersion");

    if (folderId > 0) {
      Folder folder = _dlAppService.getFolder(folderId);

      if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
        throw new NoSuchFolderException("{folderId=" + folderId + "}");
      }
    }

    InputStream inputStream = null;

    try {
      String contentType = uploadPortletRequest.getContentType("file");
      long size = uploadPortletRequest.getSize("file");

      if ((cmd.equals(Constants.ADD) || cmd.equals(Constants.ADD_DYNAMIC)) && (size == 0)) {

        contentType = MimeTypesUtil.getContentType(title);
      }

      if (cmd.equals(Constants.ADD) || cmd.equals(Constants.ADD_DYNAMIC) || (size > 0)) {

        String portletName = portletConfig.getPortletName();

        if (portletName.equals(DLPortletKeys.MEDIA_GALLERY_DISPLAY)) {
          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

          DLPortletInstanceSettings dlPortletInstanceSettings =
              DLPortletInstanceSettings.getInstance(
                  themeDisplay.getLayout(), portletDisplay.getId());

          String[] mimeTypes = dlPortletInstanceSettings.getMimeTypes();

          if (Arrays.binarySearch(mimeTypes, contentType) < 0) {
            throw new FileMimeTypeException(contentType);
          }
        }
      }

      inputStream = uploadPortletRequest.getFileAsStream("file");

      ServiceContext serviceContext =
          ServiceContextFactory.getInstance(DLFileEntry.class.getName(), uploadPortletRequest);

      FileEntry fileEntry = null;

      if (cmd.equals(Constants.ADD) || cmd.equals(Constants.ADD_DYNAMIC)) {

        // Add file entry

        fileEntry =
            _dlAppService.addFileEntry(
                repositoryId,
                folderId,
                sourceFileName,
                contentType,
                title,
                description,
                changeLog,
                inputStream,
                size,
                serviceContext);

        if (cmd.equals(Constants.ADD_DYNAMIC)) {
          JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

          jsonObject.put("fileEntryId", fileEntry.getFileEntryId());

          JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
        }
      } else if (cmd.equals(Constants.UPDATE_AND_CHECKIN)) {

        // Update file entry and checkin

        fileEntry =
            _dlAppService.updateFileEntryAndCheckIn(
                fileEntryId,
                sourceFileName,
                contentType,
                title,
                description,
                changeLog,
                majorVersion,
                inputStream,
                size,
                serviceContext);
      } else {

        // Update file entry

        fileEntry =
            _dlAppService.updateFileEntry(
                fileEntryId,
                sourceFileName,
                contentType,
                title,
                description,
                changeLog,
                majorVersion,
                inputStream,
                size,
                serviceContext);
      }

      return fileEntry;
    } catch (Exception e) {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException();
        } else if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }
      }

      throw e;
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    FileEntry fileEntry = null;

    PortletConfig portletConfig = getPortletConfig(actionRequest);

    try {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException();
        } else if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }

        throw new PortalException(uploadException.getCause());
      } else if (cmd.equals(Constants.ADD)
          || cmd.equals(Constants.ADD_DYNAMIC)
          || cmd.equals(Constants.UPDATE)
          || cmd.equals(Constants.UPDATE_AND_CHECKIN)) {

        fileEntry = updateFileEntry(portletConfig, actionRequest, actionResponse);
      } else if (cmd.equals(Constants.ADD_MULTIPLE)) {
        addMultipleFileEntries(portletConfig, actionRequest, actionResponse);

        hideDefaultSuccessMessage(actionRequest);
      } else if (cmd.equals(Constants.ADD_TEMP)) {
        addTempFileEntry(actionRequest, actionResponse);
      } else if (cmd.equals(Constants.DELETE)) {
        deleteFileEntry(actionRequest, false);
      } else if (cmd.equals(Constants.DELETE_TEMP)) {
        deleteTempFileEntry(actionRequest, actionResponse);
      } else if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
        cancelFileEntriesCheckOut(actionRequest);
      } else if (cmd.equals(Constants.CHECKIN)) {
        checkInFileEntries(actionRequest);
      } else if (cmd.equals(Constants.CHECKOUT)) {
        checkOutFileEntries(actionRequest);
      } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
        deleteFileEntry(actionRequest, true);
      } else if (cmd.equals(Constants.RESTORE)) {
        restoreTrashEntries(actionRequest);
      } else if (cmd.equals(Constants.REVERT)) {
        revertFileEntry(actionRequest);
      }

      WindowState windowState = actionRequest.getWindowState();

      if (cmd.equals(Constants.ADD_TEMP) || cmd.equals(Constants.DELETE_TEMP)) {

        actionResponse.setRenderParameter("mvcPath", "/null.jsp");
      } else if (cmd.equals(Constants.PREVIEW)) {
      } else if (!windowState.equals(LiferayWindowState.POP_UP)) {
      } else {
        String redirect = ParamUtil.getString(actionRequest, "redirect");
        int workflowAction =
            ParamUtil.getInteger(
                actionRequest, "workflowAction", WorkflowConstants.ACTION_SAVE_DRAFT);

        if ((fileEntry != null) && (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {

          redirect = getSaveAndContinueRedirect(portletConfig, actionRequest, fileEntry, redirect);

          sendRedirect(actionRequest, actionResponse, redirect);
        } else {
          if (windowState.equals(LiferayWindowState.POP_UP)) {
            redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

            if (Validator.isNotNull(redirect)) {
              if (cmd.equals(Constants.ADD) && (fileEntry != null)) {

                String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);

                String namespace = PortalUtil.getPortletNamespace(portletId);

                redirect =
                    HttpUtil.addParameter(
                        redirect, namespace + "className", DLFileEntry.class.getName());
                redirect =
                    HttpUtil.addParameter(
                        redirect, namespace + "classPK", fileEntry.getFileEntryId());
              }

              sendRedirect(actionRequest, actionResponse, redirect);
            }
          }
        }
      }
    } catch (Exception e) {
      handleUploadException(portletConfig, actionRequest, actionResponse, cmd, e);
    }
  }
  protected void addTempFileEntry(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    String sourceFileName = uploadPortletRequest.getFileName("file");

    StringBundler sb = new StringBundler(5);

    sb.append(FileUtil.stripExtension(sourceFileName));
    sb.append(DL.TEMP_RANDOM_SUFFIX);
    sb.append(StringUtil.randomString());

    String extension = FileUtil.getExtension(sourceFileName);

    if (Validator.isNotNull(extension)) {
      sb.append(StringPool.PERIOD);
      sb.append(extension);
    }

    InputStream inputStream = null;

    try {
      inputStream = uploadPortletRequest.getFileAsStream("file");

      String contentType = uploadPortletRequest.getContentType("file");

      FileEntry fileEntry =
          _dlAppService.addTempFileEntry(
              themeDisplay.getScopeGroupId(),
              folderId,
              TEMP_FOLDER_NAME,
              sb.toString(),
              inputStream,
              contentType);

      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      jsonObject.put("groupId", fileEntry.getGroupId());
      jsonObject.put("name", fileEntry.getTitle());
      jsonObject.put("title", sourceFileName);
      jsonObject.put("uuid", fileEntry.getUuid());

      JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
    } catch (Exception e) {
      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if ((uploadException != null)
          && (uploadException.getCause() instanceof FileUploadBase.IOFileUploadException)) {

        // Cancelled a temporary upload

      } else if ((uploadException != null) && uploadException.isExceededSizeLimit()) {

        throw new FileSizeException(uploadException.getCause());
      } else {
        throw e;
      }
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  @Override
  protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    try {
      BlogsEntry entry = null;
      String oldUrlTitle = StringPool.BLANK;
      List<BlogsEntryAttachmentFileEntryReference> blogsEntryAttachmentFileEntryReferences = null;

      UploadException uploadException =
          (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

      if (uploadException != null) {
        if (uploadException.isExceededLiferayFileItemSizeLimit()) {
          throw new LiferayFileItemException();
        } else if (uploadException.isExceededSizeLimit()) {
          throw new FileSizeException(uploadException.getCause());
        }

        throw new PortalException(uploadException.getCause());
      } else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {

        Callable<Object[]> updateEntryCallable = new UpdateEntryCallable(actionRequest);

        Object[] returnValue =
            TransactionInvokerUtil.invoke(_transactionAttribute, updateEntryCallable);

        entry = (BlogsEntry) returnValue[0];
        oldUrlTitle = ((String) returnValue[1]);
        blogsEntryAttachmentFileEntryReferences =
            ((List<BlogsEntryAttachmentFileEntryReference>) returnValue[2]);
      } else if (cmd.equals(Constants.DELETE)) {
        deleteEntries(actionRequest, false);
      } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
        deleteEntries(actionRequest, true);
      } else if (cmd.equals(Constants.RESTORE)) {
        restoreTrashEntries(actionRequest);
      } else if (cmd.equals(Constants.SUBSCRIBE)) {
        subscribe(actionRequest);
      } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
        unsubscribe(actionRequest);
      }

      String redirect = ParamUtil.getString(actionRequest, "redirect");
      boolean updateRedirect = false;

      String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);

      if (Validator.isNotNull(oldUrlTitle)) {
        String oldRedirectParam = PortalUtil.getPortletNamespace(portletId) + "redirect";

        String oldRedirect = HttpUtil.getParameter(redirect, oldRedirectParam, false);

        if (Validator.isNotNull(oldRedirect)) {
          String newRedirect = HttpUtil.decodeURL(oldRedirect);

          newRedirect = StringUtil.replace(newRedirect, oldUrlTitle, entry.getUrlTitle());
          newRedirect = StringUtil.replace(newRedirect, oldRedirectParam, "redirect");

          redirect = StringUtil.replace(redirect, oldRedirect, newRedirect);
        } else if (redirect.endsWith("/blogs/" + oldUrlTitle)
            || redirect.contains("/blogs/" + oldUrlTitle + "?")
            || redirect.contains("/blog/" + oldUrlTitle + "?")) {

          redirect = StringUtil.replace(redirect, oldUrlTitle, entry.getUrlTitle());
        }

        updateRedirect = true;
      }

      int workflowAction =
          ParamUtil.getInteger(
              actionRequest, "workflowAction", WorkflowConstants.ACTION_SAVE_DRAFT);

      boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");

      if (ajax) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

        JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

        for (BlogsEntryAttachmentFileEntryReference blogsEntryAttachmentFileEntryReference :
            blogsEntryAttachmentFileEntryReferences) {

          JSONObject blogsEntryFileEntryReferencesJSONObject = JSONFactoryUtil.createJSONObject();

          blogsEntryFileEntryReferencesJSONObject.put(
              "attributeDataImageId", EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
          blogsEntryFileEntryReferencesJSONObject.put(
              "fileEntryId",
              String.valueOf(
                  blogsEntryAttachmentFileEntryReference.getTempBlogsEntryAttachmentFileEntryId()));
          blogsEntryFileEntryReferencesJSONObject.put(
              "fileEntryUrl",
              PortletFileRepositoryUtil.getPortletFileEntryURL(
                  null,
                  blogsEntryAttachmentFileEntryReference.getBlogsEntryAttachmentFileEntry(),
                  StringPool.BLANK));

          jsonArray.put(blogsEntryFileEntryReferencesJSONObject);
        }

        jsonObject.put("blogsEntryAttachmentReferences", jsonArray);

        jsonObject.put("entryId", entry.getEntryId());
        jsonObject.put("redirect", redirect);
        jsonObject.put("updateRedirect", updateRedirect);

        JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);

        return;
      }

      if ((entry != null) && (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {

        redirect = getSaveAndContinueRedirect(actionRequest, entry, redirect);

        sendRedirect(actionRequest, actionResponse, redirect);
      } else {
        WindowState windowState = actionRequest.getWindowState();

        if (!windowState.equals(LiferayWindowState.POP_UP)) {
          sendRedirect(actionRequest, actionResponse, redirect);
        } else {
          redirect = PortalUtil.escapeRedirect(redirect);

          if (Validator.isNotNull(redirect)) {
            if (cmd.equals(Constants.ADD) && (entry != null)) {
              String namespace = PortalUtil.getPortletNamespace(portletId);

              redirect =
                  HttpUtil.addParameter(
                      redirect, namespace + "className", BlogsEntry.class.getName());
              redirect = HttpUtil.addParameter(redirect, namespace + "classPK", entry.getEntryId());
            }

            sendRedirect(actionRequest, actionResponse, redirect);
          }
        }
      }
    } catch (Exception e) {
      String mvcPath = "/blogs/edit_entry.jsp";

      if (e instanceof NoSuchEntryException || e instanceof PrincipalException) {

        SessionErrors.add(actionRequest, e.getClass());

        mvcPath = "/blogs/error.jsp";
      } else if (e instanceof EntryContentException
          || e instanceof EntryCoverImageCropException
          || e instanceof EntryDescriptionException
          || e instanceof EntryDisplayDateException
          || e instanceof EntrySmallImageNameException
          || e instanceof EntrySmallImageScaleException
          || e instanceof EntryTitleException
          || e instanceof FileSizeException
          || e instanceof LiferayFileItemException
          || e instanceof SanitizerException) {

        SessionErrors.add(actionRequest, e.getClass());
      } else if (e instanceof AssetCategoryException || e instanceof AssetTagException) {

        SessionErrors.add(actionRequest, e.getClass(), e);
      } else {
        Throwable cause = e.getCause();

        if (cause instanceof SanitizerException) {
          SessionErrors.add(actionRequest, SanitizerException.class);
        } else {
          throw e;
        }
      }

      actionResponse.setRenderParameter("mvcPath", mvcPath);
    } catch (Throwable t) {
      _log.error(t, t);

      actionResponse.setRenderParameter("mvcPath", "/blogs/error.jsp");
    }
  }