Example usage for com.liferay.portal.kernel.util TextFormatter formatStorageSize

List of usage examples for com.liferay.portal.kernel.util TextFormatter formatStorageSize

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util TextFormatter formatStorageSize.

Prototype

public static String formatStorageSize(int size, Locale locale) 

Source Link

Usage

From source file:com.liferay.document.library.web.internal.display.context.logic.UIItemsBuilder.java

License:Open Source License

public void addDownloadMenuItem(List<MenuItem> menuItems) throws PortalException {

    if (!_fileEntryDisplayContextHelper.isDownloadActionAvailable()) {
        return;//  ww w .j a  va2s. c om
    }

    String label = TextFormatter.formatStorageSize(_fileEntry.getSize(), _themeDisplay.getLocale());

    label = _themeDisplay.translate("download") + " (" + label + ")";

    String url = DLUtil.getDownloadURL(_fileEntry, _fileVersion, _themeDisplay, StringPool.BLANK, false, true);

    URLMenuItem urlMenuItem = _addURLUIItem(new URLMenuItem(), menuItems, DLUIItemKeys.DOWNLOAD, label, url);

    urlMenuItem.setMethod("get");
    urlMenuItem.setTarget("_blank");
}

From source file:com.liferay.document.library.web.internal.portlet.action.EditFileEntryMVCActionCommand.java

License:Open Source License

protected String getAddMultipleFileEntriesErrorMessage(PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse, Exception e) throws Exception {

    String errorMessage = null;// w ww. j  a va2 s  .c o  m

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (e instanceof AntivirusScannerException) {
        AntivirusScannerException ase = (AntivirusScannerException) e;

        errorMessage = themeDisplay.translate(ase.getMessageKey());
    } else if (e instanceof AssetCategoryException) {
        AssetCategoryException ace = (AssetCategoryException) e;

        AssetVocabulary assetVocabulary = ace.getVocabulary();

        String vocabularyTitle = StringPool.BLANK;

        if (assetVocabulary != null) {
            vocabularyTitle = assetVocabulary.getTitle(themeDisplay.getLocale());
        }

        if (ace.getType() == AssetCategoryException.AT_LEAST_ONE_CATEGORY) {
            errorMessage = themeDisplay.translate("please-select-at-least-one-category-for-x", vocabularyTitle);
        } else if (ace.getType() == AssetCategoryException.TOO_MANY_CATEGORIES) {

            errorMessage = themeDisplay.translate("you-cannot-select-more-than-one-category-for-x",
                    vocabularyTitle);
        }
    } else if (e instanceof DuplicateFileEntryException) {
        errorMessage = themeDisplay.translate("the-folder-you-selected-already-has-an-entry-with-this-"
                + "name.-please-select-a-different-folder");
    } else if (e instanceof FileExtensionException) {
        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-extension-x",
                StringUtil.merge(getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
    } else if (e instanceof FileNameException) {
        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
    } else if (e instanceof FileSizeException) {
        long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

        if (fileMaxSize == 0) {
            fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
        }

        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-size-no-larger-than-x",
                TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));
    } else if (e instanceof InvalidFileEntryTypeException) {
        errorMessage = themeDisplay.translate("the-document-type-you-selected-is-not-valid-for-this-folder");
    } else {
        errorMessage = themeDisplay.translate("an-unexpected-error-occurred-while-saving-your-document");
    }

    return errorMessage;
}

From source file:com.liferay.document.library.web.internal.portlet.action.EditFileEntryMVCActionCommand.java

License:Open Source License

