KickJava   Java API By Example, From Geeks To Geeks.

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


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.environment.AbstractEnvironment;
19 import org.apache.cocoon.environment.Context;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.PermanentRedirector;
22 import org.apache.cocoon.environment.Redirector;
23 import org.apache.cocoon.environment.Session;
24
25 import javax.portlet.PortletContext;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * Implements {@link org.apache.cocoon.environment.Environment} interface for the JSR-168
31  * Portlet environment.
32  *
33  * @author <a HREF="mailto:alex.rudnev@dc.gov">Alex Rudnev</a>
34  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
35  * @version CVS $Id: PortletEnvironment.java 46202 2004-09-16 20:00:29Z vgritsenko $
36  */

37 public class PortletEnvironment extends AbstractEnvironment implements Redirector, PermanentRedirector {
38
39     /**
40      * As portlets do not have a pathInfo in the request, we can simulate this by passing
41      * a parameter. If parameter is null, this will translate to the absent pathInfo,
42      * and portlets will be matched in the sitemap using only servletPath.
43      */

44     public static final String JavaDoc PARAMETER_PATH_INFO = "cocoon-portlet-path";
45
46     /**
47      * This header can be read from any portlet request,
48      * and can be set on action response.
49      */

50     public static final String JavaDoc HEADER_PORTLET_MODE = "X-Portlet-Mode";
51
52     /**
53      * This header can be read from any portlet request,
54      * and can be set on action response.
55      */

56     public static final String JavaDoc HEADER_WINDOW_STATE = "X-Window-State";
57
58     /**
59      * This header can be set only, and only on render response.
60      */

61     public static final String JavaDoc HEADER_PORTLET_TITLE = "X-Portlet-Title";
62
63     /**
64      * This is the prefix for application scope session attributes.
65      */

66     public static final String JavaDoc SESSION_APPLICATION_SCOPE = "portlet-application-";
67
68     /**
69      * This is the prefix for portlet scope session attributes.
70      */

71     public static final String JavaDoc SESSION_PORTLET_SCOPE = "portlet-portlet-";
72
73
74     /**
75      * The PortletRequest
76      */

77     private PortletRequest request;
78
79     /**
80      * The PortletResponse
81      */

82     private PortletResponse response;
83
84     /**
85      * The PortletContext
86      */

87     private Context context;
88
89
90     /**
91      * Cache content type as there is no getContentType()
92      * method on reponse object
93      */

94     private String JavaDoc contentType;
95
96     /**
97      * Did we redirect?
98      */

99     private boolean hasRedirected;
100
101     /**
102      * @see #getDefaultSessionScope()
103      */

104     private int defaultSessionScope;
105
106     /**
107      * Constructs a PortletEnvironment object from a PortletRequest
108      * and PortletResponse objects
109      */

110     public PortletEnvironment(String JavaDoc servletPath,
111                               String JavaDoc pathInfo,
112                               String JavaDoc uri,
113                               String JavaDoc root,
114                               javax.portlet.ActionRequest request,
115                               javax.portlet.ActionResponse response,
116                               PortletContext portletContext,
117                               Context context,
118                               String JavaDoc containerEncoding,
119                               String JavaDoc defaultFormEncoding,
120                               int defaultSessionScope)
121     throws IOException JavaDoc {
122         super(uri, null, root, null);
123
124         this.request = new ActionRequest(servletPath, pathInfo, request, this);
125         this.request.setCharacterEncoding(defaultFormEncoding);
126         this.request.setContainerEncoding(containerEncoding);
127         this.response = new ActionResponse(response, request.getPreferences(), (ActionRequest) this.request, uri);
128         this.context = context;
129         this.defaultSessionScope = defaultSessionScope;
130
131         setView(extractView(this.request));
132         setAction(extractAction(this.request));
133
134         initObjectModel(request, response, portletContext);
135     }
136
137     /**
138      * Constructs a PortletEnvironment object from a PortletRequest
139      * and PortletResponse objects
140      */

141     public PortletEnvironment(String JavaDoc servletPath,
142                               String JavaDoc pathInfo,
143                               String JavaDoc uri,
144                               String JavaDoc root,
145                               javax.portlet.RenderRequest request,
146                               javax.portlet.RenderResponse response,
147                               PortletContext portletContext,
148                               Context context,
149                               String JavaDoc containerEncoding,
150                               String JavaDoc defaultFormEncoding,
151                               int defaultSessionScope)
152     throws IOException JavaDoc {
153         super(uri, null, root, null);
154
155         this.request = new RenderRequest(servletPath, pathInfo, request, this);
156         this.request.setCharacterEncoding(defaultFormEncoding);
157         this.request.setContainerEncoding(containerEncoding);
158         this.response = new RenderResponse(response, request.getPreferences());
159         this.context = context;
160         this.defaultSessionScope = defaultSessionScope;
161
162         setView(extractView(this.request));
163         setAction(extractAction(this.request));
164
165         initObjectModel(request, response, portletContext);
166     }
167
168     private void initObjectModel(javax.portlet.PortletRequest portletRequest,
169                                  javax.portlet.PortletResponse portletResponse,
170                                  PortletContext portletContext) {
171         this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request);
172         this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, this.response);
173         this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, this.context);
174
175         // This is a kind of a hack for the components that need
176
// the real portlet objects to pass them along to other
177
// libraries.
178
PortletObjectModelHelper.setPortletRequest(this.objectModel, portletRequest);
179         PortletObjectModelHelper.setPortletResponse(this.objectModel, portletResponse);
180         PortletObjectModelHelper.setPortletContext(this.objectModel, portletContext);
181     }
182
183
184     public void redirect(boolean sessionmode, String JavaDoc newURL) throws IOException JavaDoc {
185         this.hasRedirected = true;
186
187         // check if session mode shall be activated
188
if (sessionmode) {
189             if (getLogger().isDebugEnabled()) {
190                 String JavaDoc s = request.getRequestedSessionId();
191                 if (s != null) {
192                     getLogger().debug("Session ID in request = " + s +
193                                       (request.isRequestedSessionIdValid() ? " (valid)" : " (invalid)"));
194                 }
195             }
196
197             // get session from request, or create new session
198
Session session = request.getSession(true);
199             if (getLogger().isDebugEnabled()) {
200                 getLogger().debug("Session ID = " + session.getId());
201             }
202         }
203
204         // redirect
205
String JavaDoc redirect = newURL;
206         if (getLogger().isDebugEnabled()) {
207             getLogger().debug("Sending redirect to '" + redirect + "'");
208         }
209
210         this.response.sendRedirect(redirect);
211     }
212
213     /**
214      * In portlet environment this is the same as {@link #redirect(boolean, String)}
215      */

