001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.web.portlet;
018    
019    
020    import java.util.Collections;
021    import java.util.Map;
022    import javax.portlet.PortletContext;
023    import javax.portlet.PortletRequest;
024    import javax.portlet.PortletResponse;
025    import org.apache.commons.chain.web.WebContext;
026    
027    
028    /**
029     * <p>Concrete implementation of {@link WebContext} suitable for use in
030     * portlets.  The abstract methods are mapped to the appropriate
031     * collections of the underlying portlet context, request, and response
032     * instances that are passed to the constructor (or the initialize method).</p>
033     *
034     * @author Craig R. McClanahan
035     * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
036     */
037    
038    public class PortletWebContext extends WebContext {
039    
040    
041        // ------------------------------------------------------------ Constructors
042    
043    
044        /**
045         * <p>Construct an uninitialized {@link PortletWebContext} instance.</p>
046         */
047        public PortletWebContext() {
048        }
049    
050    
051        /**
052         * <p>Construct a {@link PortletWebContext} instance that is initialized
053         * with the specified Portlet API objects.</p>
054         *
055         * @param context The <code>PortletContext</code> for this web application
056         * @param request The <code>PortletRequest</code> for this request
057         * @param response The <code>PortletResponse</code> for this request
058         */
059        public PortletWebContext(PortletContext context,
060                                 PortletRequest request,
061                                 PortletResponse response) {
062    
063            initialize(context, request, response);
064    
065        }
066    
067    
068        // ------------------------------------------------------ Instance Variables
069    
070    
071        /**
072         * <p>The lazily instantiated <code>Map</code> of application scope
073         * attributes.</p>
074         */
075        private Map applicationScope = null;
076    
077    
078        /**
079         * <p>The <code>PortletContext</code> for this web application.</p>
080         */
081        protected PortletContext context = null;
082    
083    
084        /**
085         * <p>The lazily instantiated <code>Map</code> of header name-value
086         * combinations (immutable).</p>
087         */
088        private Map header = null;
089    
090    
091        /**
092         * <p>The lazily instantitated <code>Map</code> of header name-values
093         * combinations (immutable).</p>
094         */
095        private Map headerValues = null;
096    
097    
098        /**
099         * <p>The lazily instantiated <code>Map</code> of context initialization
100         * parameters.</p>
101         */
102        private Map initParam = null;
103    
104    
105        /**
106         * <p>The lazily instantiated <code>Map</code> of request
107         * parameter name-value.</p>
108         */
109        private Map param = null;
110    
111    
112        /**
113         * <p>The lazily instantiated <code>Map</code> of request
114         * parameter name-values.</p>
115         */
116        private Map paramValues = null;
117    
118    
119        /**
120         * <p>The <code>PortletRequest</code> for this request.</p>
121         */
122        protected PortletRequest request = null;
123    
124    
125        /**
126         * <p>The lazily instantiated <code>Map</code> of request scope
127         * attributes.</p>
128         */
129        private Map requestScope = null;
130    
131    
132        /**
133         * <p>The <code>PortletResponse</code> for this request.</p>
134         */
135        protected PortletResponse response = null;
136    
137    
138        /**
139         * <p>The lazily instantiated <code>Map</code> of session scope
140         * attributes.</p>
141         */
142        private Map sessionScope = null;
143    
144    
145        // ---------------------------------------------------------- Public Methods
146    
147    
148        /**
149         * <p>Return the {@link PortletContext} for this context.</p>
150         *
151         * @return The <code>PortletContext</code> for this request
152         */
153        public PortletContext getContext() {
154    
155        return (this.context);
156    
157        }
158    
159    
160        /**
161         * <p>Return the {@link PortletRequest} for this context.</p>
162         *
163         * @return The <code>PortletRequest</code> for this context.
164         */
165        public PortletRequest getRequest() {
166    
167        return (this.request);
168    
169        }
170    
171    
172        /**
173         * <p>Return the {@link PortletResponse} for this context.</p>
174         *
175         * @return The <code>PortletResponse</code> for this context.
176         */
177        public PortletResponse getResponse() {
178    
179        return (this.response);
180    
181        }
182    
183    
184        /**
185         * <p>Initialize (or reinitialize) this {@link PortletWebContext} instance
186         * for the specified Portlet API objects.</p>
187         *
188         * @param context The <code>PortletContext</code> for this web application
189         * @param request The <code>PortletRequest</code> for this request
190         * @param response The <code>PortletResponse</code> for this request
191         */
192        public void initialize(PortletContext context,
193                               PortletRequest request,
194                               PortletResponse response) {
195    
196            // Save the specified Portlet API object references
197            this.context = context;
198            this.request = request;
199            this.response = response;
200    
201            // Perform other setup as needed
202    
203        }
204    
205    
206        /**
207         * <p>Release references to allocated resources acquired in
208         * <code>initialize()</code> of via subsequent processing.  After this
209         * method is called, subsequent calls to any other method than
210         * <code>initialize()</code> will return undefined results.</p>
211         */
212        public void release() {
213    
214            // Release references to allocated collections
215            applicationScope = null;
216            header = null;
217            headerValues = null;
218            initParam = null;
219            param = null;
220            paramValues = null;
221            requestScope = null;
222            sessionScope = null;
223    
224            // Release references to Portlet API objects
225            context = null;
226            request = null;
227            response = null;
228    
229        }
230    
231    
232    
233        // ------------------------------------------------------ WebContext Methods
234    
235    
236        /**
237         * See the {@link WebContext}'s Javadoc.
238         *
239         * @return Application scope Map.
240         */
241        public Map getApplicationScope() {
242    
243            if ((applicationScope == null) && (context != null)) {
244                applicationScope = new PortletApplicationScopeMap(context);
245            }
246            return (applicationScope);
247    
248        }
249    
250    
251        /**
252         * See the {@link WebContext}'s Javadoc.
253         *
254         * @return Header values Map.
255         */
256        public Map getHeader() {
257    
258            if ((header == null) && (request != null)) {
259            //            header = new PortletHeaderMap(request);
260            header = Collections.EMPTY_MAP;
261            }
262            return (header);
263    
264        }
265    
266    
267        /**
268         * See the {@link WebContext}'s Javadoc.
269         *
270         * @return Header values Map.
271         */
272        public Map getHeaderValues() {
273    
274            if ((headerValues == null) && (request != null)) {
275            //            headerValues = new PortletHeaderValuesMap(request);
276            headerValues = Collections.EMPTY_MAP;
277            }
278            return (headerValues);
279    
280        }
281    
282    
283        /**
284         * See the {@link WebContext}'s Javadoc.
285         *
286         * @return Initialization parameter Map.
287         */
288        public Map getInitParam() {
289    
290            if ((initParam == null) && (context != null)) {
291                initParam = new PortletInitParamMap(context);
292            }
293            return (initParam);
294    
295        }
296    
297    
298        /**
299         * See the {@link WebContext}'s Javadoc.
300         *
301         * @return Request parameter Map.
302         */
303        public Map getParam() {
304    
305            if ((param == null) && (request != null)) {
306                param = new PortletParamMap(request);
307            }
308            return (param);
309    
310        }
311    
312    
313        /**
314         * See the {@link WebContext}'s Javadoc.
315         *
316         * @return Request parameter Map.
317         */
318        public Map getParamValues() {
319    
320            if ((paramValues == null) && (request != null)) {
321                paramValues = new PortletParamValuesMap(request);
322            }
323            return (paramValues);
324    
325        }
326    
327    
328        /**
329         * Returns an empty Map - portlets don't support Cookies.
330         *
331         * @return An empty Map.
332         * @since Chain 1.1
333         */
334        public Map getCookies() {
335    
336            return Collections.EMPTY_MAP;
337    
338        }
339    
340    
341        /**
342         * See the {@link WebContext}'s Javadoc.
343         *
344         * @return Request scope Map.
345         */
346        public Map getRequestScope() {
347    
348            if ((requestScope == null) && (request != null)) {
349                requestScope = new PortletRequestScopeMap(request);
350            }
351            return (requestScope);
352    
353        }
354    
355    
356        /**
357         * See the {@link WebContext}'s Javadoc.
358         *
359         * @return Session scope Map.
360         */
361        public Map getSessionScope() {
362    
363            if ((sessionScope == null) && (request != null)) {
364                sessionScope =
365                new PortletSessionScopeMap(request);
366            }
367            return (sessionScope);
368    
369        }
370    
371    
372    
373    }