protected void handleUploadException(PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse, String cmd, Exception e) throws Exception {

    if (e instanceof AssetCategoryException || e instanceof AssetTagException) {

        SessionErrors.add(actionRequest, e.getClass(), e);
    } else if (e instanceof AntivirusScannerException || e instanceof DuplicateFileEntryException
            || e instanceof DuplicateFolderNameException || e instanceof FileExtensionException
            || e instanceof FileMimeTypeException || e instanceof FileNameException
            || e instanceof FileSizeException || e instanceof LiferayFileItemException
            || e instanceof NoSuchFolderException || e instanceof SourceFileNameException
            || e instanceof StorageFieldRequiredException || e instanceof UploadRequestSizeException) {

        if (!cmd.equals(Constants.ADD_DYNAMIC) && !cmd.equals(Constants.ADD_MULTIPLE)
                && !cmd.equals(Constants.ADD_TEMP)) {

            if (e instanceof AntivirusScannerException) {
                SessionErrors.add(actionRequest, e.getClass(), e);
            } else {
                SessionErrors.add(actionRequest, e.getClass());
            }/*from   w  ww. j a v  a  2  s.com*/

            return;
        } else if (cmd.equals(Constants.ADD_TEMP)) {
            hideDefaultErrorMessage(actionRequest);
        }

        if (e instanceof AntivirusScannerException || e instanceof DuplicateFileEntryException
                || e instanceof FileExtensionException || e instanceof FileNameException
                || e instanceof FileSizeException || e instanceof UploadRequestSizeException) {

            HttpServletResponse response = _portal.getHttpServletResponse(actionResponse);

            response.setContentType(ContentTypes.TEXT_HTML);
            response.setStatus(HttpServletResponse.SC_OK);

            String errorMessage = StringPool.BLANK;
            int errorType = 0;

            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

            if (e instanceof AntivirusScannerException) {
                AntivirusScannerException ase = (AntivirusScannerException) e;

                errorMessage = themeDisplay.translate(ase.getMessageKey());

                errorType = ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
            }

            if (e instanceof DuplicateFileEntryException) {
                errorMessage = themeDisplay.translate("please-enter-a-unique-document-name");
                errorType = ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
            } else if (e instanceof FileExtensionException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-extension-x", StringUtil
                        .merge(getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
                errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
            } else if (e instanceof FileNameException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
                errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
            } else if (e instanceof FileSizeException) {
                long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

                if (fileMaxSize == 0) {
                    fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
                }

                errorMessage = themeDisplay.translate(
                        "please-enter-a-file-with-a-valid-file-size-no-" + "larger-than-x",
                        TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));

                errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
            }

            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            jsonObject.put("message", errorMessage);
            jsonObject.put("status", errorType);

            JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
        }

        if (e instanceof AntivirusScannerException) {
            SessionErrors.add(actionRequest, e.getClass(), e);
        } else {
            SessionErrors.add(actionRequest, e.getClass());
        }
    } else if (e instanceof DuplicateLockException || e instanceof FileEntryLockException.MustOwnLock
            || e instanceof InvalidFileVersionException || e instanceof NoSuchFileEntryException
            || e instanceof PrincipalException) {

        if (e instanceof DuplicateLockException) {
            DuplicateLockException dle = (DuplicateLockException) e;

            SessionErrors.add(actionRequest, dle.getClass(), dle.getLock());
        } else {
            SessionErrors.add(actionRequest, e.getClass());
        }

        actionResponse.setRenderParameter("mvcPath", "/document_library/error.jsp");
    } else {
        Throwable cause = e.getCause();

        if (cause instanceof DuplicateFileEntryException) {
            SessionErrors.add(actionRequest, DuplicateFileEntryException.class);
        } else {
            throw e;
        }
    }
}

From source file:com.liferay.exportimport.staging.StagingImpl.java

License:Open Source License

