Example usage for com.liferay.portal.kernel.util WebKeys UPLOAD_EXCEPTION

List of usage examples for com.liferay.portal.kernel.util WebKeys UPLOAD_EXCEPTION

Introduction

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

Prototype

String UPLOAD_EXCEPTION

To view the source code for com.liferay.portal.kernel.util WebKeys UPLOAD_EXCEPTION.

Click Source Link

Usage

From source file:org.opencps.paymentmgt.portlet.PaymentMgtFrontOfficePortlet.java

License:Open Source License

/**
 * Upload file// ww w.  j a  v  a  2 s  . c o  m
 * 
 * @param actionRequest
 * @param actionResponse
 * @return
 * @throws Exception
 */
protected FileEntry updateFileEntry(ActionRequest actionRequest, ActionResponse actionResponse)
        throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

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

    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);

    long repositoryId = themeDisplay.getScopeGroupId();

    long folderId = 0;

    Folder folderFile = initFolderService(actionRequest, themeDisplay);

    if (Validator.isNotNull(folderFile)) {
        folderId = folderFile.getFolderId();
    }

    Date now = new Date();
    String sourceFileName = now.getTime() + "_" + uploadPortletRequest.getFileName("uploadedFile");
    String title = ParamUtil.getString(uploadPortletRequest, "fileName");

    String description = ParamUtil.getString(uploadPortletRequest, "description");
    String changeLog = ParamUtil.getString(uploadPortletRequest, "changeLog");
    System.out.println("----FILE NAME----" + sourceFileName);
    if (folderId > 0) {
        Folder folder = DLAppServiceUtil.getFolder(folderId);

        if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
            throw new NoSuchFolderException("{folderId=" + folderId + "}");
        }
    }

    InputStream inputStream = null;

    try {
        String contentType = uploadPortletRequest.getContentType("uploadedFile");

        long size = uploadPortletRequest.getSize("uploadedFile");

        if ((cmd.equals(Constants.ADD) || cmd.equals(Constants.ADD_DYNAMIC)) && (size == 0)) {

            contentType = MimeTypesUtil.getContentType(title);
        }

        inputStream = uploadPortletRequest.getFileAsStream("uploadedFile");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
                uploadPortletRequest);
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);

        FileEntry fileEntry = null;

        // Add file entry

        fileEntry = DLAppServiceUtil.addFileEntry(repositoryId, folderId, sourceFileName, contentType,
                sourceFileName, description, changeLog, inputStream, size, serviceContext);
        System.out.println("----FILE ENTRY ID----" + fileEntry.getFileEntryId());
        AssetPublisherUtil.addAndStoreSelection(actionRequest, DLFileEntry.class.getName(),
                fileEntry.getFileEntryId(), -1);

        AssetPublisherUtil.addRecentFolderId(actionRequest, DLFileEntry.class.getName(), folderId);

        return fileEntry;
    } catch (Exception e) {
        e.printStackTrace();
        UploadException uploadException = (UploadException) actionRequest
                .getAttribute(WebKeys.UPLOAD_EXCEPTION);

        if (uploadException != null) {
            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
                throw new LiferayFileItemException();
            } else if (uploadException.isExceededSizeLimit()) {
                throw new FileSizeException(uploadException.getCause());
            }
        }

        throw e;
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}