001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.action.portlet;
017:
018: import javax.portlet.ActionResponse;
019: import javax.portlet.PortletMode;
020:
021: import org.springframework.util.Assert;
022: import org.springframework.util.ClassUtils;
023: import org.springframework.webflow.action.AbstractAction;
024: import org.springframework.webflow.context.portlet.PortletExternalContext;
025: import org.springframework.webflow.execution.Event;
026: import org.springframework.webflow.execution.RequestContext;
027:
028: /**
029: * Action implementation that changes a PortletResponse mode. The action only
030: * generates the
031: * {@link org.springframework.webflow.action.AbstractAction#success()} event.
032: * All error cases result in an exception being thrown.
033: * <p>
034: * This class is usefull when you want to change the current PortletMode before
035: * entering a specific state, e.g. it can be the first state in a subflow.
036: * <p>
037: * Note: if you can, change the PortletMode using Portlet URLs (PortletURL class
038: * or portlet TAG).
039: *
040: * @author J.Enrique Ruiz
041: * @author Cesar Ordinana
042: * @author Erwin Vervaet
043: */
044: public class SetPortletModeAction extends AbstractAction {
045:
046: /**
047: * The portlet mode to set can be specified in an action state action
048: * attribute with this name ("portletMode").
049: */
050: public static final String PORTLET_MODE_ATTRIBUTE = "portletMode";
051:
052: /**
053: * The default portlet mode. Default is "view".
054: */
055: private PortletMode portletMode = PortletMode.VIEW;
056:
057: /**
058: * Returns the mode that will be set in the response.
059: */
060: public PortletMode getPortletMode() {
061: return portletMode;
062: }
063:
064: /**
065: * Sets the mode that will be set in the response.
066: */
067: public void setPortletMode(PortletMode portletMode) {
068: Assert.notNull(portletMode,
069: "The portlet mode is required and cannot be null");
070: this .portletMode = portletMode;
071: }
072:
073: /**
074: * Sets the PortletMode.
075: * @param context the action execution context, for accessing and setting
076: * data in "flow scope" or "request scope"
077: * @return the action result event
078: * @throws Exception an <b>unrecoverable</b> exception occured, either
079: * checked or unchecked
080: */
081: protected Event doExecute(RequestContext context) throws Exception {
082: Assert.isInstanceOf(PortletExternalContext.class, context
083: .getExternalContext(), "'"
084: + ClassUtils.getShortName(this .getClass())
085: + "' can only work with 'PortletExternalContext': ");
086: PortletExternalContext portletContext = (PortletExternalContext) context
087: .getExternalContext();
088: if (portletContext.getResponse() instanceof ActionResponse) {
089: PortletMode mode = (PortletMode) context.getAttributes()
090: .get(PORTLET_MODE_ATTRIBUTE, PortletMode.class,
091: getPortletMode());
092: ((ActionResponse) portletContext.getResponse())
093: .setPortletMode(mode);
094: return success();
095: } else {
096: // portlet mode and the window state can be changed through
097: // ActionResponse only, if this is not the case, it means that this
098: // action has been invoked directly in a RenderRequest
099: throw new IllegalStateException(
100: "SetPortletModeAction can only be invoked within a Action request -- "
101: + "make sure you are not invoking it in a RenderRequest");
102: }
103: }
104: }
|