@Override
public JSONObject getExceptionMessagesJSONObject(Locale locale, Exception e,
        ExportImportConfiguration exportImportConfiguration) {

    JSONObject exceptionMessagesJSONObject = JSONFactoryUtil.createJSONObject();

    String errorMessage = StringPool.BLANK;
    JSONArray errorMessagesJSONArray = null;
    int errorType = 0;
    JSONArray warningMessagesJSONArray = null;

    ResourceBundle resourceBundle = ResourceBundleUtil.getBundle("content.Language", locale, getClass());

    Throwable cause = e.getCause();

    if (e instanceof DuplicateFileEntryException) {
        errorMessage = LanguageUtil.get(locale, "please-enter-a-unique-document-name");
        errorType = ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
    } else if (e instanceof FileExtensionException) {
        errorMessage = LanguageUtil.format(locale,
                "document-names-must-end-with-one-of-the-following-extensions", ".lar", false);
        errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
    } else if (e instanceof FileNameException) {
        errorMessage = LanguageUtil.get(locale, "please-enter-a-file-with-a-valid-file-name");
        errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
    } else if (e instanceof FileSizeException || e instanceof LARFileSizeException) {

        if ((exportImportConfiguration != null) && ((exportImportConfiguration
                .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL)
                || (exportImportConfiguration
                        .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE)
                || (exportImportConfiguration
                        .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_PORTLET))) {

            errorMessage = LanguageUtil.get(locale,
                    "file-size-limit-exceeded.-please-ensure-that-the-file-"
                            + "does-not-exceed-the-file-size-limit-in-both-the-"
                            + "live-environment-and-the-staging-environment");
        } else {//from   w  w w.  j  ava  2s .c om
            errorMessage = LanguageUtil.format(locale,
                    "please-enter-a-file-with-a-valid-file-size-no-larger-" + "than-x",
                    TextFormatter.formatStorageSize(_dlValidator.getMaxAllowableSize(), locale), false);
        }

        errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
    } else if (e instanceof LARTypeException) {
        LARTypeException lte = (LARTypeException) e;

        if (lte.getType() == LARTypeException.TYPE_COMPANY_GROUP) {
            errorMessage = LanguageUtil.format(resourceBundle, "a-x-can-only-be-imported-to-a-x",
                    "global-site");
        } else if (lte.getType() == LARTypeException.TYPE_LAYOUT_PROTOTYPE) {
            errorMessage = LanguageUtil.format(resourceBundle, "a-x-can-only-be-imported-to-a-x",
                    LanguageUtil.get(locale, "page-template"));
        } else if (lte.getType() == LARTypeException.TYPE_LAYOUT_SET) {
            errorMessage = LanguageUtil.format(resourceBundle, "a-x-can-only-be-imported-to-a-x", "site");
        } else if (lte.getType() == LARTypeException.TYPE_LAYOUT_SET_PROTOTYPE) {

            errorMessage = LanguageUtil.format(resourceBundle, "a-x-can-only-be-imported-to-a-x",
                    LanguageUtil.get(locale, "site-template"));
        } else {
            errorMessage = LanguageUtil.format(resourceBundle, "uploaded-lar-file-type-x-does-not-match-x",
                    new Object[] { lte.getActualLARType(),
                            StringUtil.merge(lte.getExpectedLARTypes(), StringPool.COMMA_AND_SPACE) });
        }

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof LARFileException) {
        LARFileException lfe = (LARFileException) e;

        if (lfe.getType() == LARFileException.TYPE_INVALID_MANIFEST) {
            errorMessage = LanguageUtil.format(resourceBundle, "invalid-manifest.xml-x", lfe.getMessage());
        } else if (lfe.getType() == LARFileException.TYPE_MISSING_MANIFEST) {
            errorMessage = LanguageUtil.get(resourceBundle, "missing-manifest.xml");
        } else {
            errorMessage = LanguageUtil.get(locale, "please-specify-a-lar-file-to-import");
        }

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof LayoutImportException || cause instanceof LayoutImportException) {

        LayoutImportException lie = null;

        if (e instanceof LayoutImportException) {
            lie = (LayoutImportException) e;
        } else {
            lie = (LayoutImportException) cause;
        }

        if (lie.getType() == LayoutImportException.TYPE_WRONG_BUILD_NUMBER) {

            errorMessage = LanguageUtil.format(resourceBundle,
                    "lar-build-number-x-does-not-match-portal-build-number-x", lie.getArguments());
        } else if (lie.getType() == LayoutImportException.TYPE_WRONG_LAR_SCHEMA_VERSION) {

            errorMessage = LanguageUtil.format(resourceBundle,
                    "lar-schema-version-x-does-not-match-deployed-export-" + "import-schema-version-x",
                    lie.getArguments());
        } else if (lie.getType() == LayoutImportException.TYPE_WRONG_PORTLET_SCHEMA_VERSION) {

            Object[] arguments = lie.getArguments();

            Portlet portlet = _portletLocalService.getPortletById((String) arguments[1]);

            arguments[1] = portlet.getDisplayName();

            errorMessage = LanguageUtil.format(resourceBundle,
                    "portlet's-schema-version-x-in-the-lar-is-not-valid-for-"
                            + "the-deployed-portlet-x-with-schema-version-x",
                    lie.getArguments());
        } else {
            errorMessage = e.getLocalizedMessage();
        }

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof LayoutPrototypeException) {
        LayoutPrototypeException lpe = (LayoutPrototypeException) e;

        StringBundler sb = new StringBundler(4);

        sb.append("the-lar-file-could-not-be-imported-because-it-");
        sb.append("requires-page-templates-or-site-templates-that-could-");
        sb.append("not-be-found.-please-import-the-following-templates-");
        sb.append("manually");

        errorMessage = LanguageUtil.get(resourceBundle, sb.toString());

        errorMessagesJSONArray = JSONFactoryUtil.createJSONArray();

        List<Tuple> missingLayoutPrototypes = lpe.getMissingLayoutPrototypes();

        for (Tuple missingLayoutPrototype : missingLayoutPrototypes) {
            JSONObject errorMessageJSONObject = JSONFactoryUtil.createJSONObject();

            String layoutPrototypeUuid = (String) missingLayoutPrototype.getObject(1);

            errorMessageJSONObject.put("info", layoutPrototypeUuid);

            String layoutPrototypeName = (String) missingLayoutPrototype.getObject(2);

            errorMessageJSONObject.put("name", layoutPrototypeName);

            String layoutPrototypeClassName = (String) missingLayoutPrototype.getObject(0);

            errorMessageJSONObject.put("type",
                    ResourceActionsUtil.getModelResource(locale, layoutPrototypeClassName));

            errorMessagesJSONArray.put(errorMessageJSONObject);
        }

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof LocaleException) {
        LocaleException le = (LocaleException) e;

        errorMessage = LanguageUtil.format(locale,
                "the-available-languages-in-the-lar-file-x-do-not-match-the-" + "site's-available-languages-x",
                new String[] { StringUtil.merge(le.getSourceAvailableLocales(), StringPool.COMMA_AND_SPACE),
                        StringUtil.merge(le.getTargetAvailableLocales(), StringPool.COMMA_AND_SPACE) },
                false);

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof MissingReferenceException) {
        MissingReferenceException mre = (MissingReferenceException) e;

        if ((exportImportConfiguration != null) && ((exportImportConfiguration
                .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL)
                || (exportImportConfiguration
                        .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE)
                || (exportImportConfiguration
                        .getType() == ExportImportConfigurationConstants.TYPE_PUBLISH_PORTLET))) {

            errorMessage = LanguageUtil.get(locale, "there-are-missing-references-that-could-not-be-found-in-"
                    + "the-live-environment-the-following-elements-are-" + "published-from-their-own-site");
        } else {
            errorMessage = LanguageUtil.get(locale,
                    "there-are-missing-references-that-could-not-be-found-in-" + "the-current-site");
        }

        MissingReferences missingReferences = mre.getMissingReferences();

        errorMessagesJSONArray = getErrorMessagesJSONArray(locale,
                missingReferences.getDependencyMissingReferences());

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
        warningMessagesJSONArray = getWarningMessagesJSONArray(locale,
                missingReferences.getWeakMissingReferences());
    } else if (e instanceof PortletDataException) {
        PortletDataException pde = (PortletDataException) e;

        String referrerClassName = pde.getStagedModelClassName();
        String referrerDisplayName = pde.getStagedModelDisplayName();

        String modelResource = ResourceActionsUtil.getModelResource(locale, referrerClassName);

        if (pde.getType() == PortletDataException.DELETE_PORTLET_DATA) {
            if (Validator.isNotNull(pde.getLocalizedMessage())) {
                errorMessage = LanguageUtil.format(locale,
                        "the-following-error-in-x-while-deleting-its-data-" + "has-stopped-the-process-x",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale),
                                pde.getLocalizedMessage() },
                        false);
            } else {
                errorMessage = LanguageUtil.format(locale,
                        "an-unexpected-error-in-x-while-deleting-its-data-" + "has-stopped-the-process",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale) }, false);
            }
        } else if (pde.getType() == PortletDataException.EXPORT_PORTLET_DATA) {

            if (Validator.isNotNull(pde.getLocalizedMessage())) {
                errorMessage = LanguageUtil.format(locale,
                        "the-following-error-in-x-while-exporting-its-data-" + "has-stopped-the-process-x",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale),
                                pde.getLocalizedMessage() },
                        false);
            } else {
                errorMessage = LanguageUtil.format(locale,
                        "an-unexpected-error-in-x-while-exporting-its-data-" + "has-stopped-the-process",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale) }, false);
            }
        } else if (pde.getType() == PortletDataException.IMPORT_PORTLET_DATA) {

            if (Validator.isNotNull(pde.getLocalizedMessage())) {
                errorMessage = LanguageUtil.format(locale,
                        "the-following-error-in-x-while-importing-its-data-" + "has-stopped-the-process-x",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale),
                                pde.getLocalizedMessage() },
                        false);
            } else {
                errorMessage = LanguageUtil.format(locale,
                        "an-unexpected-error-in-x-while-importing-its-data-" + "has-stopped-the-process",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale) }, false);
            }
        } else if (pde.getType() == PortletDataException.INVALID_GROUP) {
            errorMessage = LanguageUtil.format(locale,
                    "the-x-x-could-not-be-exported-because-it-is-not-in-the-" + "currently-exported-group",
                    new String[] { modelResource, referrerDisplayName }, false);
        } else if (pde.getType() == PortletDataException.MISSING_DEPENDENCY) {
            errorMessage = LanguageUtil.format(locale,
                    "the-x-x-has-missing-references-that-could-not-be-found-" + "during-the-process",
                    new String[] { modelResource, referrerDisplayName }, false);
        } else if (pde.getType() == PortletDataException.PREPARE_MANIFEST_SUMMARY) {

            if (Validator.isNotNull(pde.getLocalizedMessage())) {
                errorMessage = LanguageUtil.format(locale,
                        "the-following-error-in-x-while-preparing-its-" + "manifest-has-stopped-the-process-x",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale),
                                pde.getLocalizedMessage() },
                        false);
            } else {
                errorMessage = LanguageUtil.format(locale,
                        "an-unexpected-error-in-x-while-preparing-its-" + "manifest-has-stopped-the-process",
                        new String[] { _portal.getPortletTitle(pde.getPortletId(), locale) }, false);
            }
        } else if (pde.getType() == PortletDataException.STATUS_IN_TRASH) {
            errorMessage = LanguageUtil.format(locale,
                    "the-x-x-could-not-be-exported-because-it-is-in-the-" + "recycle-bin",
                    new String[] { modelResource, referrerDisplayName }, false);
        } else if (pde.getType() == PortletDataException.STATUS_UNAVAILABLE) {
            errorMessage = LanguageUtil.format(locale,
                    "the-x-x-could-not-be-exported-because-its-workflow-" + "status-is-not-exportable",
                    new String[] { modelResource, referrerDisplayName }, false);
        } else if (Validator.isNotNull(referrerDisplayName)) {
            errorMessage = LanguageUtil.format(resourceBundle,
                    "the-following-error-occurred-while-processing-the-x-x-x",
                    new String[] { modelResource, referrerDisplayName, e.getLocalizedMessage() });
        } else {
            errorMessage = e.getLocalizedMessage();
        }

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else if (e instanceof PortletIdException) {
        PortletIdException pie = (PortletIdException) e;

        Portlet portlet = _portletLocalService.getPortletById(pie.getMessage());

        errorMessage = LanguageUtil.format(resourceBundle, "a-x-can-only-be-imported-to-a-x",
                portlet.getDisplayName() + " Portlet");

        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    } else {
        errorMessage = e.getLocalizedMessage();
        errorType = ServletResponseConstants.SC_FILE_CUSTOM_EXCEPTION;
    }

    exceptionMessagesJSONObject.put("message", errorMessage);

    if ((errorMessagesJSONArray != null) && (errorMessagesJSONArray.length() > 0)) {

        exceptionMessagesJSONObject.put("messageListItems", errorMessagesJSONArray);
    }

    exceptionMessagesJSONObject.put("status", errorType);

    if ((warningMessagesJSONArray != null) && (warningMessagesJSONArray.length() > 0)) {

        exceptionMessagesJSONObject.put("warningMessages", warningMessagesJSONArray);
    }

    return exceptionMessagesJSONObject;
}