216     public void permanentRedirect(boolean sessionmode, String JavaDoc newURL) throws IOException JavaDoc {
217         redirect(sessionmode, newURL);
218     }
219
220     public boolean hasRedirected() {
221         return this.hasRedirected;
222     }
223
224     /**
225      * Portlet environment does not support response status code.
226      */

227     public void setStatus(int statusCode) {
228     }
229
230     /**
231      * Portlet environment does not support response status code.
232      */

233     public void sendStatus(int sc) {
234         throw new AbstractMethodError JavaDoc("Not Implemented");
235     }
236
237     /**
238      * Set the ContentType
239      */

240     public void setContentType(String JavaDoc contentType) {
241         this.response.setContentType(contentType);
242         this.contentType = contentType;
243     }
244
245     /**
246      * Get the ContentType
247      */

248     public String JavaDoc getContentType() {
249         return this.contentType;
250     }
251
252     /**
253      * Portlet environment does not support response content length.
254      * This method does nothing.
255      */

256     public void setContentLength(int length) {
257     }
258
259     /**
260      * This method always returns true because portlet environment
261      * does not support response codes.
262      */

263     public boolean isResponseModified(long lastModified) {
264         return true;
265     }
266
267     /**
268      * Portlet environment does not support response status code.
269      * This method does nothing.
270      */

271     public void setResponseIsNotModified() {
272     }
273
274     /**
275      * Reset the response if possible. This allows error handlers to have
276      * a higher chance to produce clean output if the pipeline that raised
277      * the error has already output some data.
278      *
279      * @return true if the response was successfully reset
280      */

281     public boolean tryResetResponse() throws IOException JavaDoc {
282         if (!super.tryResetResponse()) {
283             try {
284                 if (!this.response.isCommitted()) {
285                     this.response.reset();
286                     if (getLogger().isDebugEnabled()) {
287                         getLogger().debug("Response successfully reset");
288                     }
289                     return true;
290                 }
291             } catch (Exception JavaDoc e) {
292                 // Log the error, but don't transmit it
293
getLogger().warn("Problem resetting response", e);
294             }
295             if (getLogger().isDebugEnabled()) {
296                 getLogger().debug("Response wasn't reset");
297             }
298             return false;
299         }
300         return true;
301     }
302
303     /**
304      * Get the output stream where to write the generated resource.
305      * The returned stream is buffered by the environment. If the
306      * buffer size is -1 then the complete output is buffered.
307      * If the buffer size is 0, no buffering takes place.
308      * This method replaces {@link #getOutputStream()}.
309      */

310     public OutputStream JavaDoc getOutputStream(final int bufferSize) throws IOException JavaDoc {
311         if (this.outputStream == null) {
312             this.outputStream = this.response.getOutputStream();
313         }
314         return super.getOutputStream(bufferSize);
315     }
316
317     /**
318      * Always return <code>true</code>.
319      */

320     public boolean isExternal() {
321         return true;
322     }
323
324     /**
325      * Default scope for the session attributes, either
326      * {@link javax.portlet.PortletSession#PORTLET_SCOPE} or
327      * {@link javax.portlet.PortletSession#APPLICATION_SCOPE}.
328      */

329     int getDefaultSessionScope() {
330         return this.defaultSessionScope;
331     }
332 }
333
Popular Tags