Java tutorial
/** * 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.blogs.web.internal.upload; import com.liferay.blogs.kernel.exception.EntryImageNameException; import com.liferay.blogs.kernel.exception.EntryImageSizeException; 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.security.auth.PrincipalException; import com.liferay.portal.kernel.security.permission.ActionKeys; import com.liferay.portal.kernel.security.permission.PermissionChecker; import com.liferay.portal.kernel.security.permission.ResourcePermissionCheckerUtil; import com.liferay.portal.kernel.servlet.ServletResponseConstants; import com.liferay.portal.kernel.upload.BaseUploadHandler; import com.liferay.portal.kernel.util.FileUtil; 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.util.PrefsPropsUtil; import com.liferay.portal.util.PropsValues; import com.liferay.portlet.blogs.service.permission.BlogsPermission; import javax.portlet.PortletRequest; import javax.portlet.PortletResponse; /** * @author Sergio Gonzlez * @author Adolfo Prez */ public abstract class BaseBlogsUploadHandler extends BaseUploadHandler { @Override public void validateFile(String fileName, String contentType, long size) throws PortalException { long maxSize = getMaxFileSize(); if ((maxSize > 0) && (size > maxSize)) { throw new EntryImageSizeException(); } String extension = FileUtil.getExtension(fileName); String[] imageExtensions = PrefsPropsUtil.getStringArray(PropsKeys.BLOGS_IMAGE_EXTENSIONS, StringPool.COMMA); for (String imageExtension : imageExtensions) { if (StringPool.STAR.equals(imageExtension) || imageExtension.equals(StringPool.PERIOD + extension)) { return; } } throw new EntryImageNameException("Invalid image for file name " + fileName); } @Override protected void checkPermission(long groupId, long folderId, PermissionChecker permissionChecker) throws PortalException { boolean containsResourcePermission = ResourcePermissionCheckerUtil.containsResourcePermission( permissionChecker, BlogsPermission.RESOURCE_NAME, groupId, ActionKeys.ADD_ENTRY); if (!containsResourcePermission) { throw new PrincipalException.MustHavePermission(permissionChecker, BlogsPermission.RESOURCE_NAME, groupId, ActionKeys.ADD_ENTRY); } } @Override protected void doHandleUploadException(PortletRequest portletRequest, PortletResponse portletResponse, PortalException pe, JSONObject jsonObject) throws PortalException { if (pe instanceof EntryImageNameException || pe instanceof EntryImageSizeException) { String errorMessage = StringPool.BLANK; int errorType = 0; if (pe instanceof EntryImageNameException) { errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION; String[] imageExtensions = PrefsPropsUtil.getStringArray(PropsKeys.BLOGS_IMAGE_EXTENSIONS, StringPool.COMMA); errorMessage = StringUtil.merge(imageExtensions); } else if (pe instanceof EntryImageSizeException) { errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION; } JSONObject errorJSONObject = JSONFactoryUtil.createJSONObject(); errorJSONObject.put("errorType", errorType); errorJSONObject.put("message", errorMessage); jsonObject.put("error", errorJSONObject); } else { throw pe; } } protected long getMaxFileSize() { return PropsValues.BLOGS_IMAGE_MAX_SIZE; } @Override protected String getParameterName() { return "imageSelectorFileName"; } }