Example usage for com.liferay.portal.kernel.util PortalUtil getUploadPortletRequest

List of usage examples for com.liferay.portal.kernel.util PortalUtil getUploadPortletRequest

Introduction

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

Prototype

public static UploadPortletRequest getUploadPortletRequest(PortletRequest portletRequest) 

Source Link

Usage

From source file:ch.inofix.referencemanager.web.internal.portlet.ReferenceManagerPortlet.java

License:Apache License

/**
 * /*w  w  w .  jav a 2  s . com*/
 * @param actionRequest
 * @param actionResponse
 * @since 1.0.0
 * @throws Exception
 */
public void importBibTeXFile(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

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

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    File file = uploadPortletRequest.getFile("file");
    String fileName = file.getName();

    long userId = themeDisplay.getUserId();
    long groupId = themeDisplay.getScopeGroupId();
    boolean privateLayout = themeDisplay.getLayout().isPrivateLayout();

    String servletContextName = request.getSession().getServletContext().getServletContextName();

    String[] servletContextNames = new String[] { servletContextName };

    Map<String, String[]> parameterMap = new HashMap<String, String[]>(actionRequest.getParameterMap());
    parameterMap.put("servletContextNames", servletContextNames);

    if (Validator.isNotNull(file)) {

        String message = PortletUtil.translate("upload-successfull-import-will-finish-in-a-separate-thread");
        ServiceContext serviceContext = ServiceContextFactory.getInstance(Reference.class.getName(),
                uploadPortletRequest);

        _referenceService.importReferencesInBackground(userId, fileName, groupId, privateLayout, parameterMap,
                file, serviceContext);

        SessionMessages.add(actionRequest, "request_processed", message);

    } else {

        SessionErrors.add(actionRequest, "file-not-found");

    }
}

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

License:Open Source License

protected FileEntry addFileEntry(ActionRequest actionRequest) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

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

    long repositoryId = ParamUtil.getLong(uploadPortletRequest, "repositoryId");
    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    String sourceFileName = uploadPortletRequest.getFileName("file");
    String title = ParamUtil.getString(uploadPortletRequest, "title");
    String description = ParamUtil.getString(uploadPortletRequest, "description");

    if (folderId > 0) {
        Folder folder = _dlAppService.getFolder(folderId);

        if (folder.getGroupId() != themeDisplay.getScopeGroupId()) {
            throw new NoSuchFolderException("{folderId=" + folderId + "}");
        }/*from  w  w  w.  j ava2  s.com*/
    }

    InputStream inputStream = null;

    try {
        String contentType = uploadPortletRequest.getContentType("file");
        long size = uploadPortletRequest.getSize("file");

        if (size == 0) {
            contentType = MimeTypesUtil.getContentType(title);
        }

        inputStream = uploadPortletRequest.getFileAsStream("file");

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

        FileEntry fileEntry = _dlAppService.addFileEntry(repositoryId, folderId, sourceFileName, contentType,
                title, description, StringPool.BLANK, inputStream, size, serviceContext);

        return fileEntry;
    } finally {
        StreamUtil.cleanUp(inputStream);
    }
}

From source file:com.liferay.message.boards.web.internal.portlet.action.EditMessageMVCActionCommand.java

License:Open Source License

