This project has retired. For details please refer to its Attic page.
PortletTilesRequestContext xref
View Javadoc

1   /*
2    * $Id: PortletTilesRequestContext.java 736275 2009-01-21 09:58:20Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.portlet.context;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  import java.util.Collections;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import javax.portlet.PortletContext;
32  import javax.portlet.PortletException;
33  import javax.portlet.PortletRequest;
34  import javax.portlet.PortletRequestDispatcher;
35  import javax.portlet.PortletResponse;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.tiles.TilesApplicationContext;
40  import org.apache.tiles.context.TilesApplicationContextWrapper;
41  import org.apache.tiles.context.TilesRequestContext;
42  import org.apache.tiles.util.TilesIOException;
43  
44  /***
45   * Portlet-based TilesApplicationContext implementation.
46   *
47   * @version $Rev: 736275 $ $Date: 2009-01-21 10:58:20 +0100 (mer, 21 gen 2009) $
48   */
49  public class PortletTilesRequestContext extends TilesApplicationContextWrapper
50          implements TilesRequestContext {
51  
52      /***
53       * <p>The lazily instantiated <code>Map</code> of header name-value
54       * combinations (immutable).</p>
55       */
56      private Map<String, String> header = null;
57  
58  
59      /***
60       * <p>The lazily instantitated <code>Map</code> of header name-values
61       * combinations (immutable).</p>
62       */
63      private Map<String, String[]> headerValues = null;
64  
65      /***
66       * The <code>PortletContext</code> for this application.
67       */
68      private PortletContext context = null;
69  
70      /***
71       * <p>The <code>PortletRequest</code> for this request.</p>
72       */
73      protected PortletRequest request = null;
74  
75  
76      /***
77       * <p>The lazily instantiated <code>Map</code> of request scope
78       * attributes.</p>
79       */
80      private Map<String, Object> requestScope = null;
81  
82  
83      /***
84       * <p>The <code>PortletResponse</code> for this request.</p>
85       */
86      protected PortletResponse response = null;
87  
88  
89      /***
90       * The request objects, lazily initialized.
91       */
92      private Object[] requestObjects;
93  
94      /***
95       * <p>The lazily instantiated <code>Map</code> of session scope
96       * attributes.</p>
97       */
98      private Map<String, Object> sessionScope = null;
99  
100 
101     /***
102      * <p>Indicates whether the request is an ActionRequest or RenderRequest.
103      */
104     private boolean isRenderRequest;
105     /***
106      * <p>The lazily instantiated <code>Map</code> of request
107      * parameter name-value.</p>
108      */
109     protected Map<String, String> param = null;
110     /***
111      * <p>The lazily instantiated <code>Map</code> of request
112      * parameter name-values.</p>
113      */
114     protected Map<String, String[]> paramValues = null;
115 
116 
117     /***
118      * Creates a new instance of PortletTilesRequestContext.
119      *
120      * @param applicationContext The Tiles application context.
121      * @param context The portlet context to use.
122      * @param request The request object to use.
123      * @param response The response object to use.
124      * @since 2.1.1
125      */
126     public PortletTilesRequestContext(
127             TilesApplicationContext applicationContext, PortletContext context,
128             PortletRequest request, PortletResponse response) {
129         super(applicationContext);
130         this.context = context;
131         initialize(request, response);
132     }
133 
134     /***
135      * Creates a new instance of PortletTilesRequestContext.
136      *
137      * @param context The portlet context to use.
138      * @param request The request object to use.
139      * @param response The response object to use.
140      * @deprecated Use
141      * {@link #PortletTilesRequestContext(TilesApplicationContext, PortletContext, PortletRequest, PortletResponse)}
142      * .
143      */
144     @Deprecated
145     public PortletTilesRequestContext(PortletContext context, PortletRequest request,
146                                       PortletResponse response) {
147         super(new PortletTilesApplicationContext(context));
148         this.context = context;
149         initialize(request, response);
150     }
151 
152     /***
153      * <p>Initialize (or reinitialize) this {@link PortletTilesRequestContext} instance
154      * for the specified Portlet API objects.</p>
155      *
156      * @param request  The <code>PortletRequest</code> for this request
157      * @param response The <code>PortletResponse</code> for this request
158      */
159     public void initialize(PortletRequest request,
160                            PortletResponse response) {
161         // Save the specified Portlet API object references
162         this.request = request;
163         this.response = response;
164 
165         // Perform other setup as needed
166         this.isRenderRequest = false;
167         if (request != null) {
168             if (request instanceof RenderRequest) {
169                 isRenderRequest = true;
170             }
171         }
172     }
173 
174     /***
175      * <p>Release references to allocated resources acquired in
176      * <code>initialize()</code> of via subsequent processing.  After this
177      * method is called, subsequent calls to any other method than
178      * <code>initialize()</code> will return undefined results.</p>
179      */
180     public void release() {
181 
182         // Release references to allocated collections
183         header = null;
184         headerValues = null;
185         param = null;
186         paramValues = null;
187         requestScope = null;
188         sessionScope = null;
189 
190         // Release references to Portlet API objects
191         context = null;
192         request = null;
193         response = null;
194     }
195 
196     /***
197      * <p>Return the {@link PortletRequest} for this context.</p>
198      *
199      * @return The used portlet request.
200      */
201     public PortletRequest getRequest() {
202         return (this.request);
203     }
204 
205     /***
206      * <p>Return the {@link PortletResponse} for this context.</p>
207      *
208      * @return The used portlet response.
209      */
210     public PortletResponse getResponse() {
211         return (this.response);
212     }
213 
214     /*** {@inheritDoc} */
215     @SuppressWarnings("unchecked")
216     public Map<String, String> getHeader() {
217         if ((header == null) && (request != null)) {
218             header = Collections.EMPTY_MAP;
219         }
220         return (header);
221     }
222 
223     /*** {@inheritDoc} */
224     @SuppressWarnings("unchecked")
225     public Map<String, String[]> getHeaderValues() {
226         if ((headerValues == null) && (request != null)) {
227             headerValues = Collections.EMPTY_MAP;
228         }
229         return (headerValues);
230     }
231 
232     /*** {@inheritDoc} */
233     public Map<String, String> getParam() {
234         if ((param == null) && (request != null)) {
235             param = new PortletParamMap(request);
236         }
237         return (param);
238     }
239 
240     /*** {@inheritDoc} */
241     public Map<String, String[]> getParamValues() {
242         if ((paramValues == null) && (request != null)) {
243             paramValues = new PortletParamValuesMap(request);
244         }
245         return (paramValues);
246     }
247 
248     /*** {@inheritDoc} */
249     public Map<String, Object> getRequestScope() {
250         if ((requestScope == null) && (request != null)) {
251             requestScope = new PortletRequestScopeMap(request);
252         }
253         return (requestScope);
254     }
255 
256     /*** {@inheritDoc} */
257     public Map<String, Object> getSessionScope() {
258         if ((sessionScope == null) && (request != null)) {
259             sessionScope =
260                 new PortletSessionScopeMap(request.getPortletSession());
261         }
262         return (sessionScope);
263     }
264 
265     /*** {@inheritDoc} */
266     public TilesApplicationContext getApplicationContext() {
267         return getWrappedApplicationContext();
268     }
269 
270     /*** {@inheritDoc} */
271     public void dispatch(String path) throws IOException {
272         include(path);
273     }
274 
275     /*** {@inheritDoc} */
276     public void include(String path) throws IOException {
277         if (isRenderRequest) {
278             try {
279                 PortletRequestDispatcher rd = context.getRequestDispatcher(path);
280 
281                 if (rd == null) {
282                     throw new IOException(
283                             "No portlet request dispatcher returned for path '"
284                                     + path + "'");
285                 }
286 
287                 rd.include((RenderRequest) request,
288                     (RenderResponse) response);
289             } catch (PortletException e) {
290                 throw new TilesIOException(
291                         "PortletException while including path '" + path + "'.",
292                         e);
293             }
294         }
295     }
296 
297     /*** {@inheritDoc} */
298     public OutputStream getOutputStream() throws IOException {
299         return ((RenderResponse) response).getPortletOutputStream();
300     }
301 
302     /*** {@inheritDoc} */
303     public PrintWriter getPrintWriter() throws IOException {
304         return ((RenderResponse) response).getWriter();
305     }
306 
307     /*** {@inheritDoc} */
308     public Writer getWriter() throws IOException {
309         return ((RenderResponse) response).getWriter();
310     }
311 
312     /*** {@inheritDoc} */
313     public Object[] getRequestObjects() {
314         if (requestObjects == null) {
315             requestObjects = new Object[2];
316             requestObjects[0] = request;
317             requestObjects[1] = response;
318         }
319         return requestObjects;
320     }
321 
322     /*** {@inheritDoc} */
323     public Locale getRequestLocale() {
324         if (request != null) {
325             return request.getLocale();
326         } else {
327             return null;
328         }
329     }
330 
331     /*** {@inheritDoc} */
332     public boolean isUserInRole(String role) {
333         return request.isUserInRole(role);
334     }
335 }