/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.wiki.web.internal.portlet.action;
import com.liferay.asset.kernel.exception.AssetCategoryException;
import com.liferay.asset.kernel.exception.AssetTagException;
import com.liferay.document.library.kernel.antivirus.AntivirusScannerException;
import com.liferay.document.library.kernel.exception.DuplicateFileEntryException;
import com.liferay.document.library.kernel.exception.DuplicateFolderNameException;
import com.liferay.document.library.kernel.exception.FileExtensionException;
import com.liferay.document.library.kernel.exception.FileMimeTypeException;
import com.liferay.document.library.kernel.exception.FileNameException;
import com.liferay.document.library.kernel.exception.FileSizeException;
import com.liferay.document.library.kernel.exception.InvalidFileVersionException;
import com.liferay.document.library.kernel.exception.NoSuchFileEntryException;
import com.liferay.document.library.kernel.exception.NoSuchFolderException;
import com.liferay.document.library.kernel.exception.SourceFileNameException;
import com.liferay.document.library.kernel.util.DLValidator;
import com.liferay.dynamic.data.mapping.kernel.StorageFieldRequiredException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.lock.DuplicateLockException;
import com.liferay.portal.kernel.model.TrashedModel;
import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.servlet.ServletResponseConstants;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.upload.LiferayFileItemException;
import com.liferay.portal.kernel.upload.UploadException;
import com.liferay.portal.kernel.upload.UploadRequestSizeException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.TextFormatter;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.wiki.constants.WikiPortletKeys;
import com.liferay.wiki.exception.NoSuchNodeException;
import com.liferay.wiki.exception.NoSuchPageException;
import com.liferay.wiki.web.internal.WikiAttachmentsHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Jorge Ferrer
* @author Roberto Díaz
*/
@Component(
immediate = true,
property = {
"javax.portlet.name=" + WikiPortletKeys.WIKI,
"javax.portlet.name=" + WikiPortletKeys.WIKI_ADMIN,
"javax.portlet.name=" + WikiPortletKeys.WIKI_DISPLAY,
"mvc.command.name=/wiki/edit_page_attachment"
},
service = MVCActionCommand.class
)
public class EditPageAttachmentsMVCActionCommand extends BaseMVCActionCommand {
@Reference(unbind = "-")
public void setWikiAttachmentsHelper(
WikiAttachmentsHelper wikiAttachmentsHelper) {
_wikiAttachmentsHelper = wikiAttachmentsHelper;
}
protected void deleteAttachment(
ActionRequest actionRequest, boolean moveToTrash)
throws Exception {
TrashedModel trashedModel = _wikiAttachmentsHelper.deleteAttachment(
actionRequest, moveToTrash);
if (moveToTrash && (trashedModel != null)) {
Map<String, Object> data = new HashMap<>();
data.put(Constants.CMD, Constants.REMOVE);
List<TrashedModel> trashedModels = new ArrayList<>();
trashedModels.add(trashedModel);
data.put("trashedModels", trashedModels);
addDeleteSuccessData(actionRequest, data);
}
}
@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) {
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)) {
_wikiAttachmentsHelper.addAttachments(actionRequest);
}
else if (cmd.equals(Constants.DELETE)) {
deleteAttachment(actionRequest, false);
}
else if (cmd.equals(Constants.EMPTY_TRASH)) {
_wikiAttachmentsHelper.emptyTrash(actionRequest);
}
else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
deleteAttachment(actionRequest, true);
}
else if (cmd.equals(Constants.RESTORE)) {
_wikiAttachmentsHelper.restoreEntries(actionRequest);
String redirect = ParamUtil.getString(
actionRequest, "redirect");
if (Validator.isNotNull(redirect)) {
actionResponse.sendRedirect(redirect);
}
}
}
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);
}
}
/**
* TODO: Remove. This should extend from EditFileEntryAction once it is
* modularized.
*/
protected String[] getAllowedFileExtensions(
PortletConfig portletConfig, PortletRequest portletRequest,
PortletResponse portletResponse)
throws PortalException {
return PrefsPropsUtil.getStringArray(
PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
}
/**
* TODO: Remove. This should extend from EditFileEntryAction once it is
* modularized.
*/
protected void handleUploadException(
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse, String cmd, Exception e)
throws Exception {
if (e instanceof AssetCategoryException ||
e instanceof AssetTagException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else if (e instanceof AntivirusScannerException ||
e instanceof DuplicateFileEntryException ||
e instanceof DuplicateFolderNameException ||
e instanceof FileExtensionException ||
e instanceof FileMimeTypeException ||
e instanceof FileNameException ||
e instanceof FileSizeException ||
e instanceof LiferayFileItemException ||
e instanceof NoSuchFolderException ||
e instanceof SourceFileNameException ||
e instanceof StorageFieldRequiredException ||
e instanceof UploadRequestSizeException) {
if (!cmd.equals(Constants.ADD_DYNAMIC) &&
!cmd.equals(Constants.ADD_MULTIPLE) &&
!cmd.equals(Constants.ADD_TEMP)) {
if (e instanceof AntivirusScannerException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else {
SessionErrors.add(actionRequest, e.getClass());
}
return;
}
else if (cmd.equals(Constants.ADD_TEMP)) {
hideDefaultErrorMessage(actionRequest);
}
if (e instanceof AntivirusScannerException ||
e instanceof DuplicateFileEntryException ||
e instanceof FileExtensionException ||
e instanceof FileNameException ||
e instanceof FileSizeException ||
e instanceof UploadRequestSizeException) {
HttpServletResponse response = _portal.getHttpServletResponse(
actionResponse);
response.setContentType(ContentTypes.TEXT_HTML);
response.setStatus(HttpServletResponse.SC_OK);
String errorMessage = StringPool.BLANK;
int errorType = 0;
ThemeDisplay themeDisplay =
(ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
if (e instanceof AntivirusScannerException) {
AntivirusScannerException ase =
(AntivirusScannerException)e;
errorMessage = themeDisplay.translate(ase.getMessageKey());
errorType =
ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
}
if (e instanceof DuplicateFileEntryException) {
errorMessage = themeDisplay.translate(
"please-enter-a-unique-document-name");
errorType =
ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
}
else if (e instanceof FileExtensionException) {
errorMessage = themeDisplay.translate(
"please-enter-a-file-with-a-valid-extension-x",
StringUtil.merge(
getAllowedFileExtensions(
portletConfig, actionRequest, actionResponse)));
errorType =
ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
}
else if (e instanceof FileNameException) {
errorMessage = themeDisplay.translate(
"please-enter-a-file-with-a-valid-file-name");
errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
}
else if (e instanceof FileSizeException) {
errorMessage = themeDisplay.translate(
"please-enter-a-file-with-a-valid-file-size-no-" +
"larger-than-x",
TextFormatter.formatStorageSize(
_dlValidator.getMaxAllowableSize(),
themeDisplay.getLocale()));
errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
}
JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
jsonObject.put("message", errorMessage);
jsonObject.put("status", errorType);
JSONPortletResponseUtil.writeJSON(
actionRequest, actionResponse, jsonObject);
}
if (e instanceof AntivirusScannerException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else {
SessionErrors.add(actionRequest, e.getClass());
}
}
else if (e instanceof DuplicateLockException ||
e instanceof InvalidFileVersionException ||
e instanceof NoSuchFileEntryException ||
e instanceof PrincipalException) {
if (e instanceof DuplicateLockException) {
DuplicateLockException dle = (DuplicateLockException)e;
SessionErrors.add(actionRequest, dle.getClass(), dle.getLock());
}
else {
SessionErrors.add(actionRequest, e.getClass());
}
actionResponse.setRenderParameter(
"mvcPath", "/html/porltet/document_library/error.jsp");
}
else {
Throwable cause = e.getCause();
if (cause instanceof DuplicateFileEntryException) {
SessionErrors.add(
actionRequest, DuplicateFileEntryException.class);
}
else {
throw e;
}
}
}
@Reference
private DLValidator _dlValidator;
@Reference
private Portal _portal;
private WikiAttachmentsHelper _wikiAttachmentsHelper;
}