protected MBMessage updateMessage(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

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

    long messageId = ParamUtil.getLong(actionRequest, "messageId");

    long groupId = themeDisplay.getScopeGroupId();
    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
    long threadId = ParamUtil.getLong(actionRequest, "threadId");
    long parentMessageId = ParamUtil.getLong(actionRequest, "parentMessageId");
    String subject = ParamUtil.getString(actionRequest, "subject");
    String body = ParamUtil.getString(actionRequest, "body");

    MBGroupServiceSettings mbGroupServiceSettings = MBGroupServiceSettings.getInstance(groupId);

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<>(5);

    try {/*  ww  w  .  j a v a2 s .com*/
        UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

        for (int i = 1; i <= 5; i++) {
            String fileName = uploadPortletRequest.getFileName("msgFile" + i);
            InputStream inputStream = uploadPortletRequest.getFileAsStream("msgFile" + i);

            if ((inputStream == null) || Validator.isNull(fileName)) {
                continue;
            }

            ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName, inputStream);

            inputStreamOVPs.add(inputStreamOVP);
        }

        boolean question = ParamUtil.getBoolean(actionRequest, "question");
        boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
        double priority = ParamUtil.getDouble(actionRequest, "priority");
        boolean allowPingbacks = ParamUtil.getBoolean(actionRequest, "allowPingbacks");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(MBMessage.class.getName(),
                actionRequest);

        boolean preview = ParamUtil.getBoolean(actionRequest, "preview");

        serviceContext.setAttribute("preview", preview);

        MBMessage message = null;

        if (messageId <= 0) {
            if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {

                CaptchaUtil.check(actionRequest);
            }

            if (threadId <= 0) {

                // Post new thread

                message = _mbMessageService.addMessage(groupId, categoryId, subject, body,
                        mbGroupServiceSettings.getMessageFormat(), inputStreamOVPs, anonymous, priority,
                        allowPingbacks, serviceContext);

                if (question) {
                    _mbThreadLocalService.updateQuestion(message.getThreadId(), true);
                }
            } else {

                // Post reply

                message = _mbMessageService.addMessage(parentMessageId, subject, body,
                        mbGroupServiceSettings.getMessageFormat(), inputStreamOVPs, anonymous, priority,
                        allowPingbacks, serviceContext);
            }
        } else {
            List<String> existingFiles = new ArrayList<>();

            for (int i = 1; i <= 5; i++) {
                String path = ParamUtil.getString(actionRequest, "existingPath" + i);

                if (Validator.isNotNull(path)) {
                    existingFiles.add(path);
                }
            }

            // Update message

            message = _mbMessageService.updateMessage(messageId, subject, body, inputStreamOVPs, existingFiles,
                    priority, allowPingbacks, serviceContext);

            if (message.isRoot()) {
                _mbThreadLocalService.updateQuestion(message.getThreadId(), question);
            }
        }

        PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

        boolean subscribe = ParamUtil.getBoolean(actionRequest, "subscribe");

        if (!preview && subscribe
                && MBMessagePermission.contains(permissionChecker, message, ActionKeys.SUBSCRIBE)) {

            _mbMessageService.subscribeMessage(message.getMessageId());
        }

        return message;
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            InputStream inputStream = inputStreamOVP.getValue();

            StreamUtil.cleanUp(inputStream);
        }
    }
}

From source file:com.liferay.tool.datamanipulator.portlet.DataManipulatorAdmin.java

License:Open Source License

public void create(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    RequestContext requestContext = new RequestContext(uploadRequest);

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

    String handlerName = ParamUtil.getString(uploadRequest, "handlerName");

    BaseHandler handler = HandlerUtil.getHandler(handlerName);

    boolean handlerIsPortalType = HandlerUtil.isPortalType(handlerName);

    long[] groupIds = requestContext.getGroupIds();

    if ((groupIds.length <= 0) || handlerIsPortalType) {
        groupIds = new long[] { themeDisplay.getScopeGroupId() };
    }/*from w w w  .  ja va2s  . c o m*/

    long[] userIds = requestContext.getUserIds();

    if ((userIds.length <= 0) || handlerIsPortalType) {
        userIds = new long[] { themeDisplay.getUserId() };
    }

    for (long groupId : groupIds) {
        for (long userId : userIds) {
            requestContext = new RequestContext(uploadRequest);

            requestContext.setGroupId(groupId);
            requestContext.setUserId(userId);

            HandlerThread handlerThread = new HandlerThread(handler, requestContext);

            handlerThread.start();
        }
    }
}

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

License:Open Source License

protected void importPages(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    String importProgressId = ParamUtil.getString(uploadPortletRequest, "importProgressId");

    ProgressTracker progressTracker = new ProgressTracker(importProgressId);

    ProgressTrackerThreadLocal.setProgressTracker(progressTracker);

    progressTracker.start(actionRequest);

    long nodeId = ParamUtil.getLong(uploadPortletRequest, "nodeId");
    String importer = ParamUtil.getString(uploadPortletRequest, "importer");

    int filesCount = ParamUtil.getInteger(uploadPortletRequest, "filesCount", 10);

    InputStream[] inputStreams = new InputStream[filesCount];

    try {//w ww.  ja  v  a2s  . co m
        for (int i = 0; i < filesCount; i++) {
            inputStreams[i] = uploadPortletRequest.getFileAsStream("file" + i);
        }

        NotificationThreadLocal.setEnabled(false);
        WikiCacheThreadLocal.setClearCache(false);

        _wikiNodeService.importPages(nodeId, importer, inputStreams, actionRequest.getParameterMap());
    } finally {
        for (InputStream inputStream : inputStreams) {
            StreamUtil.cleanUp(inputStream);
        }
    }

    _wikiCacheHelper.clearCache(nodeId);

    progressTracker.finish(actionRequest);
}

From source file:com.liferay.wiki.web.internal.WikiAttachmentsHelper.java

License:Open Source License

