001
014
015 package com.liferay.portal.kernel.upload;
016
017 import com.liferay.portal.kernel.editor.EditorConstants;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil;
023 import com.liferay.portal.kernel.repository.model.FileEntry;
024 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
025 import com.liferay.portal.kernel.util.FileUtil;
026 import com.liferay.portal.kernel.util.ParamUtil;
027 import com.liferay.portal.kernel.util.StreamUtil;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.WebKeys;
030 import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
031 import com.liferay.portal.security.permission.PermissionChecker;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.theme.ThemeDisplay;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portlet.documentlibrary.FileNameException;
036 import com.liferay.portlet.documentlibrary.FileSizeException;
037 import com.liferay.portlet.documentlibrary.antivirus.AntivirusScannerException;
038
039 import java.io.IOException;
040 import java.io.InputStream;
041
042 import javax.portlet.PortletRequest;
043 import javax.portlet.PortletResponse;
044
045
050 public abstract class BaseUploadHandler implements UploadHandler {
051
052 @Override
053 public void upload(
054 PortletRequest portletRequest, PortletResponse portletResponse)
055 throws PortalException {
056
057 UploadPortletRequest uploadPortletRequest =
058 PortalUtil.getUploadPortletRequest(portletRequest);
059
060 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
061 WebKeys.THEME_DISPLAY);
062
063 checkPermission(
064 themeDisplay.getScopeGroupId(), getFolderId(uploadPortletRequest),
065 themeDisplay.getPermissionChecker());
066
067 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
068
069 try {
070 UploadException uploadException =
071 (UploadException)portletRequest.getAttribute(
072 WebKeys.UPLOAD_EXCEPTION);
073
074 if (uploadException != null) {
075 Throwable cause = uploadException.getCause();
076
077 if (uploadException.isExceededFileSizeLimit()) {
078 throw new FileSizeException(cause);
079 }
080
081 if (uploadException.isExceededLiferayFileItemSizeLimit()) {
082 throw new LiferayFileItemException(cause);
083 }
084
085 if (uploadException.isExceededUploadRequestSizeLimit()) {
086 throw new UploadRequestSizeException(cause);
087 }
088
089 throw new PortalException(cause);
090 }
091
092 JSONObject imageJSONObject = getImageJSONObject(portletRequest);
093
094 String randomId = ParamUtil.getString(
095 uploadPortletRequest, "randomId");
096
097 imageJSONObject.put("randomId", randomId);
098
099 jsonObject.put("file", imageJSONObject);
100
101 jsonObject.put("success", Boolean.TRUE);
102
103 JSONPortletResponseUtil.writeJSON(
104 portletRequest, portletResponse, jsonObject);
105 }
106 catch (IOException ioe) {
107 throw new SystemException(ioe);
108 }
109 catch (PortalException pe) {
110 handleUploadException(
111 portletRequest, portletResponse, pe, jsonObject);
112 }
113 }
114
115 protected abstract FileEntry addFileEntry(
116 long userId, long groupId, long folderId, String fileName,
117 String contentType, InputStream inputStream, long size,
118 ServiceContext serviceContext)
119 throws PortalException;
120
121 protected abstract void checkPermission(
122 long groupId, long folderId, PermissionChecker permissionChecker)
123 throws PortalException;
124
125 protected void doHandleUploadException(
126 PortletRequest portletRequest, PortletResponse portletResponse,
127 PortalException pe, JSONObject jsonObject)
128 throws PortalException {
129
130 throw pe;
131 }
132
133 protected abstract FileEntry fetchFileEntry(
134 long userId, long groupId, long folderId, String fileName)
135 throws PortalException;
136
137 protected long getFolderId(UploadPortletRequest uploadPortletRequest) {
138 return 0;
139 }
140
141 protected JSONObject getImageJSONObject(PortletRequest portletRequest)
142 throws PortalException {
143
144 UploadPortletRequest uploadPortletRequest =
145 PortalUtil.getUploadPortletRequest(portletRequest);
146
147 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
148 WebKeys.THEME_DISPLAY);
149
150 JSONObject imageJSONObject = JSONFactoryUtil.createJSONObject();
151
152 InputStream inputStream = null;
153
154 try {
155 imageJSONObject.put(
156 "attributeDataImageId",
157 EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
158
159 String parameterName = getParameterName();
160
161 String fileName = uploadPortletRequest.getFileName(parameterName);
162 String contentType = uploadPortletRequest.getContentType(
163 parameterName);
164 long size = uploadPortletRequest.getSize(parameterName);
165
166 validateFile(fileName, contentType, size);
167
168 long folderId = getFolderId(uploadPortletRequest);
169
170 String uniqueFileName = getUniqueFileName(
171 themeDisplay, fileName, folderId);
172
173 inputStream = uploadPortletRequest.getFileAsStream(parameterName);
174
175 FileEntry fileEntry = addFileEntry(
176 themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
177 folderId, uniqueFileName, contentType, inputStream, size,
178 getServiceContext(uploadPortletRequest));
179
180 imageJSONObject.put("fileEntryId", fileEntry.getFileEntryId());
181 imageJSONObject.put("groupId", fileEntry.getGroupId());
182 imageJSONObject.put("title", fileEntry.getTitle());
183 imageJSONObject.put("url", getURL(fileEntry, themeDisplay));
184 imageJSONObject.put("uuid", fileEntry.getUuid());
185
186 return imageJSONObject;
187 }
188 catch (IOException ioe) {
189 throw new SystemException(ioe);
190 }
191 finally {
192 StreamUtil.cleanUp(inputStream);
193 }
194 }
195
196 protected abstract String getParameterName();
197
198
201 protected ServiceContext getServiceContext(
202 UploadPortletRequest uploadPortletRequest)
203 throws PortalException {
204
205 return null;
206 }
207
208 protected String getUniqueFileName(
209 ThemeDisplay themeDisplay, String fileName, long folderId)
210 throws PortalException {
211
212 FileEntry fileEntry = fetchFileEntry(
213 themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), folderId,
214 fileName);
215
216 if (fileEntry == null) {
217 return fileName;
218 }
219
220 int suffix = 1;
221
222 for (int i = 0; i < _UNIQUE_FILE_NAME_TRIES; i++) {
223 String curFileName = FileUtil.appendParentheticalSuffix(
224 fileName, String.valueOf(suffix));
225
226 fileEntry = fetchFileEntry(
227 themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
228 folderId, curFileName);
229
230 if (fileEntry == null) {
231 return curFileName;
232 }
233
234 suffix++;
235 }
236
237 throw new PortalException(
238 "Unable to get a unique file name for " + fileName);
239 }
240
241 protected String getURL(FileEntry fileEntry, ThemeDisplay themeDisplay) {
242 return PortletFileRepositoryUtil.getPortletFileEntryURL(
243 themeDisplay, fileEntry, StringPool.BLANK);
244 }
245
246 protected void handleUploadException(
247 PortletRequest portletRequest, PortletResponse portletResponse,
248 PortalException pe, JSONObject jsonObject)
249 throws PortalException {
250
251 jsonObject.put("success", Boolean.FALSE);
252
253 if (pe instanceof AntivirusScannerException ||
254 pe instanceof FileNameException ||
255 pe instanceof FileSizeException ||
256 pe instanceof UploadRequestSizeException) {
257
258 String errorMessage = StringPool.BLANK;
259 int errorType = 0;
260
261 ThemeDisplay themeDisplay =
262 (ThemeDisplay)portletRequest.getAttribute(
263 WebKeys.THEME_DISPLAY);
264
265 if (pe instanceof AntivirusScannerException) {
266 errorType =
267 ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
268 AntivirusScannerException ase = (AntivirusScannerException)pe;
269
270 errorMessage = themeDisplay.translate(ase.getMessageKey());
271 }
272 else if (pe instanceof FileNameException) {
273 errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
274 }
275 else if (pe instanceof FileSizeException) {
276 errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
277 }
278 else if (pe instanceof UploadRequestSizeException) {
279 errorType =
280 ServletResponseConstants.SC_UPLOAD_REQUEST_SIZE_EXCEPTION;
281 }
282
283 JSONObject errorJSONObject = JSONFactoryUtil.createJSONObject();
284
285 errorJSONObject.put("errorType", errorType);
286 errorJSONObject.put("message", errorMessage);
287
288 jsonObject.put("error", errorJSONObject);
289 }
290 else {
291 doHandleUploadException(
292 portletRequest, portletResponse, pe, jsonObject);
293 }
294
295 try {
296 JSONPortletResponseUtil.writeJSON(
297 portletRequest, portletResponse, jsonObject);
298 }
299 catch (IOException ioe) {
300 throw new SystemException(ioe);
301 }
302 }
303
304 protected abstract void validateFile(
305 String fileName, String contentType, long size)
306 throws PortalException;
307
308 protected static final String TEMP_FOLDER_NAME =
309 BaseUploadHandler.class.getName();
310
311 private static final int _UNIQUE_FILE_NAME_TRIES = 50;
312
313 }