KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > portlet > ActionResponse


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.environment.portlet;
17
18 import org.apache.cocoon.util.NetUtils;
19
20 import org.apache.avalon.framework.CascadingRuntimeException;
21
22 import javax.portlet.PortletMode;
23 import javax.portlet.PortletModeException;
24 import javax.portlet.PortletPreferences;
25 import javax.portlet.WindowState;
26 import javax.portlet.WindowStateException;
27
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * Implements the {@link org.apache.cocoon.environment.Response} interface for
35  * the JSR-168 (Portlet) environment.
36  *
37  * @author <a HREF="mailto:vadim.gritsenko@dc.gov">Vadim Gritsenko</a>
38  * @version CVS $Id: ActionResponse.java 123825 2004-12-31 21:18:01Z antonio $
39  */

40 public final class ActionResponse extends PortletResponse {
41
42     private String JavaDoc uri;
43     private ActionRequest request;
44
45     /**
46      * Creates a ActionResponse based on a real
47      * {@link ActionResponse} object
48      */

49     protected ActionResponse(javax.portlet.ActionResponse response,
50                              PortletPreferences preferences,
51                              ActionRequest request,
52                              String JavaDoc uri) {
53         super(response, preferences);
54         this.request = request;
55         this.uri = uri;
56     }
57
58     // Response API methods
59

60     /**
61      *
62      * @see PortletEnvironment#HEADER_PORTLET_MODE
63      * @see PortletEnvironment#HEADER_WINDOW_STATE
64      */

65     public void addHeader(String JavaDoc name, String JavaDoc value) {
66         if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
67             try {
68                 this.getActionResponse().setPortletMode(new PortletMode(value));
69             } catch (PortletModeException e) {
70                 throw new CascadingRuntimeException("Cant set portlet mode '" + value + "'", e);
71             }
72         } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
73             try {
74                 this.getActionResponse().setWindowState(new WindowState(value));
75             } catch (WindowStateException e) {
76                 throw new CascadingRuntimeException("Cant set window state '" + value + "'", e);
77             }
78         } else {
79             super.addHeader(name, value);
80         }
81     }
82
83     /**
84      *
85      * @see PortletEnvironment#HEADER_PORTLET_MODE
86      * @see PortletEnvironment#HEADER_WINDOW_STATE
87      */

88     public void setHeader(String JavaDoc name, String JavaDoc value) {
89         if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) {
90             try {
91                 this.getActionResponse().setPortletMode(new PortletMode(value));
92             } catch (PortletModeException e) {
93                 throw new CascadingRuntimeException("Cant set portlet mode '" + value + "'", e);
94             }
95         } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) {
96             try {
97                 this.getActionResponse().setWindowState(new WindowState(value));
98             } catch (WindowStateException e) {
99                 throw new CascadingRuntimeException("Cant set window state '" + value + "'", e);
100             }
101         } else {
102             super.setHeader(name, value);
103         }
104     }
105
106     /**
107      * Implements redirect.
108      *
109      * Redirects to self (starting with the question mark) are processed
110      * differently from other redirects: redirect parameters are parsed and
111      * set on response using {@link ActionResponse#setRenderParameter(String, String)}
112      * method, {@link ActionResponse#sendRedirect(String)} method is not called.
113      *
114      * @param location
115      * @throws IOException
116      */

117     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc {
118         String JavaDoc servletPath = this.request.getServletPath();
119
120         // Strip off parameters
121
Map JavaDoc parameters = new HashMap JavaDoc(7);
122         String JavaDoc absLoc = NetUtils.deparameterize(location, parameters);
123
124         // Absolutize
125
if (absLoc.length() > 0) {
126             String JavaDoc base = NetUtils.getPath(uri);
127             absLoc = NetUtils.absolutize(base, absLoc);
128             absLoc = NetUtils.normalize(absLoc);
129         } else {
130             absLoc = uri;
131         }
132
133         // Redirect within the portlet?
134
if (absLoc.startsWith(servletPath)) {
135             String JavaDoc pathInfo = absLoc.substring(servletPath.length());
136
137             for (Iterator JavaDoc i = parameters.entrySet().iterator(); i.hasNext();) {
138                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
139                 String JavaDoc name = (String JavaDoc)me.getKey();
140                 String JavaDoc value = (String JavaDoc)me.getValue();
141                 getActionResponse().setRenderParameter(name, value);
142             }
143             getActionResponse().setRenderParameter(PortletEnvironment.PARAMETER_PATH_INFO, pathInfo);
144         } else {
145             getActionResponse().sendRedirect(location);
146         }
147
148 /*
149         if (location.startsWith("?")) {
150             Map parameters = new HashMap(7);
151             NetUtils.deparameterize(location, parameters);
152             for (Iterator i = parameters.keySet().iterator(); i.hasNext();) {
153                 String name = (String)i.next();
154                 String value = (String)parameters.get(name);
155                 getActionResponse().setRenderParameter(name, value);
156             }
157         } else {
158             getActionResponse().sendRedirect(location);
159         }
160 */

161     }
162
163     // ActionResponse API methods
164

165     /**
166      * Type cast portletResponse to {@link ActionResponse}
167      *
168      * @return type casted portletResponse
169      */

170     public javax.portlet.ActionResponse getActionResponse() {
171         return (javax.portlet.ActionResponse) getPortletResponse();
172     }
173
174     public void setPortletMode(PortletMode mode) throws PortletModeException {
175         getActionResponse().setPortletMode(mode);
176     }
177
178     public void setRenderParameter(String JavaDoc key, String JavaDoc value) {
179         getActionResponse().setRenderParameter(key, value);
180     }
181
182     public void setRenderParameter(String JavaDoc key, String JavaDoc[] values) {
183         getActionResponse().setRenderParameter(key, values);
184     }
185
186     public void setRenderParameters(Map JavaDoc parameters) {
187         getActionResponse().setRenderParameters(parameters);
188     }
189
190     public void setWindowState(WindowState state) throws WindowStateException {
191         getActionResponse().setWindowState(state);
192     }
193
194
195     // Portlet Environment API methods
196

197     /**
198      * Action response is always committed (in a sense that you cannot reset() response)
199      */

200     boolean isCommitted() {
201         return true;
202     }
203 }
204
Popular Tags