Example usage for com.liferay.portal.kernel.exception PortalException PortalException

List of usage examples for com.liferay.portal.kernel.exception PortalException PortalException

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.exception PortalException PortalException.

Prototype

public PortalException(String msg, Throwable cause) 

Source Link

Usage

From source file:au.com.permeance.liferay.portlet.documentlibrary.action.DownloadFolderZipAction.java

License:Open Source License

protected void downloadFolder(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
    long groupId = themeDisplay.getScopeGroupId();
    long repositoryId = ParamUtil.getLong(resourceRequest, "repositoryId");
    long folderId = ParamUtil.getLong(resourceRequest, "folderId");
    Folder folder = DLAppServiceUtil.getFolder(folderId);
    ServiceContext serviceContext = ServiceContextFactory.getInstance(Folder.class.getName(), resourceRequest);

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    if (!folder.containsPermission(permissionChecker, ActionKeys.VIEW)) {
        throw new PrincipalException();
    }//  www.  j  a  va 2 s. com

    File tempZipFile = null;

    try {

        tempZipFile = createTempZipFile();
        DLFolderExportZipServiceUtil.exportFolderToZipFile(groupId, repositoryId, folderId, serviceContext,
                tempZipFile);
        String downloadZipFileName = folder.getName() + ZIP_FILE_EXT;
        sendZipFile(resourceRequest, resourceResponse, tempZipFile, downloadZipFileName);

    } catch (Exception e) {

        String msg = "Error downloading folder " + folderId + " from repository " + repositoryId + " : "
                + e.getMessage();

        s_log.error(msg, e);

        throw new PortalException(msg, e);

    } finally {

        safeDeleteFile(tempZipFile);
        tempZipFile = null;

    }
}

From source file:au.com.permeance.liferay.portlet.documentlibrary.action.DownloadFolderZipAction.java

License:Open Source License

protected void sendZipFile(ResourceRequest resourceRequest, ResourceResponse resourceResponse, File zipFile,
        String zipFileName) throws Exception {

    FileInputStream zipFileInputStream = null;

    try {//from   w  ww . ja v  a  2 s  .  c  o  m

        if (StringUtils.isEmpty(zipFileName)) {
            zipFileName = FilenameUtils.getBaseName(zipFile.getName());
        }

        String zipFileMimeType = MimeTypesUtil.getContentType(zipFileName);
        resourceResponse.setContentType(zipFileMimeType);

        int folderDownloadCacheMaxAge = DownloadFolderZipPropsValues.DL_FOLDER_DOWNLOAD_CACHE_MAX_AGE;
        if (folderDownloadCacheMaxAge > 0) {
            String cacheControlValue = "max-age=" + folderDownloadCacheMaxAge + ", must-revalidate";
            resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, cacheControlValue);
        }

        String contentDispositionValue = "attachment; filename=\"" + zipFileName + "\"";
        resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, contentDispositionValue);

        // NOTE: java.io.File may return a length of 0 (zero) for a valid file
        // @see java.io.File#length()
        long zipFileLength = zipFile.length();
        if ((zipFileLength > 0L) && (zipFileLength < (long) Integer.MAX_VALUE)) {
            resourceResponse.setContentLength((int) zipFileLength);
        }

        zipFileInputStream = new FileInputStream(zipFile);
        OutputStream responseOutputStream = resourceResponse.getPortletOutputStream();
        long responseByteCount = IOUtils.copy(zipFileInputStream, responseOutputStream);
        responseOutputStream.flush();
        responseOutputStream.close();
        zipFileInputStream.close();
        zipFileInputStream = null;

        if (s_log.isDebugEnabled()) {
            s_log.debug("sent " + responseByteCount + " byte(s) for ZIP file " + zipFileName);
        }

    } catch (Exception e) {

        String name = StringPool.BLANK;
        if (zipFile != null) {
            name = zipFile.getName();
        }

        String msg = "Error sending ZIP file " + name + " : " + e.getMessage();
        s_log.error(msg);
        throw new PortalException(msg, e);

    } finally {

        if (zipFileInputStream != null) {
            try {
                zipFileInputStream.close();
                zipFileInputStream = null;
            } catch (Exception e) {
                String msg = "Error closing ZIP input stream : " + e.getMessage();
                s_log.error(msg);
            }
        }

    }
}

From source file:au.com.permeance.liferay.portlet.documentlibrary.action.FolderInfoAction.java