From source file:com.liferay.image.uploader.web.internal.portlet.action.UploadImageMVCActionCommand.java

License:Open Source License

protected void handleUploadException(ActionRequest actionRequest, ActionResponse actionResponse, String cmd,
        long maxFileSize, Exception e) throws Exception {

    if (e instanceof PrincipalException) {
        SessionErrors.add(actionRequest, e.getClass());

        actionResponse.setRenderParameter("mvcPath", "/error.jsp");
    } else if (e instanceof AntivirusScannerException || e instanceof FileExtensionException
            || e instanceof FileSizeException || e instanceof ImageTypeException
            || e instanceof NoSuchFileException || e instanceof UploadException
            || e instanceof UploadRequestSizeException) {

        if (cmd.equals(Constants.ADD_TEMP)) {
            hideDefaultErrorMessage(actionRequest);

            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

            String errorMessage = StringPool.BLANK;

            if (e instanceof AntivirusScannerException) {
                AntivirusScannerException ase = (AntivirusScannerException) e;

                errorMessage = themeDisplay.translate(ase.getMessageKey());
            } else if (e instanceof FileExtensionException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-extension-x",
                        StringUtil.merge(_dlConfiguration.fileExtensions()));
            } else if (e instanceof FileSizeException) {
                if (maxFileSize == 0) {
                    maxFileSize = _uploadServletRequestConfigurationHelper.getMaxSize();
                }//from w  w w . j a  va 2 s.  c  om

                errorMessage = themeDisplay.translate(
                        "please-enter-a-file-with-a-valid-file-size-no-" + "larger-than-x",
                        TextFormatter.formatStorageSize(maxFileSize, themeDisplay.getLocale()));
            } else if (e instanceof ImageTypeException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-type");
            } else if (e instanceof NoSuchFileException || e instanceof UploadException) {

                errorMessage = themeDisplay
                        .translate("an-unexpected-error-occurred-while-uploading-your-" + "file");
            }

            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            jsonObject.put("errorMessage", errorMessage);

            JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
        } else {
            SessionErrors.add(actionRequest, e.getClass(), e);
        }
    } else {
        throw e;
    }
}

