001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.image.selector;
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.upload.LiferayFileItemException;
025    import com.liferay.portal.kernel.upload.UploadException;
026    import com.liferay.portal.kernel.upload.UploadPortletRequest;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.TempFileEntryUtil;
032    import com.liferay.portal.kernel.util.WebKeys;
033    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
034    import com.liferay.portal.security.permission.PermissionChecker;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.documentlibrary.FileSizeException;
038    
039    import java.io.IOException;
040    import java.io.InputStream;
041    
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PortletResponse;
044    
045    /**
046     * @author Sergio Gonz??lez
047     * @author Adolfo P??rez
048     */
049    public abstract class BaseImageSelectorUploadHandler
050            implements ImageSelectorUploadHandler {
051    
052            @Override
053            public void uploadSelectedImage(
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(),
065                            themeDisplay.getPermissionChecker());
066    
067                    UploadException uploadException =
068                            (UploadException)portletRequest.getAttribute(
069                                    WebKeys.UPLOAD_EXCEPTION);
070    
071                    if (uploadException != null) {
072                            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
073                                    throw new LiferayFileItemException();
074                            }
075                            else if (uploadException.isExceededSizeLimit()) {
076                                    throw new FileSizeException(uploadException.getCause());
077                            }
078    
079                            throw new PortalException(uploadException.getCause());
080                    }
081    
082                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
083    
084                    String randomId = ParamUtil.getString(uploadPortletRequest, "randomId");
085    
086                    try {
087                            JSONObject imageJSONObject = getImageJSONObject(portletRequest);
088    
089                            jsonObject.put("success", Boolean.TRUE);
090    
091                            imageJSONObject.put("randomId", randomId);
092    
093                            jsonObject.put("image", imageJSONObject);
094    
095                            JSONPortletResponseUtil.writeJSON(
096                                    portletRequest, portletResponse, jsonObject);
097                    }
098                    catch (IOException ioe) {
099                            throw new SystemException(ioe);
100                    }
101                    catch (PortalException pe) {
102                            handleUploadException(
103                                    portletRequest, portletResponse, pe, jsonObject);
104                    }
105            }
106    
107            protected abstract void checkPermission(
108                            long groupId, PermissionChecker permissionChecker)
109                    throws PortalException;
110    
111            protected JSONObject getImageJSONObject(PortletRequest portletRequest)
112                    throws PortalException {
113    
114                    UploadPortletRequest uploadPortletRequest =
115                            PortalUtil.getUploadPortletRequest(portletRequest);
116    
117                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    JSONObject imageJSONObject = JSONFactoryUtil.createJSONObject();
121    
122                    InputStream inputStream = null;
123    
124                    try {
125                            imageJSONObject.put(
126                                    "attributeDataImageId",
127                                    EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
128    
129                            String fileName = uploadPortletRequest.getFileName(
130                                    "imageSelectorFileName");
131                            String contentType = uploadPortletRequest.getContentType(
132                                    "imageSelectorFileName");
133                            long size = uploadPortletRequest.getSize("imageSelectorFileName");
134    
135                            validateFile(fileName, contentType, size);
136    
137                            inputStream = uploadPortletRequest.getFileAsStream(
138                                    "imageSelectorFileName");
139    
140                            FileEntry fileEntry = TempFileEntryUtil.addTempFileEntry(
141                                    themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
142                                    _TEMP_FOLDER_NAME, StringUtil.randomString() + fileName,
143                                    inputStream, contentType);
144    
145                            imageJSONObject.put("fileEntryId", fileEntry.getFileEntryId());
146    
147                            imageJSONObject.put(
148                                    "url",
149                                    PortletFileRepositoryUtil.getPortletFileEntryURL(
150                                            themeDisplay, fileEntry, StringPool.BLANK));
151    
152                            return imageJSONObject;
153                    }
154                    catch (IOException ioe) {
155                            throw new SystemException(ioe);
156                    }
157                    finally {
158                            StreamUtil.cleanUp(inputStream);
159                    }
160            }
161    
162            protected abstract void handleUploadException(
163                            PortletRequest portletRequest, PortletResponse portletResponse,
164                            PortalException pe, JSONObject jsonObject)
165                    throws PortalException;
166    
167            protected abstract void validateFile(
168                            String fileName, String contentType, long size)
169                    throws PortalException;
170    
171            private static final String _TEMP_FOLDER_NAME =
172                    BaseImageSelectorUploadHandler.class.getName();
173    
174    }