License:Open Source License

protected DLFolderInfo buildFolderInfo(RenderRequest renderRequest, RenderResponse renderResponse)
        throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
    long groupId = ParamUtil.getLong(renderRequest, "groupId");
    long scopeGroupId = themeDisplay.getScopeGroupId();
    String scopeGroupName = themeDisplay.getScopeGroupName();
    long repositoryId = ParamUtil.getLong(renderRequest, "repositoryId");
    long folderId = ParamUtil.getLong(renderRequest, "folderId");
    Folder folder = DLAppServiceUtil.getFolder(folderId);
    ServiceContext serviceContext = ServiceContextFactory.getInstance(Folder.class.getName(), renderRequest);

    if (s_log.isDebugEnabled()) {
        s_log.debug("building folder info ...");
        ParamUtil.print(renderRequest);//  ww  w  . j  a va 2 s.co  m
        s_log.debug("groupId: " + groupId);
        s_log.debug("scopeGroupId: " + scopeGroupId);
        s_log.debug("scopeGroupName: " + scopeGroupName);
        s_log.debug("repositoryId: " + repositoryId);
        s_log.debug("folderId: " + folderId);
    }

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    if (!folder.containsPermission(permissionChecker, ActionKeys.VIEW)) {
        throw new PrincipalException();
    }

    DLFolderInfo folderInfo = new DLFolderInfo();

    try {

        User user = UserServiceUtil.getUserById(folder.getUserId());

        DLFolderUsage folderUsage = DLFolderUsageServiceUtil.calculateFolderUsage(repositoryId, folderId,
                serviceContext);

        folderInfo.setFolderId(folderId);
        folderInfo.setFolderName(folder.getName());
        folderInfo.setFolderCreateDate(folder.getCreateDate());
        folderInfo.setFolderDescription(folder.getDescription());
        folderInfo.setFolderPath(buildPath(folder));
        folderInfo.setFolderUsage(folderUsage);
        folderInfo.setFolderUserId(folder.getUserId());

        if (user != null) {
            folderInfo.setFolderUserFullName(user.getFullName());
        }

        folderInfo.setRepositoryId(repositoryId);

        Repository repository = null;
        if (repositoryId != scopeGroupId) {
            repository = RepositoryServiceUtil.getRepository(repositoryId);
            if (repository != null) {
                folderInfo.setRepositoryName(repository.getName());
                folderInfo.setRepositoryClassName(repository.getClassName());
                folderInfo.setRepositoryDescription(repository.getDescription());
            }
        }

    } catch (Exception e) {

        String msg = "Error building information for folder " + folderId + " in repository " + repositoryId
                + " : " + e.getMessage();

        s_log.error(msg, e);

        throw new PortalException(msg, e);

    } finally {

        // placeholder

    }

    return folderInfo;
}

From source file:au.com.permeance.liferay.portlet.documentlibrary.service.impl.DLFolderExportZipHelper.java

License:Open Source License

/**
 * Export folder to ZIP writer.//  www. j a  v  a2 s.c om
 * 
 * @param groupId group id
 * @param repositoryId source repository containing folder to export
 * @param folder source folder to export
 * @param folderPath source folder path to export
 * @param serviceContext service context
 * @param zipWriter destination ZIP writer
 * 
 * @throws PortalException
 * @throws SystemException
 * @throws RemoteException
 */
public static void exportFolderToZipWriter(long groupId, long repositoryId, Folder folder, String folderPath,
        ServiceContext serviceContext, ZipWriter zipWriter) throws PortalException, SystemException {

    // Export file entries in folder to ZIP writer

    List<FileEntry> fileEntryList = DLAppServiceUtil.getFileEntries(folder.getRepositoryId(),
            folder.getFolderId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);

    for (FileEntry fileEntry : fileEntryList) {
        try {
            exportFileEntryToZipWriter(fileEntry, folderPath, zipWriter);
        } catch (Exception e) {
            String msg = "Error exporting file entry to ZIP file : " + e.getMessage();
            s_log.error(msg, e);
            throw new PortalException(msg, e);
        }
    }

    // Export sub-folders in folder to ZIP writer

    List<Folder> subFolderList = DLAppServiceUtil.getFolders(folder.getRepositoryId(), folder.getFolderId(),
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);

    for (Folder subFolder : subFolderList) {
        String subFolderName = subFolder.getName();
        String subFolderPath = folderPath + subFolderName + StringPool.FORWARD_SLASH;
        exportFolderToZipWriter(groupId, repositoryId, subFolder, subFolderPath, serviceContext, zipWriter);
    }
}

