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

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

Introduction

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

Prototype

public static String escapeRedirect(String url) 

Source Link

Document

Escapes the URL for use in a redirect and checks that security settings allow the URL is allowed for redirects.

Usage

From source file:com.liferay.akismet.hook.action.AkismetEditDiscussionAction.java

License:Open Source License

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

    try {//from  w  ww.j  ava2s  .c  o m
        checkPermission(request);

        updateStatus(request, response);

        String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(request, "redirect"));

        response.sendRedirect(redirect);
    } catch (Exception e) {
        PortalUtil.sendError(e, request, response);
    }

    return null;
}

From source file:com.liferay.blogs.web.internal.portlet.action.EditEntryMVCActionCommand.java

License:Open Source License

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

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

    try {//  ww  w  .j  a va 2  s .  com
        BlogsEntry entry = null;
        List<BlogsEntryAttachmentFileEntryReference> blogsEntryAttachmentFileEntryReferences = null;

        UploadException uploadException = (UploadException) actionRequest
                .getAttribute(WebKeys.UPLOAD_EXCEPTION);

        if (uploadException != null) {
            Throwable cause = uploadException.getCause();

            if (uploadException.isExceededFileSizeLimit()) {
                throw new FileSizeException(cause);
            }

            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
                throw new LiferayFileItemException(cause);
            }

            if (uploadException.isExceededUploadRequestSizeLimit()) {
                throw new UploadRequestSizeException(cause);
            }

            throw new PortalException(cause);
        } else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {

            Callable<Object[]> updateEntryCallable = new UpdateEntryCallable(actionRequest);

            Object[] returnValue = TransactionInvokerUtil.invoke(_transactionConfig, updateEntryCallable);

            entry = (BlogsEntry) returnValue[0];
            blogsEntryAttachmentFileEntryReferences = (List<BlogsEntryAttachmentFileEntryReference>) returnValue[1];
        } else if (cmd.equals(Constants.DELETE)) {
            deleteEntries(actionRequest, false);
        } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
            deleteEntries(actionRequest, true);
        } else if (cmd.equals(Constants.RESTORE)) {
            restoreTrashEntries(actionRequest);
        } else if (cmd.equals(Constants.SUBSCRIBE)) {
            subscribe(actionRequest);
        } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
            unsubscribe(actionRequest);
        }

        String redirect = ParamUtil.getString(actionRequest, "redirect");
        String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);

        int workflowAction = ParamUtil.getInteger(actionRequest, "workflowAction",
                WorkflowConstants.ACTION_SAVE_DRAFT);

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

        if (ajax) {
            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

            for (BlogsEntryAttachmentFileEntryReference blogsEntryAttachmentFileEntryReference : blogsEntryAttachmentFileEntryReferences) {

                JSONObject blogsEntryFileEntryReferencesJSONObject = JSONFactoryUtil.createJSONObject();

                blogsEntryFileEntryReferencesJSONObject.put("attributeDataImageId",
                        EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
                blogsEntryFileEntryReferencesJSONObject.put("fileEntryId", String.valueOf(
                        blogsEntryAttachmentFileEntryReference.getTempBlogsEntryAttachmentFileEntryId()));
                blogsEntryFileEntryReferencesJSONObject.put("fileEntryUrl",
                        PortletFileRepositoryUtil.getPortletFileEntryURL(null,
                                blogsEntryAttachmentFileEntryReference.getBlogsEntryAttachmentFileEntry(),
                                StringPool.BLANK));

                jsonArray.put(blogsEntryFileEntryReferencesJSONObject);
            }

            jsonObject.put("blogsEntryAttachmentReferences", jsonArray);
            jsonObject.put("coverImageFileEntryId", entry.getCoverImageFileEntryId());
            jsonObject.put("entryId", entry.getEntryId());
            jsonObject.put("redirect", redirect);

            JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);

            return;
        }

        if ((entry != null) && (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {

            redirect = getSaveAndContinueRedirect(actionRequest, entry, redirect);

            sendRedirect(actionRequest, actionResponse, redirect);
        } else {
            WindowState windowState = actionRequest.getWindowState();

            if (!windowState.equals(LiferayWindowState.POP_UP)) {
                sendRedirect(actionRequest, actionResponse, redirect);
            } else {
                redirect = PortalUtil.escapeRedirect(redirect);

                if (Validator.isNotNull(redirect)) {
                    if (cmd.equals(Constants.ADD) && (entry != null)) {
                        String namespace = PortalUtil.getPortletNamespace(portletId);

                        redirect = HttpUtil.addParameter(redirect, namespace + "className",
                                BlogsEntry.class.getName());
                        redirect = HttpUtil.addParameter(redirect, namespace + "classPK", entry.getEntryId());
                    }

                    actionRequest.setAttribute(WebKeys.REDIRECT, redirect);
                }
            }
        }
    } catch (AssetCategoryException | AssetTagException e) {
        SessionErrors.add(actionRequest, e.getClass(), e);

        actionResponse.setRenderParameter("mvcRenderCommandName", "/blogs/edit_entry");

        hideDefaultSuccessMessage(actionRequest);
    } catch (EntryContentException | EntryCoverImageCropException | EntryDescriptionException
            | EntryDisplayDateException | EntrySmallImageNameException | EntrySmallImageScaleException
            | EntryTitleException | EntryUrlTitleException | FileSizeException | LiferayFileItemException
            | SanitizerException | UploadRequestSizeException e) {

        SessionErrors.add(actionRequest, e.getClass());

        actionResponse.setRenderParameter("mvcRenderCommandName", "/blogs/edit_entry");

        hideDefaultSuccessMessage(actionRequest);
    } catch (NoSuchEntryException | PrincipalException e) {
        SessionErrors.add(actionRequest, e.getClass());

        actionResponse.setRenderParameter("mvcPath", "/blogs/error.jsp");

        hideDefaultSuccessMessage(actionRequest);
    } catch (Throwable t) {
        _log.error(t, t);

        actionResponse.setRenderParameter("mvcPath", "/blogs/error.jsp");

        hideDefaultSuccessMessage(actionRequest);
    }
}

