View Javadoc

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.commons.chain.web.portlet;
17  
18  
19  import java.util.Collections;
20  import java.util.Map;
21  import javax.portlet.PortletContext;
22  import javax.portlet.PortletRequest;
23  import javax.portlet.PortletResponse;
24  import org.apache.commons.chain.web.WebContext;
25  
26  
27  /***
28   * <p>Concrete implementation of {@link WebContext} suitable for use in
29   * portlets.  The abstract methods are mapped to the appropriate
30   * collections of the underlying portlet context, request, and response
31   * instances that are passed to the constructor (or the initialize method).</p>
32   *
33   * @author Craig R. McClanahan
34   * @version $Revision: 412789 $ $Date: 2006-06-08 17:19:14 +0100 (Thu, 08 Jun 2006) $
35   */
36  
37  public class PortletWebContext extends WebContext {
38  
39  
40      // ------------------------------------------------------------ Constructors
41  
42  
43      /***
44       * <p>Construct an uninitialized {@link PortletWebContext} instance.</p>
45       */
46      public PortletWebContext() {
47      }
48  
49  
50      /***
51       * <p>Construct a {@link PortletWebContext} instance that is initialized
52       * with the specified Portlet API objects.</p>
53       *
54       * @param context The <code>PortletContext</code> for this web application
55       * @param request The <code>PortletRequest</code> for this request
56       * @param response The <code>PortletResponse</code> for this request
57       */
58      public PortletWebContext(PortletContext context,
59                               PortletRequest request,
60                               PortletResponse response) {
61  
62          initialize(context, request, response);
63  
64      }
65  
66  
67      // ------------------------------------------------------ Instance Variables
68  
69  
70      /***
71       * <p>The lazily instantiated <code>Map</code> of application scope
72       * attributes.</p>
73       */
74      private Map applicationScope = null;
75  
76  
77      /***
78       * <p>The <code>PortletContext</code> for this web application.</p>
79       */
80      protected PortletContext context = null;
81  
82  
83      /***
84       * <p>The lazily instantiated <code>Map</code> of header name-value
85       * combinations (immutable).</p>
86       */
87      private Map header = null;
88  
89  
90      /***
91       * <p>The lazily instantitated <code>Map</code> of header name-values
92       * combinations (immutable).</p>
93       */
94      private Map headerValues = null;
95  
96  
97      /***
98       * <p>The lazily instantiated <code>Map</code> of context initialization
99       * parameters.</p>
100      */
101     private Map initParam = null;
102 
103 
104     /***
105      * <p>The lazily instantiated <code>Map</code> of request
106      * parameter name-value.</p>
107      */
108     private Map param = null;
109 
110 
111     /***
112      * <p>The lazily instantiated <code>Map</code> of request
113      * parameter name-values.</p>
114      */
115     private Map paramValues = null;
116 
117 
118     /***
119      * <p>The <code>PortletRequest</code> for this request.</p>
120      */
121     protected PortletRequest request = null;
122 
123 
124     /***
125      * <p>The lazily instantiated <code>Map</code> of request scope
126      * attributes.</p>
127      */
128     private Map requestScope = null;
129 
130 
131     /***
132      * <p>The <code>PortletResponse</code> for this request.</p>
133      */
134     protected PortletResponse response = null;
135 
136 
137     /***
138      * <p>The lazily instantiated <code>Map</code> of session scope
139      * attributes.</p>
140      */
141     private Map sessionScope = null;
142 
143 
144     // ---------------------------------------------------------- Public Methods
145 
146 
147     /***
148      * <p>Return the {@link PortletContext} for this context.</p>
149      *
150      * @return The <code>PortletContext</code> for this request
151      */
152     public PortletContext getContext() {
153 
154     return (this.context);
155 
156     }
157 
158 
159     /***
160      * <p>Return the {@link PortletRequest} for this context.</p>
161      *
162      * @return The <code>PortletRequest</code> for this context.
163      */
164     public PortletRequest getRequest() {
165 
166     return (this.request);
167 
168     }
169 
170 
171     /***
172      * <p>Return the {@link PortletResponse} for this context.</p>
173      *
174      * @return The <code>PortletResponse</code> for this context.
175      */
176     public PortletResponse getResponse() {
177 
178     return (this.response);
179 
180     }
181 
182 
183     /***
184      * <p>Initialize (or reinitialize) this {@link PortletWebContext} instance
185      * for the specified Portlet API objects.</p>
186      *
187      * @param context The <code>PortletContext</code> for this web application
188      * @param request The <code>PortletRequest</code> for this request
189      * @param response The <code>PortletResponse</code> for this request
190      */
191     public void initialize(PortletContext context,
192                            PortletRequest request,
193                            PortletResponse response) {
194 
195         // Save the specified Portlet API object references
196         this.context = context;
197         this.request = request;
198         this.response = response;
199 
200         // Perform other setup as needed
201 
202     }
203 
204 
205     /***
206      * <p>Release references to allocated resources acquired in
207      * <code>initialize()</code> of via subsequent processing.  After this
208      * method is called, subsequent calls to any other method than
209      * <code>initialize()</code> will return undefined results.</p>
210      */
211     public void release() {
212 
213         // Release references to allocated collections
214         applicationScope = null;
215         header = null;
216         headerValues = null;
217         initParam = null;
218         param = null;
219         paramValues = null;
220         requestScope = null;
221         sessionScope = null;
222 
223         // Release references to Portlet API objects
224         context = null;
225         request = null;
226         response = null;
227 
228     }
229 
230 
231 
232     // ------------------------------------------------------ WebContext Methods
233 
234 
235     /***
236      * See the {@link WebContext}'s Javadoc.
237      *
238      * @return Application scope Map.
239      */
240     public Map getApplicationScope() {
241 
242         if ((applicationScope == null) && (context != null)) {
243             applicationScope = new PortletApplicationScopeMap(context);
244         }
245         return (applicationScope);
246 
247     }
248 
249 
250     /***
251      * See the {@link WebContext}'s Javadoc.
252      *
253      * @return Header values Map.
254      */
255     public Map getHeader() {
256 
257         if ((header == null) && (request != null)) {
258         //            header = new PortletHeaderMap(request);
259         header = Collections.EMPTY_MAP;
260         }
261         return (header);
262 
263     }
264 
265 
266     /***
267      * See the {@link WebContext}'s Javadoc.
268      *
269      * @return Header values Map.
270      */
271     public Map getHeaderValues() {
272 
273         if ((headerValues == null) && (request != null)) {
274         //            headerValues = new PortletHeaderValuesMap(request);
275         headerValues = Collections.EMPTY_MAP;
276         }
277         return (headerValues);
278 
279     }
280 
281 
282     /***
283      * See the {@link WebContext}'s Javadoc.
284      *
285      * @return Initialization parameter Map.
286      */
287     public Map getInitParam() {
288 
289         if ((initParam == null) && (context != null)) {
290             initParam = new PortletInitParamMap(context);
291         }
292         return (initParam);
293 
294     }
295 
296 
297     /***
298      * See the {@link WebContext}'s Javadoc.
299      *
300      * @return Request parameter Map.
301      */
302     public Map getParam() {
303 
304         if ((param == null) && (request != null)) {
305             param = new PortletParamMap(request);
306         }
307         return (param);
308 
309     }
310 
311 
312     /***
313      * See the {@link WebContext}'s Javadoc.
314      *
315      * @return Request parameter Map.
316      */
317     public Map getParamValues() {
318 
319         if ((paramValues == null) && (request != null)) {
320             paramValues = new PortletParamValuesMap(request);
321         }
322         return (paramValues);
323 
324     }
325 
326 
327     /***
328      * Returns an empty Map - portlets don't support Cookies.
329      *
330      * @return An empty Map.
331      * @since Chain 1.1
332      */
333     public Map getCookies() {
334 
335         return Collections.EMPTY_MAP;
336 
337     }
338 
339 
340     /***
341      * See the {@link WebContext}'s Javadoc.
342      *
343      * @return Request scope Map.
344      */
345     public Map getRequestScope() {
346 
347         if ((requestScope == null) && (request != null)) {
348             requestScope = new PortletRequestScopeMap(request);
349         }
350         return (requestScope);
351 
352     }
353 
354 
355     /***
356      * See the {@link WebContext}'s Javadoc.
357      *
358      * @return Session scope Map.
359      */
360     public Map getSessionScope() {
361 
362         if ((sessionScope == null) && (request != null)) {
363             sessionScope =
364             new PortletSessionScopeMap(request);
365         }
366         return (sessionScope);
367 
368     }
369 
370 
371 
372 }