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.portlet.blogs.image.selector;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.image.selector.BaseImageSelectorUploadHandler;
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.servlet.ServletResponseConstants;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.WebKeys;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.security.permission.ResourcePermissionCheckerUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PrefsPropsUtil;
034    import com.liferay.portlet.blogs.CoverImageNameException;
035    import com.liferay.portlet.blogs.CoverImageSizeException;
036    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
037    import com.liferay.portlet.documentlibrary.FileNameException;
038    import com.liferay.portlet.documentlibrary.antivirus.AntivirusScannerException;
039    
040    import java.io.IOException;
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 BaseBlogsImageSelectorUploadHandler
050            extends BaseImageSelectorUploadHandler {
051    
052            @Override
053            public void checkPermission(
054                            long groupId, PermissionChecker permissionChecker)
055                    throws PortalException {
056    
057                    boolean containsResourcePermission =
058                            ResourcePermissionCheckerUtil.containsResourcePermission(
059                                    permissionChecker, BlogsPermission.RESOURCE_NAME, groupId,
060                                    ActionKeys.ADD_ENTRY);
061    
062                    if (!containsResourcePermission) {
063                            throw new PrincipalException();
064                    }
065            }
066    
067            @Override
068            public void validateFile(String fileName, String contentType, long size)
069                    throws PortalException {
070    
071                    String extension = FileUtil.getExtension(fileName);
072    
073                    String[] imageExtensions = PrefsPropsUtil.getStringArray(
074                            PropsKeys.BLOGS_IMAGE_EXTENSIONS, StringPool.COMMA);
075    
076                    for (String imageExtension : imageExtensions) {
077                            if (StringPool.STAR.equals(imageExtension) ||
078                                    imageExtension.equals(StringPool.PERIOD + extension)) {
079    
080                                    return;
081                            }
082                    }
083    
084                    throw new CoverImageNameException(
085                            "Invalid cover image for file name " + fileName);
086            }
087    
088            protected abstract long getMaxFileSize();
089    
090            @Override
091            protected void handleUploadException(
092                            PortletRequest portletRequest, PortletResponse portletResponse,
093                            PortalException pe, JSONObject jsonObject)
094                    throws PortalException {
095    
096                    jsonObject.put("success", Boolean.FALSE);
097    
098                    if (pe instanceof AntivirusScannerException ||
099                            pe instanceof CoverImageNameException ||
100                            pe instanceof CoverImageSizeException ||
101                            pe instanceof FileNameException) {
102    
103                            String errorMessage = StringPool.BLANK;
104                            int errorType = 0;
105    
106                            ThemeDisplay themeDisplay =
107                                    (ThemeDisplay)portletRequest.getAttribute(
108                                            WebKeys.THEME_DISPLAY);
109    
110                            if (pe instanceof AntivirusScannerException) {
111                                    errorType =
112                                            ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
113                                    AntivirusScannerException ase = (AntivirusScannerException)pe;
114    
115                                    errorMessage = themeDisplay.translate(ase.getMessageKey());
116                            }
117                            else if (pe instanceof CoverImageNameException) {
118                                    errorType =
119                                            ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
120                            }
121                            else if (pe instanceof CoverImageSizeException) {
122                                    errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
123                            }
124                            else if (pe instanceof FileNameException) {
125                                    errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
126                            }
127    
128                            JSONObject errorJSONObject = JSONFactoryUtil.createJSONObject();
129    
130                            errorJSONObject.put("errorType", errorType);
131                            errorJSONObject.put("message", errorMessage);
132    
133                            jsonObject.put("error", errorJSONObject);
134    
135                            try {
136                                    JSONPortletResponseUtil.writeJSON(
137                                            portletRequest, portletResponse, jsonObject);
138                            }
139                            catch (IOException ioe) {
140                                    throw new SystemException(ioe);
141                            }
142                    }
143                    else {
144                            throw pe;
145                    }
146            }
147    
148    }