com.liferay.portlet.portletconfiguration.action.EditConfigurationAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.portletconfiguration.action.EditConfigurationAction.java

Source

/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.portletconfiguration.action;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.portlet.ResourceServingConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Portlet;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.WebKeys;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Brian Wing Shun Chan
 * @author Raymond Aug
 */
public class EditConfigurationAction extends PortletAction {

    @Override
    public void processAction(ActionMapping actionMapping, ActionForm actionForm, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        Portlet portlet = null;

        try {
            portlet = ActionUtil.getPortlet(actionRequest);
        } catch (PrincipalException pe) {
            SessionErrors.add(actionRequest, PrincipalException.class.getName());

            setForward(actionRequest, "portlet.portlet_configuration.error");

            return;
        }

        actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);

        ConfigurationAction configurationAction = getConfigurationAction(portlet);

        if (configurationAction == null) {
            return;
        }

        configurationAction.processAction(portletConfig, actionRequest, actionResponse);
    }

    @Override
    public ActionForward render(ActionMapping actionMapping, ActionForm actionForm, PortletConfig portletConfig,
            RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {

        Portlet portlet = null;

        try {
            portlet = ActionUtil.getPortlet(renderRequest);
        } catch (PrincipalException pe) {
            SessionErrors.add(renderRequest, PrincipalException.class.getName());

            return actionMapping.findForward("portlet.portlet_configuration.error");
        }

        renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);

        renderResponse.setTitle(ActionUtil.getTitle(portlet, renderRequest));

        ConfigurationAction configurationAction = getConfigurationAction(portlet);

        if (configurationAction != null) {
            String path = configurationAction.render(portletConfig, renderRequest, renderResponse);

            if (_log.isDebugEnabled()) {
                _log.debug("Configuration action returned render path " + path);
            }

            if (Validator.isNotNull(path)) {
                renderRequest.setAttribute(WebKeys.CONFIGURATION_ACTION_PATH, path);
            } else {
                _log.error("Configuration action returned a null path");
            }
        }

        return actionMapping
                .findForward(getForward(renderRequest, "portlet.portlet_configuration.edit_configuration"));
    }

    @Override
    public void serveResource(ActionMapping actionMapping, ActionForm actionForm, PortletConfig portletConfig,
            ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception {

        Portlet portlet = null;

        try {
            portlet = ActionUtil.getPortlet(resourceRequest);
        } catch (PrincipalException pe) {
            return;
        }

        resourceRequest = ActionUtil.getWrappedResourceRequest(resourceRequest, null);

        ResourceServingConfigurationAction resourceServingConfigurationAction = (ResourceServingConfigurationAction) getConfigurationAction(
                portlet);

        if (resourceServingConfigurationAction == null) {
            return;
        }

        resourceServingConfigurationAction.serveResource(portletConfig, resourceRequest, resourceResponse);
    }

    protected ConfigurationAction getConfigurationAction(Portlet portlet) throws Exception {

        if (portlet == null) {
            return null;
        }

        ConfigurationAction configurationAction = portlet.getConfigurationActionInstance();

        if (configurationAction == null) {
            _log.error("Configuration action for portlet " + portlet.getPortletId() + " is null");
        }

        return configurationAction;
    }

    private static Log _log = LogFactoryUtil.getLog(EditConfigurationAction.class);

}