From source file:com.liferay.item.selector.taglib.internal.util.ItemSelectorRepositoryEntryBrowserUtil.java

License:Open Source License

public static JSONObject getItemMetadataJSONObject(FileEntry fileEntry, Locale locale) throws PortalException {

    JSONObject itemMetadataJSONObject = JSONFactoryUtil.createJSONObject();

    JSONArray groupsJSONArray = JSONFactoryUtil.createJSONArray();

    JSONObject firstTabJSONObject = JSONFactoryUtil.createJSONObject();

    JSONArray firstTabDataJSONArray = JSONFactoryUtil.createJSONArray();

    FileVersion latestFileVersion = fileEntry.getLatestFileVersion();

    firstTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "format"),
            HtmlUtil.escape(latestFileVersion.getExtension())));

    firstTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "size"),
            TextFormatter.formatStorageSize(fileEntry.getSize(), locale)));
    firstTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "name"),
            HtmlUtil.escape(DLUtil.getTitleWithExtension(fileEntry))));

    Date modifiedDate = fileEntry.getModifiedDate();

    firstTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "modified"),
            LanguageUtil.format(locale, "x-ago-by-x",
                    new Object[] {
                            LanguageUtil.getTimeDescription(locale,
                                    System.currentTimeMillis() - modifiedDate.getTime(), true),
                            HtmlUtil.escape(fileEntry.getUserName()) })));

    firstTabJSONObject.put("data", firstTabDataJSONArray);

    firstTabJSONObject.put("title", LanguageUtil.get(locale, "file-info"));

    groupsJSONArray.put(firstTabJSONObject);

    JSONObject secondTabJSONObject = JSONFactoryUtil.createJSONObject();

    JSONArray secondTabDataJSONArray = JSONFactoryUtil.createJSONArray();

    secondTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "version"),
            HtmlUtil.escape(latestFileVersion.getVersion())));
    secondTabDataJSONArray.put(_createJSONObject(LanguageUtil.get(locale, "status"),
            WorkflowConstants.getStatusLabel(latestFileVersion.getStatus())));

    secondTabJSONObject.put("data", secondTabDataJSONArray);

    secondTabJSONObject.put("title", LanguageUtil.get(locale, "version"));

    groupsJSONArray.put(secondTabJSONObject);

    itemMetadataJSONObject.put("groups", groupsJSONArray);

    return itemMetadataJSONObject;
}