public void addAttachments(ActionRequest actionRequest) throws Exception {
    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
    String title = ParamUtil.getString(actionRequest, "title");

    int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<>();

    try {// w  w  w  .  java 2  s. c om
        if (numOfFiles == 0) {
            String fileName = uploadPortletRequest.getFileName("file");
            InputStream inputStream = uploadPortletRequest.getFileAsStream("file");

            if (inputStream != null) {
                ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName,
                        inputStream);

                inputStreamOVPs.add(inputStreamOVP);
            }
        } else {
            for (int i = 1; i <= numOfFiles; i++) {
                String fileName = uploadPortletRequest.getFileName("file" + i);
                InputStream inputStream = uploadPortletRequest.getFileAsStream("file" + i);

                if (inputStream == null) {
                    continue;
                }

                ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<>(fileName,
                        inputStream);

                inputStreamOVPs.add(inputStreamOVP);
            }
        }

        _wikiPageService.addPageAttachments(nodeId, title, inputStreamOVPs);
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

            InputStream inputStream = inputStreamOVP.getValue();

            StreamUtil.cleanUp(inputStream);
        }
    }
}

From source file:it.infn.ct.indigo.futuregateway.portlet.action.FGAddAppMVCActionCommand.java

License:Apache License

@Override
protected final void doProcessAction(final ActionRequest actionRequest, final ActionResponse actionResponse)
        throws Exception {

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

    String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));
    String name = ParamUtil.getString(actionRequest, "fg-app-name");
    String description = ParamUtil.getString(actionRequest, "fg-app-description");
    boolean enabled = ParamUtil.getBoolean(actionRequest, "fg-app-enabled");
    String outcome = ParamUtil.getString(actionRequest, "fg-app-outcome");
    String[] paramNames = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-name");
    String[] paramValues = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-value");
    String[] paramDescriptions = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-description");
    String[] infras = ParamUtil.getStringValues(actionRequest, "fg-app-infrastructure");
    String[] fileUrls = ParamUtil.getStringValues(actionRequest, "fg-app-file-url");
    UploadPortletRequest upr = PortalUtil.getUploadPortletRequest(actionRequest);
    File[] files = upr.getFiles("fg-app-file-update");
    String[] fileNames = upr.getFileNames("fg-app-file-update");
    Map<String, File> fileToTransfer = new HashMap<>();

    JSONObject jApp = JSONFactoryUtil.createJSONObject();
    jApp.put("name", name);
    jApp.put("description", description);
    jApp.put("enabled", enabled);
    jApp.put("outcome", outcome);

    JSONArray jParams = JSONFactoryUtil.createJSONArray();
    for (int i = 0; i < paramNames.length; i++) {
        if (Validator.isNotNull(paramNames[i])) {
            JSONObject jPar = JSONFactoryUtil.createJSONObject();
            jPar.put("name", paramNames[i]);
            jPar.put("value", paramValues[i]);
            jPar.put("description", paramDescriptions[i]);
            jParams.put(jPar);/* w  w w .  j a  va  2  s.c  om*/
        }
    }
    jApp.put("parameters", jParams);

    JSONArray jInfras = JSONFactoryUtil.createJSONArray();
    for (String in : infras) {
        jInfras.put(in);
    }
    jApp.put("infrastructures", jInfras);

    if (Validator.isNotNull(fileNames)) {
        JSONArray jFiles = JSONFactoryUtil.createJSONArray();
        for (int i = 0; i < fileNames.length; i++) {
            if (Validator.isNotNull(fileNames[i])) {
                jFiles.put(fileNames[i]);
                fileToTransfer.put(fileNames[i], files[i]);
            } else {
                if (Validator.isNotNull(fileUrls[i])) {
                    jFiles.put(fileUrls[i]);
                }
            }
        }
        jApp.put("files", jFiles);
    }
    try {
        String resourceId = fgServerManager.addResource(themeDisplay.getCompanyId(),
                FGServerConstants.APPLICATION_COLLECTION, jApp.toJSONString(), themeDisplay.getUserId());
        if (!fileToTransfer.isEmpty()) {
            fgServerManager.submitFilesResource(themeDisplay.getCompanyId(),
                    FGServerConstants.APPLICATION_COLLECTION, resourceId, fileToTransfer,
                    themeDisplay.getUserId());
        }
        sendRedirect(actionRequest, actionResponse, redirect);
    } catch (IOException io) {
        log.error(io.getMessage());
        SessionErrors.add(actionRequest, io.getClass(), io);
        try {
            Map<String, String> mapInfras = fgServerManager.getInfrastructures(themeDisplay.getCompanyId(),
                    themeDisplay.getUserId());
            actionRequest.setAttribute(FGServerConstants.INFRASTRUCTURE_COLLECTION, mapInfras);
        } catch (Exception e) {
            if (e instanceof PrincipalException) {
                SessionErrors.add(actionRequest, e.getClass());
                actionResponse.setRenderParameter("mvcPath", "/error.jsp");
            } else {
                throw new PortletException(e);
            }
        }
        actionResponse.setRenderParameter("mvcPath", "/add_application.jsp");
    }
}