From source file:au.com.permeance.liferay.portlet.documentlibrary.service.impl.DLFolderExportZipHelper.java

License:Open Source License

/**
 * Adds a file entry as a ZIP entry.//ww  w.  j  a  v  a 2 s  .  c o m
 *
 * @param fileEntry file entry
 * @param folderPath folder path
 * @param zipWriter ZIP writer
 * 
 * @throws PortalException
 * @throws SystemException
 * @throws IOException
 */
public static void exportFileEntryToZipWriter(FileEntry fileEntry, String folderPath, ZipWriter zipWriter)
        throws PortalException, SystemException, IOException {

    InputStream fileInputStream = null;
    try {

        String zipEntryName = buildZipEntryName(fileEntry, folderPath, zipWriter);
        fileInputStream = fileEntry.getContentStream();
        zipWriter.addEntry(zipEntryName, fileInputStream);

    } catch (Exception e) {

        String zipWriterLabel = zipWriter.getPath();

        String msg = "Error exporting file entry " + fileEntry + " to ZIP writer " + zipWriterLabel + " : "
                + e.getMessage();

        s_log.error(msg, e);

        if (e instanceof PortalException) {
            throw (PortalException) e;

        } else if (e instanceof SystemException) {
            throw (SystemException) e;

        } else if (e instanceof IOException) {
            throw (IOException) e;

        } else {
            throw new PortalException(msg, e);
        }

    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
                fileInputStream = null;
            }
        } catch (Exception e) {
        }
    }
}

From source file:au.com.permeance.liferay.portlet.documentlibrary.service.impl.DLFolderUsageHelper.java

License:Open Source License

/**
 * Visit Folder./*from   w  ww. ja v  a2  s .c  o m*/
 * 
 * @param repositoryId repository containing folder
 * @param folder folder
 * @param folderPath folder path
 * @param serviceContext service context
 * @param folderUsageCollector folder usage collector
 * 
 * @throws PortalException 
 * @throws SystemException
 */
public static void calculateFolderUsage(long repositoryId, Folder folder, String folderPath,
        ServiceContext serviceContext, DLFolderUsageCollector folderUsageCollector)
        throws PortalException, SystemException {

    // Visit file entries in folder

    if (s_log.isDebugEnabled()) {
        s_log.debug("calculating usage for folder " + folder.getFolderId() + "/" + folder.getName());
    }

    List<FileEntry> fileEntryList = DLAppServiceUtil.getFileEntries(folder.getRepositoryId(),
            folder.getFolderId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);

    for (FileEntry fileEntry : fileEntryList) {
        try {
            calculateFileEntryUsage(fileEntry, folderPath, folderUsageCollector);
            folderUsageCollector.incrementFileCount();
        } catch (Exception e) {
            String msg = "Error visiting file entry " + fileEntry.getFileEntryId() + " : " + e.getMessage();
            s_log.error(msg, e);
            throw new PortalException(msg, e);
        }
    }

    // Visit sub-folders

    List<Folder> subFolderList = DLAppServiceUtil.getFolders(folder.getRepositoryId(), folder.getFolderId(),
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);

    for (Folder subFolder : subFolderList) {
        String subFolderName = subFolder.getName();
        String subFolderPath = folderPath + subFolderName + StringPool.FORWARD_SLASH;
        calculateFolderUsage(repositoryId, subFolder, subFolderPath, serviceContext, folderUsageCollector);
        folderUsageCollector.incrementFolderCount();
    }
}

From source file:com.beorn.paymentappapi.util.PaymentAppUtil.java

License:Open Source License

/**
 * Retrieve the notification listener as defined in this app's
 * payment-app.xml/*www  .  j  a  v a2  s. com*/
 * 
 * @param servletContext
 *            this webapp's servlet context
 * @return the notification listener
 * @throws PortalException
 * @throws SystemException
 */