From source file:com.liferay.knowledgebase.admin.util.AdminSubscriptionSender.java

License:Open Source License

protected String getEmailKBArticleAttachments(Locale locale) throws Exception {

    List<FileEntry> attachmentsFileEntries = _kbArticle.getAttachmentsFileEntries();

    if (attachmentsFileEntries.isEmpty()) {
        return StringPool.BLANK;
    }//from ww  w .  j a  v a  2  s .  co  m

    StringBundler sb = new StringBundler(attachmentsFileEntries.size() * 5);

    for (FileEntry fileEntry : attachmentsFileEntries) {
        sb.append(fileEntry.getTitle());
        sb.append(" (");
        sb.append(TextFormatter.formatStorageSize(fileEntry.getSize(), locale));
        sb.append(")");
        sb.append("<br />");
    }

    return sb.toString();
}

From source file:com.liferay.wiki.web.internal.portlet.action.EditPageAttachmentsMVCActionCommand.java

License:Open Source License

/**
 * TODO: Remove. This should extend from EditFileEntryAction once it is
 * modularized./*from ww w .  j a va  2s.c om*/
 */
protected void handleUploadException(PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse, String cmd, Exception e) throws Exception {

    if (e instanceof AssetCategoryException || e instanceof AssetTagException) {

        SessionErrors.add(actionRequest, e.getClass(), e);
    } else if (e instanceof AntivirusScannerException || e instanceof DuplicateFileEntryException
            || e instanceof DuplicateFolderNameException || e instanceof FileExtensionException
            || e instanceof FileMimeTypeException || e instanceof FileNameException
            || e instanceof FileSizeException || e instanceof LiferayFileItemException
            || e instanceof NoSuchFolderException || e instanceof SourceFileNameException
            || e instanceof StorageFieldRequiredException || e instanceof UploadRequestSizeException) {

        if (!cmd.equals(Constants.ADD_DYNAMIC) && !cmd.equals(Constants.ADD_MULTIPLE)
                && !cmd.equals(Constants.ADD_TEMP)) {

            if (e instanceof AntivirusScannerException) {
                SessionErrors.add(actionRequest, e.getClass(), e);
            } else {
                SessionErrors.add(actionRequest, e.getClass());
            }

            return;
        } else if (cmd.equals(Constants.ADD_TEMP)) {
            hideDefaultErrorMessage(actionRequest);
        }

        if (e instanceof AntivirusScannerException || e instanceof DuplicateFileEntryException
                || e instanceof FileExtensionException || e instanceof FileNameException
                || e instanceof FileSizeException || e instanceof UploadRequestSizeException) {

            HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

            response.setContentType(ContentTypes.TEXT_HTML);
            response.setStatus(HttpServletResponse.SC_OK);

            String errorMessage = StringPool.BLANK;
            int errorType = 0;

            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

            if (e instanceof AntivirusScannerException) {
                AntivirusScannerException ase = (AntivirusScannerException) e;

                errorMessage = themeDisplay.translate(ase.getMessageKey());
                errorType = ServletResponseConstants.SC_FILE_ANTIVIRUS_EXCEPTION;
            }

            if (e instanceof DuplicateFileEntryException) {
                errorMessage = themeDisplay.translate("please-enter-a-unique-document-name");
                errorType = ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION;
            } else if (e instanceof FileExtensionException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-extension-x", StringUtil
                        .merge(getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
                errorType = ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION;
            } else if (e instanceof FileNameException) {
                errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
                errorType = ServletResponseConstants.SC_FILE_NAME_EXCEPTION;
            } else if (e instanceof FileSizeException) {
                long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

                if (fileMaxSize == 0) {
                    fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
                }

                errorMessage = themeDisplay.translate(
                        "please-enter-a-file-with-a-valid-file-size-no-larger" + "-than-x",
                        TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));

                errorType = ServletResponseConstants.SC_FILE_SIZE_EXCEPTION;
            }

            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            jsonObject.put("message", errorMessage);
            jsonObject.put("status", errorType);

            JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
        }

        if (e instanceof AntivirusScannerException) {
            SessionErrors.add(actionRequest, e.getClass(), e);
        } else {
            SessionErrors.add(actionRequest, e.getClass());
        }
    } else if (e instanceof DuplicateLockException || e instanceof InvalidFileVersionException
            || e instanceof NoSuchFileEntryException || e instanceof PrincipalException) {

        if (e instanceof DuplicateLockException) {
            DuplicateLockException dle = (DuplicateLockException) e;

            SessionErrors.add(actionRequest, dle.getClass(), dle.getLock());
        } else {
            SessionErrors.add(actionRequest, e.getClass());
        }

        actionResponse.setRenderParameter("mvcPath", "/html/porltet/document_library/error.jsp");
    } else {
        Throwable cause = e.getCause();

        if (cause instanceof DuplicateFileEntryException) {
            SessionErrors.add(actionRequest, DuplicateFileEntryException.class);
        } else {
            throw e;
        }
    }
}