From source file:it.infn.ct.indigo.futuregateway.portlet.action.FGEditAppMVCActionCommand.java

License:Apache License

@Override
protected final void doProcessAction(final ActionRequest actionRequest, final ActionResponse actionResponse)
        throws Exception {

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

    String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));
    String id = ParamUtil.getString(actionRequest, "fg-app-id");
    String name = ParamUtil.getString(actionRequest, "fg-app-name");
    String description = ParamUtil.getString(actionRequest, "fg-app-description");
    boolean enabled = ParamUtil.getBoolean(actionRequest, "fg-app-enabled");
    String outcome = ParamUtil.getString(actionRequest, "fg-app-outcome");
    String[] paramNames = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-name");
    String[] paramValues = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-value");
    String[] paramDescriptions = ParamUtil.getStringValues(actionRequest, "fg-app-parameter-description");
    String[] infras = ParamUtil.getStringValues(actionRequest, "fg-app-infrastructure");
    String[] fileUrls = ParamUtil.getStringValues(actionRequest, "fg-app-file-url");
    String[] fileOld = ParamUtil.getStringValues(actionRequest, "fg-app-file-old");
    UploadPortletRequest upr = PortalUtil.getUploadPortletRequest(actionRequest);
    InputStream[] files = upr.getFilesAsStream("fg-app-file-update", true);
    String[] fileNames = upr.getFileNames("fg-app-file-update");
    Map<String, InputStream> fileToTransfer = new HashMap<>();

    JSONObject jApp = JSONFactoryUtil.createJSONObject();
    if (!id.isEmpty()) {
        jApp.put("id", id);
    }//from w  ww.j  ava 2s  .  co m
    jApp.put("name", name);
    jApp.put("description", description);
    jApp.put("enabled", enabled);
    jApp.put("outcome", outcome);

    JSONArray jParams = JSONFactoryUtil.createJSONArray();
    for (int i = 0; i < paramNames.length; i++) {
        if (Validator.isNotNull(paramNames[i])) {
            JSONObject jPar = JSONFactoryUtil.createJSONObject();
            jPar.put("name", paramNames[i]);
            jPar.put("value", paramValues[i]);
            if (paramDescriptions.length == paramNames.length) {
                jPar.put("description", paramDescriptions[i]);
            }
            jParams.put(jPar);
        }
    }
    jApp.put("parameters", jParams);

    JSONArray jInfras = JSONFactoryUtil.createJSONArray();
    for (String in : infras) {
        jInfras.put(in);
    }
    jApp.put("infrastructures", jInfras);

    if (Validator.isNotNull(fileNames)) {
        JSONArray jFiles = JSONFactoryUtil.createJSONArray();
        for (int i = 0; i < fileNames.length; i++) {
            if (Validator.isNotNull(fileNames[i])) {
                jFiles.put(fileNames[i]);
                fileToTransfer.put(fileNames[i], files[i]);
            } else {
                if (fileUrls.length == fileNames.length && Validator.isNotNull(fileUrls[i])) {
                    jFiles.put(fileUrls[i]);
                } else {
                    if (fileOld[i].compareTo("N/A") != 0) {
                        jFiles.put(fileOld[i]);
                    }
                }
            }
        }
        jApp.put("files", jFiles);
    }
    try {
        String resourceId = fgServerManager.addResource(themeDisplay.getCompanyId(),
                FGServerConstants.APPLICATION_COLLECTION, id, jApp.toJSONString(), themeDisplay.getUserId());
        if (!fileToTransfer.isEmpty()) {
            fgServerManager.submitFilesResource(themeDisplay.getCompanyId(),
                    FGServerConstants.APPLICATION_COLLECTION, resourceId, fileToTransfer,
                    themeDisplay.getUserId());
        }
        sendRedirect(actionRequest, actionResponse, redirect);
    } catch (IOException io) {
        log.error(io.getMessage());
        SessionErrors.add(actionRequest, io.getClass(), io);
        try {
            Map<String, String> mapInfras = fgServerManager.getInfrastructures(themeDisplay.getCompanyId(),
                    themeDisplay.getUserId());
            actionRequest.setAttribute(FGServerConstants.INFRASTRUCTURE_COLLECTION, mapInfras);
        } catch (Exception e) {
            if (e instanceof PrincipalException) {
                SessionErrors.add(actionRequest, e.getClass());
                actionResponse.setRenderParameter("mvcPath", "/error.jsp");
            } else {
                throw new PortletException(e);
            }
        }
        actionResponse.setRenderParameter("mvcPath", "/add_application.jsp");
    }
}