public static NotificationListener getNotificationListener(ServletContext servletContext)
        throws PortalException, SystemException {

    try {
        InputStream is = getAppDescriptionStream(servletContext);

        if (is == null)
            return null;

        try {
            Document document = SAXReaderUtil.read(is);

            Node notificationListenerClassNode = document
                    .selectSingleNode("/plugin/notification-listener-class");
            if (notificationListenerClassNode == null)
                return null;

            String className = notificationListenerClassNode.getStringValue();

            try {
                NotificationListener notificationListener = (NotificationListener) Class.forName(className)
                        .newInstance();

                return notificationListener;

            } catch (Exception e) {
                throw new PortalException("Could not initialize notification listener for {app:"
                        + servletContext.getServletContextName() + ", classname:" + className + "}", e);
            }

        } catch (Exception e) {
            throw new PortalException(e);

        } finally {
            is.close();
        }

    } catch (IOException e1) {
        throw new SystemException(e1);
    }
}

From source file:com.beorn.paymentpluginapi.util.PaymentPluginUtil.java

License:Open Source License

/**
 * Retrieve the config validator as defined in this plugin's
 * payment-plugin.xml/*w ww  .j a  v a2 s .c  om*/
 * 
 * @param servletContext
 *            this webapp's servlet context
 * @param key
 *            kind of config to retrieve
 * @return the payment method config validator
 * @throws PortalException
 * @throws SystemException
 */
public static ConfigValidator getConfigValidator(ServletContext servletContext, String key)
        throws PortalException, SystemException {

    try {
        InputStream is = getPluginDescriptionStream(servletContext);

        if (is == null)
            return null;

        try {
            Document document = SAXReaderUtil.read(is);

            Node configNode = document.selectSingleNode("/plugin/" + key);
            if (configNode == null)
                return null;

            Node classNameNode = configNode.selectSingleNode("./validatorClass");
            if (classNameNode == null)
                return null;

            String className = classNameNode.getStringValue();

            try {
                ConfigDescription configDescription = ConfigDescriptionUtil.parseConfigDescription(configNode);

                ConfigValidator configValidator = (ConfigValidator) Class.forName(className).newInstance();
                configValidator.setConfigDescription(configDescription);

                return configValidator;

            } catch (Exception e) {
                throw new PortalException(
                        "Could not initialize validator for {plugin:" + servletContext.getServletContextName()
                                + ", key:" + key + ", classname:" + className + "}",
                        e);
            }

        } catch (Exception e) {
            throw new PortalException(e);

        } finally {
            is.close();
        }

    } catch (IOException e1) {
        throw new SystemException(e1);
    }
}

From source file:com.liferay.adaptive.media.web.internal.portlet.action.OptimizeImagesMVCActionCommand.java

License:Open Source License

private BackgroundTask _optimizeImages(long userId, long companyId, String jobName) throws PortalException {

    Map<String, Serializable> taskContextMap = new HashMap<>();

    taskContextMap.put(OptimizeImagesBackgroundTaskConstants.COMPANY_ID, companyId);
    taskContextMap.put(BackgroundTaskContextMapConstants.DELETE_ON_SUCCESS, true);

    try {/* w w  w.  j a  v a  2 s  . c  om*/
        return _backgroundTaskManager.addBackgroundTask(userId, CompanyConstants.SYSTEM, jobName,
                OptimizeImagesAllConfigurationsBackgroundTaskExecutor.class.getName(), taskContextMap,
                new ServiceContext());
    } catch (PortalException pe) {
        throw new PortalException("Unable to schedule adaptive media images optimization", pe);
    }
}

From source file:com.liferay.adaptive.media.web.internal.portlet.action.OptimizeImagesMVCActionCommand.java

License:Open Source License

private BackgroundTask _optimizeImagesSingleConfiguration(long userId, long companyId, String jobName,
        String configurationEntryUuid) throws PortalException {

    Map<String, Serializable> taskContextMap = new HashMap<>();

    taskContextMap.put(OptimizeImagesBackgroundTaskConstants.CONFIGURATION_ENTRY_UUID, configurationEntryUuid);
    taskContextMap.put(OptimizeImagesBackgroundTaskConstants.COMPANY_ID, companyId);
    taskContextMap.put(BackgroundTaskContextMapConstants.DELETE_ON_SUCCESS, true);

    try {//from ww w .j  a v  a 2  s  .  c o  m
        return _backgroundTaskManager.addBackgroundTask(userId, CompanyConstants.SYSTEM, jobName,
                OptimizeImagesSingleConfigurationBackgroundTaskExecutor.class.getName(), taskContextMap,
                new ServiceContext());
    } catch (PortalException pe) {
        throw new PortalException("Unable to schedule adaptive media images optimization for "
                + "configuration " + configurationEntryUuid, pe);
    }
}