From source file:org.lsug.quota.web.internal.portlet.action.documentlibrary.QuotaUploadMultipleFileEntriesMVCActionCommand.java

License:Open Source License

protected String getAddMultipleFileEntriesErrorMessage(PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse, Exception e) throws Exception {

    String errorMessage = null;//w  w w .  j  a  v a2s  .c om

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (e instanceof AntivirusScannerException) {
        AntivirusScannerException ase = (AntivirusScannerException) e;

        errorMessage = themeDisplay.translate(ase.getMessageKey());
    } else if (e instanceof AssetCategoryException) {
        AssetCategoryException ace = (AssetCategoryException) e;

        AssetVocabulary assetVocabulary = ace.getVocabulary();

        String vocabularyTitle = StringPool.BLANK;

        if (assetVocabulary != null) {
            vocabularyTitle = assetVocabulary.getTitle(themeDisplay.getLocale());
        }

        if (ace.getType() == AssetCategoryException.AT_LEAST_ONE_CATEGORY) {
            errorMessage = themeDisplay.translate("please-select-at-least-one-category-for-x", vocabularyTitle);
        } else if (ace.getType() == AssetCategoryException.TOO_MANY_CATEGORIES) {

            errorMessage = themeDisplay.translate("you-cannot-select-more-than-one-category-for-x",
                    vocabularyTitle);
        }
    } else if (e instanceof DuplicateFileEntryException) {
        errorMessage = themeDisplay.translate("the-folder-you-selected-already-has-an-entry-with-this-"
                + "name.-please-select-a-different-folder");
    } else if (e instanceof FileExtensionException) {
        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-extension-x",
                StringUtil.merge(getAllowedFileExtensions(portletConfig, actionRequest, actionResponse)));
    } else if (e instanceof FileNameException) {
        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-name");
    } else if (e instanceof FileSizeException) {
        long fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE);

        if (fileMaxSize == 0) {
            fileMaxSize = PrefsPropsUtil.getLong(PropsKeys.UPLOAD_SERVLET_REQUEST_IMPL_MAX_SIZE);
        }

        errorMessage = themeDisplay.translate("please-enter-a-file-with-a-valid-file-size-no-larger-than-x",
                TextFormatter.formatStorageSize(fileMaxSize, themeDisplay.getLocale()));
    } else if (e instanceof InvalidFileEntryTypeException) {
        errorMessage = themeDisplay.translate("the-document-type-you-selected-is-not-valid-for-this-folder");
    } else if (e instanceof InvalidFileEntryTypeException) {
        errorMessage = themeDisplay.translate("the-document-type-you-selected-is-not-valid-for-this-folder");
    } else if (e instanceof QuotaExceededException) {
        ResourceBundle resourceBundle = ResourceBundleUtil.getBundle("content.Language",
                themeDisplay.getLocale(), getClass());

        errorMessage = ResourceBundleUtil.getString(resourceBundle, "quota-exceeded-error");
    } else {
        errorMessage = themeDisplay.translate("an-unexpected-error-occurred-while-saving-your-document");
    }

    return errorMessage;
}