KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > impl > ActionResponseImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
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 /*
17
18  */

19
20 package org.apache.pluto.core.impl;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.portlet.ActionResponse;
27 import javax.portlet.PortletMode;
28 import javax.portlet.PortletModeException;
29 import javax.portlet.WindowState;
30 import javax.portlet.WindowStateException;
31
32 import org.apache.pluto.core.InternalActionResponse;
33 import org.apache.pluto.om.window.PortletWindow;
34 import org.apache.pluto.services.information.DynamicInformationProvider;
35 import org.apache.pluto.services.information.InformationProviderAccess;
36 import org.apache.pluto.services.information.ResourceURLProvider;
37 import org.apache.pluto.util.StringUtils;
38
39 public class ActionResponseImpl extends PortletResponseImpl
40 implements ActionResponse, InternalActionResponse {
41
42     /**
43      * Is it still allowed to invoke the method sendRedirect() ?
44      */

45     boolean redirectAllowed = true;
46
47     private boolean redirected;
48     private String JavaDoc redirectLocation;
49
50     private Map JavaDoc renderParameters = new HashMap JavaDoc();
51     private WindowState windowState = null;
52     private PortletMode portletMode = null;
53
54     private DynamicInformationProvider provider;
55
56
57     public ActionResponseImpl(PortletWindow portletWindow,
58                               javax.servlet.http.HttpServletRequest JavaDoc servletRequest,
59                               javax.servlet.http.HttpServletResponse JavaDoc servletResponse)
60     {
61         super(portletWindow, servletRequest, servletResponse);
62
63         provider = InformationProviderAccess.getDynamicProvider(getHttpServletRequest());
64
65     }
66
67     // javax.portlet.ActionResponse ---------------------------------------------------------------
68
public void setWindowState (WindowState windowState) throws WindowStateException
69     {
70         if (redirected) {
71             throw new IllegalStateException JavaDoc("it is not allowed to invoke setWindowState after sendRedirect has been called");
72         }
73
74         if (provider.isWindowStateAllowed(windowState)) {
75             this.windowState = windowState;
76         } else {
77             throw new WindowStateException("Can't set this WindowState",windowState);
78         }
79         redirectAllowed = false;
80     }
81     
82     public void setPortletMode (PortletMode portletMode) throws PortletModeException
83     {
84         if (redirected) {
85             throw new IllegalStateException JavaDoc("it is not allowed to invoke setPortletMode after sendRedirect has been called");
86         }
87
88         // check if portal supports portlet mode
89
boolean supported = provider.isPortletModeAllowed(portletMode);
90
91         // check if portlet supports portlet mode as well
92
if (supported)
93         {
94             supported = PortletModeHelper.isPortletModeAllowedByPortlet(getInternalPortletWindow(), portletMode);
95         }
96
97         // if porlet mode is allowed
98
if (supported) {
99             this.portletMode = portletMode;
100         } else
101             throw new PortletModeException("Can't set this PortletMode",portletMode);
102
103         redirectAllowed = false;
104
105     }
106
107     public void sendRedirect(String JavaDoc location) throws java.io.IOException JavaDoc
108     {
109         if (redirectAllowed) {
110             if (location != null) {
111                 javax.servlet.http.HttpServletResponse JavaDoc redirectResponse = _getHttpServletResponse();
112                 while (redirectResponse instanceof javax.servlet.http.HttpServletResponseWrapper JavaDoc) {
113                     redirectResponse = (javax.servlet.http.HttpServletResponse JavaDoc)
114                                        ((javax.servlet.http.HttpServletResponseWrapper JavaDoc)redirectResponse).getResponse();
115                 }
116                 ResourceURLProvider provider = InformationProviderAccess.getDynamicProvider(getHttpServletRequest()).getResourceURLProvider(getInternalPortletWindow());
117                 if (location.indexOf("://") != -1) {
118                     provider.setAbsoluteURL(location);
119                 } else if (location.startsWith("/")) {
120                     provider.setFullPath(location);
121                 } else {
122                     throw new IllegalArgumentException JavaDoc("Only absolute and full path URLs are allowed. The relative path '" + location+"' is not valid.");
123                 }
124                 location = redirectResponse.encodeRedirectURL(provider.toString());
125                 //redirectResponse.sendRedirect(location);
126
redirectLocation = location;
127                 redirected = true;
128             }
129         } else
130             throw new java.lang.IllegalStateException JavaDoc("Can't invoke sendRedirect() after certain methods have been called");
131
132     }
133     
134     public void setRenderParameters(Map JavaDoc parameters)
135     {
136         if (redirected) {
137             throw new IllegalStateException JavaDoc("Can't invoke setRenderParameters() after sendRedirect() has been called");
138         }
139         if (parameters == null) {
140             throw new IllegalArgumentException JavaDoc("Render parameters must not be null.");
141         }
142         for (Iterator JavaDoc iter = parameters.entrySet().iterator(); iter.hasNext();) {
143             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
144             if (!(entry.getKey() instanceof String JavaDoc)) {
145                 throw new IllegalArgumentException JavaDoc("Key must not be null and of type java.lang.String.");
146             }
147             if (!(entry.getValue() instanceof String JavaDoc[])) {
148                 throw new IllegalArgumentException JavaDoc("Value must not be null and of type java.lang.String[].");
149             }
150         }
151
152         renderParameters = StringUtils.copyParameters(parameters);
153
154         redirectAllowed = false;
155     }
156     
157     public void setRenderParameter(String JavaDoc key, String JavaDoc value)
158     {
159         if (redirected) {
160             throw new IllegalStateException JavaDoc("Can't invoke setRenderParameter() after sendRedirect() has been called");
161         }
162
163         if ((key == null) || (value == null)) {
164             throw new IllegalArgumentException JavaDoc("Render parameter key or value must not be null.");
165         }
166
167         renderParameters.put(key, new String JavaDoc[] {value});
168
169         redirectAllowed = false;
170     }
171     
172     public void setRenderParameter(String JavaDoc key, String JavaDoc[] values)
173     {
174         if (redirected) {
175             throw new IllegalStateException JavaDoc("Can't invoke setRenderParameter() after sendRedirect() has been called");
176         }
177
178         if (key == null || values == null || values.length == 0) {
179             throw new IllegalArgumentException JavaDoc("Render parameter key or value must not be null or values be an empty array.");
180         }
181
182         renderParameters.put(key, StringUtils.copy(values));
183
184         redirectAllowed = false;
185     }
186     // --------------------------------------------------------------------------------------------
187

188     // org.apache.pluto.core.InternalActionResponse implementation --------------------------------
189
public Map JavaDoc getRenderParameters()
190     {
191         return renderParameters;
192     }
193
194     public PortletMode getChangedPortletMode()
195     {
196         return this.portletMode;
197     }
198
199     public WindowState getChangedWindowState()
200     {
201         return this.windowState;
202     }
203
204     public String JavaDoc getRedirectLocation() {
205         return redirectLocation;
206     }
207     // --------------------------------------------------------------------------------------------
208
}
209
Popular Tags