PageRenderTime 70ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/apps/upload/upload-web/src/main/java/com/liferay/upload/web/internal/MultipleUploadResponseHandler.java

http://github.com/liferay/liferay-portal
Java | 180 lines | 138 code | 26 blank | 16 comment | 13 complexity | a96a80c984495b6b719683c5afb27702 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.upload.web.internal;
  15. import com.liferay.document.library.configuration.DLConfiguration;
  16. import com.liferay.document.library.exception.DLStorageQuotaExceededException;
  17. import com.liferay.document.library.kernel.antivirus.AntivirusScannerException;
  18. import com.liferay.document.library.kernel.exception.DuplicateFileEntryException;
  19. import com.liferay.document.library.kernel.exception.FileExtensionException;
  20. import com.liferay.document.library.kernel.exception.FileNameException;
  21. import com.liferay.document.library.kernel.exception.FileSizeException;
  22. import com.liferay.document.library.kernel.util.DLValidator;
  23. import com.liferay.petra.string.StringPool;
  24. import com.liferay.portal.configuration.metatype.bnd.util.ConfigurableUtil;
  25. import com.liferay.portal.kernel.exception.PortalException;
  26. import com.liferay.portal.kernel.json.JSONFactoryUtil;
  27. import com.liferay.portal.kernel.json.JSONObject;
  28. import com.liferay.portal.kernel.json.JSONUtil;
  29. import com.liferay.portal.kernel.language.Language;
  30. import com.liferay.portal.kernel.repository.model.FileEntry;
  31. import com.liferay.portal.kernel.servlet.ServletResponseConstants;
  32. import com.liferay.portal.kernel.theme.ThemeDisplay;
  33. import com.liferay.portal.kernel.upload.UploadPortletRequest;
  34. import com.liferay.portal.kernel.upload.UploadRequestSizeException;
  35. import com.liferay.portal.kernel.util.StringUtil;
  36. import com.liferay.portal.kernel.util.WebKeys;
  37. import com.liferay.portal.util.PropsValues;
  38. import com.liferay.upload.UploadResponseHandler;
  39. import java.util.Map;
  40. import javax.portlet.PortletRequest;
  41. import org.osgi.service.component.annotations.Activate;
  42. import org.osgi.service.component.annotations.Component;
  43. import org.osgi.service.component.annotations.Modified;
  44. import org.osgi.service.component.annotations.Reference;
  45. /**
  46. * @author Sergio González
  47. */
  48. @Component(
  49. configurationPid = "com.liferay.document.library.configuration.DLConfiguration",
  50. property = "upload.response.handler=multiple",
  51. service = UploadResponseHandler.class
  52. )
  53. public class MultipleUploadResponseHandler implements UploadResponseHandler {
  54. @Override
  55. public JSONObject onFailure(
  56. PortletRequest portletRequest, PortalException portalException)
  57. throws PortalException {
  58. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  59. if (portalException instanceof AntivirusScannerException ||
  60. portalException instanceof DLStorageQuotaExceededException ||
  61. portalException instanceof DuplicateFileEntryException ||
  62. portalException instanceof FileExtensionException ||
  63. portalException instanceof FileNameException ||
  64. portalException instanceof FileSizeException ||
  65. portalException instanceof UploadRequestSizeException) {
  66. String errorMessage = StringPool.BLANK;
  67. int errorType = 0;
  68. ThemeDisplay themeDisplay =
  69. (ThemeDisplay)portletRequest.getAttribute(
  70. WebKeys.THEME_DISPLAY);
  71. if (portalException instanceof AntivirusScannerException) {
  72. AntivirusScannerException antivirusScannerException =
  73. (AntivirusScannerException)portalException;
  74. errorMessage = themeDisplay.translate(
  75. antivirusScannerException.getMessageKey());
  76. errorType =
  77. ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
  78. }
  79. if (portalException instanceof DLStorageQuotaExceededException) {
  80. errorMessage = themeDisplay.translate(
  81. "you-have-exceeded-the-x-storage-quota-for-this-instance",
  82. _language.formatStorageSize(
  83. PropsValues.DATA_LIMIT_DL_STORAGE_MAX_SIZE,
  84. themeDisplay.getLocale()));
  85. errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
  86. }
  87. else if (portalException instanceof DuplicateFileEntryException) {
  88. errorMessage = themeDisplay.translate(
  89. "please-enter-a-unique-document-name");
  90. errorType =
  91. ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
  92. }
  93. else if (portalException instanceof FileExtensionException) {
  94. errorMessage = themeDisplay.translate(
  95. "please-enter-a-file-with-a-valid-extension-x",
  96. _getAllowedFileExtensions());
  97. errorType =
  98. ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
  99. }
  100. else if (portalException instanceof FileNameException) {
  101. errorMessage = themeDisplay.translate(
  102. "please-enter-a-file-with-a-valid-file-name");
  103. }
  104. else if (portalException instanceof FileSizeException) {
  105. errorMessage = themeDisplay.translate(
  106. "please-enter-a-file-with-a-valid-file-size-no-larger-" +
  107. "than-x",
  108. _language.formatStorageSize(
  109. _dlValidator.getMaxAllowableSize(),
  110. themeDisplay.getLocale()));
  111. }
  112. else if (portalException instanceof UploadRequestSizeException) {
  113. errorType =
  114. ServletResponseConstants.SC_UPLOAD_REQUEST_SIZE_EXCEPTION;
  115. }
  116. jsonObject.put(
  117. "message", errorMessage
  118. ).put(
  119. "status", errorType
  120. );
  121. }
  122. return jsonObject;
  123. }
  124. @Override
  125. public JSONObject onSuccess(
  126. UploadPortletRequest uploadPortletRequest, FileEntry fileEntry)
  127. throws PortalException {
  128. return JSONUtil.put(
  129. "groupId", fileEntry.getGroupId()
  130. ).put(
  131. "name", fileEntry.getTitle()
  132. ).put(
  133. "title", uploadPortletRequest.getFileName("file")
  134. ).put(
  135. "uuid", fileEntry.getUuid()
  136. );
  137. }
  138. @Activate
  139. @Modified
  140. protected void activate(Map<String, Object> properties) {
  141. _dlConfiguration = ConfigurableUtil.createConfigurable(
  142. DLConfiguration.class, properties);
  143. }
  144. private String _getAllowedFileExtensions() {
  145. String[] allowedFileExtensions = _dlConfiguration.fileExtensions();
  146. return StringUtil.merge(
  147. allowedFileExtensions, StringPool.COMMA_AND_SPACE);
  148. }
  149. private volatile DLConfiguration _dlConfiguration;
  150. @Reference
  151. private DLValidator _dlValidator;
  152. @Reference
  153. private Language _language;
  154. }