KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > action > portlet > SetPortletModeAction


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.action.portlet;
17
18 import javax.portlet.ActionResponse;
19 import javax.portlet.PortletMode;
20
21 import org.springframework.util.Assert;
22 import org.springframework.util.ClassUtils;
23 import org.springframework.webflow.action.AbstractAction;
24 import org.springframework.webflow.context.portlet.PortletExternalContext;
25 import org.springframework.webflow.execution.Event;
26 import org.springframework.webflow.execution.RequestContext;
27
28 /**
29  * Action implementation that changes a PortletResponse mode. The action only
30  * generates the
31  * {@link org.springframework.webflow.action.AbstractAction#success()} event.
32  * All error cases result in an exception being thrown.
33  * <p>
34  * This class is usefull when you want to change the current PortletMode before
35  * entering a specific state, e.g. it can be the first state in a subflow.
36  * <p>
37  * Note: if you can, change the PortletMode using Portlet URLs (PortletURL class
38  * or portlet TAG).
39  *
40  * @author J.Enrique Ruiz
41  * @author César Ordiñana
42  * @author Erwin Vervaet
43  */

44 public class SetPortletModeAction extends AbstractAction {
45
46     /**
47      * The portlet mode to set can be specified in an action state action
48      * attribute with this name ("portletMode").
49      */

50     public static final String JavaDoc PORTLET_MODE_ATTRIBUTE = "portletMode";
51
52     /**
53      * The default portlet mode. Default is "view".
54      */

55     private PortletMode portletMode = PortletMode.VIEW;
56
57     /**
58      * Returns the mode that will be set in the response.
59      */

60     public PortletMode getPortletMode() {
61         return portletMode;
62     }
63
64     /**
65      * Sets the mode that will be set in the response.
66      */

67     public void setPortletMode(PortletMode portletMode) {
68         Assert.notNull(portletMode, "The portlet mode is required and cannot be null");
69         this.portletMode = portletMode;
70     }
71
72     /**
73      * Sets the PortletMode.
74      * @param context the action execution context, for accessing and setting
75      * data in "flow scope" or "request scope"
76      * @return the action result event
77      * @throws Exception an <b>unrecoverable</b> exception occured, either
78      * checked or unchecked
79      */

80     protected Event doExecute(RequestContext context) throws Exception JavaDoc {
81         Assert.isInstanceOf(PortletExternalContext.class, context.getExternalContext(), "'"
82                 + ClassUtils.getShortName(this.getClass()) + "' can only work with 'PortletExternalContext': ");
83         PortletExternalContext portletContext = (PortletExternalContext)context.getExternalContext();
84         if (portletContext.getResponse() instanceof ActionResponse) {
85             PortletMode mode =
86                 (PortletMode)context.getAttributes().get(PORTLET_MODE_ATTRIBUTE, PortletMode.class, getPortletMode());
87             ((ActionResponse)portletContext.getResponse()).setPortletMode(mode);
88             return success();
89         }
90         else {
91             // portlet mode and the window state can be changed through
92
// ActionResponse only, if this is not the case, it means that this
93
// action has been invoked directly in a RenderRequest
94
throw new IllegalStateException JavaDoc(
95                     "SetPortletModeAction can only be invoked within a Action request -- " +
96                     "make sure you are not invoking it in a RenderRequest");
97         }
98     }
99 }
Popular Tags