From source file:com.liferay.bookmarks.web.internal.portlet.action.EditEntryMVCActionCommand.java

License:Open Source License

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

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

    try {/*  w  w w .j a  v a 2 s  .  co m*/
        BookmarksEntry entry = null;

        if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
            entry = updateEntry(actionRequest);
        } else if (cmd.equals(Constants.DELETE)) {
            deleteEntry(actionRequest, false);
        } else if (cmd.equals(Constants.MOVE)) {
            moveEntries(actionRequest);
        } else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
            deleteEntry(actionRequest, true);
        } else if (cmd.equals(Constants.RESTORE)) {
            restoreTrashEntries(actionRequest);
        } else if (cmd.equals(Constants.SUBSCRIBE)) {
            subscribeEntry(actionRequest);
        } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
            unsubscribeEntry(actionRequest);
        }

        WindowState windowState = actionRequest.getWindowState();

        if (windowState.equals(LiferayWindowState.POP_UP)) {
            String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

            if (Validator.isNotNull(redirect)) {
                if (cmd.equals(Constants.ADD) && (entry != null)) {
                    String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);

                    String namespace = PortalUtil.getPortletNamespace(portletId);

                    redirect = HttpUtil.addParameter(redirect, namespace + "className",
                            BookmarksEntry.class.getName());
                    redirect = HttpUtil.addParameter(redirect, namespace + "classPK", entry.getEntryId());
                }

                actionRequest.setAttribute(WebKeys.REDIRECT, redirect);
            }
        }
    } catch (Exception e) {
        if (e instanceof NoSuchEntryException || e instanceof PrincipalException) {

            SessionErrors.add(actionRequest, e.getClass());

            actionResponse.setRenderParameter("mvcPath", "/bookmarks/error.jsp");
        } else if (e instanceof EntryURLException || e instanceof NoSuchFolderException) {

            SessionErrors.add(actionRequest, e.getClass());
        } else if (e instanceof AssetCategoryException || e instanceof AssetTagException) {

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

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

License:Open Source License

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

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

    MBMessage message = null;/*from w ww .  j a  v  a 2  s. com*/

    try {
        UploadException uploadException = (UploadException) actionRequest
                .getAttribute(WebKeys.UPLOAD_EXCEPTION);

        if (uploadException != null) {
            Throwable cause = uploadException.getCause();

            if (uploadException.isExceededFileSizeLimit()) {
                throw new FileSizeException(cause);
            }

            if (uploadException.isExceededLiferayFileItemSizeLimit()) {
                throw new LiferayFileItemException(cause);
            }

            if (uploadException.isExceededUploadRequestSizeLimit()) {
                throw new UploadRequestSizeException(cause);
            }

            throw new PortalException(cause);
        } else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {

            message = updateMessage(actionRequest, actionResponse);
        } else if (cmd.equals(Constants.ADD_ANSWER)) {
            addAnswer(actionRequest);
        } else if (cmd.equals(Constants.DELETE)) {
            deleteMessage(actionRequest);
        } else if (cmd.equals(Constants.DELETE_ANSWER)) {
            deleteAnswer(actionRequest);
        } else if (cmd.equals(Constants.LOCK)) {
            lockThreads(actionRequest);
        } else if (cmd.equals(Constants.SUBSCRIBE)) {
            subscribeMessage(actionRequest);
        } else if (cmd.equals(Constants.UNLOCK)) {
            unlockThreads(actionRequest);
        } else if (cmd.equals(Constants.UNSUBSCRIBE)) {
            unsubscribeMessage(actionRequest);
        }

        if (Validator.isNotNull(cmd)) {
            WindowState windowState = actionRequest.getWindowState();

            if (!windowState.equals(LiferayWindowState.POP_UP)) {
                String redirect = getRedirect(actionRequest, actionResponse, message);

                sendRedirect(actionRequest, actionResponse, redirect);
            } else {
                String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

                if (Validator.isNotNull(redirect)) {
                    actionResponse.sendRedirect(redirect);
                }
            }
        }
    } catch (NoSuchMessageException | PrincipalException | RequiredMessageException e) {

        SessionErrors.add(actionRequest, e.getClass());

        actionResponse.setRenderParameter("mvcPath", "/message_boards/error.jsp");
    } catch (AntivirusScannerException | CaptchaConfigurationException | CaptchaTextException
            | DuplicateFileEntryException | FileExtensionException | FileNameException | FileSizeException
            | LiferayFileItemException | LockedThreadException | MessageBodyException | MessageSubjectException
            | SanitizerException | UploadRequestSizeException e) {

        if (e instanceof AntivirusScannerException) {
            SessionErrors.add(actionRequest, e.getClass(), e);
        } else {
            SessionErrors.add(actionRequest, e.getClass());
        }
    } catch (AssetCategoryException | AssetTagException e) {
        SessionErrors.add(actionRequest, e.getClass(), e);
    } catch (Exception e) {
        Throwable cause = e.getCause();

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

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

License:Open Source License

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

    try {//from   w w w . ja v  a  2s  .  c  o  m
        moveCategory(actionRequest, actionResponse);

        String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

        if (Validator.isNotNull(redirect)) {
            actionResponse.sendRedirect(redirect);
        }
    } catch (PrincipalException pe) {
        SessionErrors.add(actionRequest, pe.getClass());
    }
}

From source file:com.liferay.staging.bar.web.internal.portlet.action.ActionUtil.java

License:Open Source License

public static void addLayoutBranchSessionMessages(ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException {

    if (SessionErrors.isEmpty(actionRequest)) {
        SessionMessages.add(actionRequest,
                PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
                StagingBarPortletKeys.STAGING_BAR);

        Map<String, String> data = new HashMap<>();

        data.put("preventNotification", Boolean.TRUE.toString());

        SessionMessages.add(actionRequest,
                PortalUtil.getPortletId(actionRequest) + SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA, data);
    }//  ww  w.  jav a 2  s.  co m

    String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

    actionResponse.sendRedirect(redirect);
}

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.FGAddInfraMVCActionCommand.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-infra-name");
    String description = ParamUtil.getString(actionRequest, "fg-infra-description");
    boolean enabled = ParamUtil.getBoolean(actionRequest, "fg-infra-enabled");
    boolean virtual = ParamUtil.getBoolean(actionRequest, "fg-infra-virtual");
    String[] paramNames = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-name");
    String[] paramValues = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-value");
    String[] paramDescriptions = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-description");

    JSONObject jInfra = JSONFactoryUtil.createJSONObject();
    jInfra.put("name", name);
    jInfra.put("description", description);
    jInfra.put("enabled", enabled);
    jInfra.put("virtual", virtual);
    JSONArray jParams = JSONFactoryUtil.createJSONArray();
    for (int i = 0; i < paramNames.length; i++) {
        JSONObject jPar = JSONFactoryUtil.createJSONObject();
        jPar.put("name", paramNames[i]);
        jPar.put("value", paramValues[i]);
        jPar.put("description", paramDescriptions[i]);
        jParams.put(jPar);/*w ww .j a v  a  2s  . co  m*/
    }
    jInfra.put("parameters", jParams);

    try {
        fgServerManager.addResource(themeDisplay.getCompanyId(), FGServerConstants.INFRASTRUCTURE_COLLECTION,
                jInfra.toJSONString(), themeDisplay.getUserId());
        sendRedirect(actionRequest, actionResponse, redirect);
    } catch (IOException io) {
        log.error(io.getMessage());
        SessionErrors.add(actionRequest, io.getClass(), io);
        actionResponse.setRenderParameter("mvcPath", "/add_infrastructure.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);
    }/* w  w w.  ja v a  2  s.c o  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");
    }
}

From source file:it.infn.ct.indigo.futuregateway.portlet.action.FGEditInfraMVCActionCommand.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-infra-id");
    String name = ParamUtil.getString(actionRequest, "fg-infra-name");
    String description = ParamUtil.getString(actionRequest, "fg-infra-description");
    boolean enabled = ParamUtil.getBoolean(actionRequest, "fg-infra-enabled");
    boolean virtual = ParamUtil.getBoolean(actionRequest, "fg-infra-virtual");
    String[] paramNames = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-name");
    String[] paramValues = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-value");
    String[] paramDescriptions = ParamUtil.getStringValues(actionRequest, "fg-infra-parameter-description");

    JSONObject jInfra = JSONFactoryUtil.createJSONObject();
    if (!id.isEmpty()) {
        jInfra.put("id", id);
    }/* w  w  w  .  j  a va 2s . c o  m*/
    jInfra.put("name", name);
    jInfra.put("description", description);
    jInfra.put("enabled", enabled);
    jInfra.put("virtual", virtual);
    JSONArray jParams = JSONFactoryUtil.createJSONArray();
    for (int i = 0; i < paramNames.length; 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);
    }
    jInfra.put("parameters", jParams);

    try {
        fgServerManager.addResource(themeDisplay.getCompanyId(), FGServerConstants.INFRASTRUCTURE_COLLECTION,
                id, jInfra.toJSONString(), themeDisplay.getUserId());
        sendRedirect(actionRequest, actionResponse, redirect);
    } catch (IOException io) {
        log.error(io.getMessage());
        SessionErrors.add(actionRequest, io.getClass(), io);
        actionResponse.setRenderParameter("mvcPath", "/add_infrastructure.jsp");
    }
}