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

1   /*
2    * $Id: PortletTilesRequestContext.java 632818 2008-03-02 19:48:05Z 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.util.Collections;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletRequestDispatcher;
32  import javax.portlet.PortletResponse;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.tiles.context.TilesRequestContext;
37  import org.apache.tiles.util.TilesIOException;
38  
39  /***
40   * Portlet-based TilesApplicationContext implementation.
41   *
42   * @version $Rev: 632818 $ $Date: 2008-03-02 20:48:05 +0100 (Sun, 02 Mar 2008) $
43   */
44  public class PortletTilesRequestContext extends PortletTilesApplicationContext implements TilesRequestContext {
45  
46      /***
47       * <p>The lazily instantiated <code>Map</code> of header name-value
48       * combinations (immutable).</p>
49       */
50      private Map<String, String> header = null;
51  
52  
53      /***
54       * <p>The lazily instantitated <code>Map</code> of header name-values
55       * combinations (immutable).</p>
56       */
57      private Map<String, String[]> headerValues = null;
58  
59  
60      /***
61       * <p>The <code>PortletRequest</code> for this request.</p>
62       */
63      protected PortletRequest request = null;
64  
65  
66      /***
67       * <p>The lazily instantiated <code>Map</code> of request scope
68       * attributes.</p>
69       */
70      private Map<String, Object> requestScope = null;
71  
72  
73      /***
74       * <p>The <code>PortletResponse</code> for this request.</p>
75       */
76      protected PortletResponse response = null;
77  
78  
79      /***
80       * <p>The lazily instantiated <code>Map</code> of session scope
81       * attributes.</p>
82       */
83      private Map<String, Object> sessionScope = null;
84  
85  
86      /***
87       * <p>Indicates whether the request is an ActionRequest or RenderRequest.
88       */
89      private boolean isRenderRequest;
90      /***
91       * <p>The lazily instantiated <code>Map</code> of request
92       * parameter name-value.</p>
93       */
94      protected Map<String, String> param = null;
95      /***
96       * <p>The lazily instantiated <code>Map</code> of request
97       * parameter name-values.</p>
98       */
99      protected Map<String, String[]> paramValues = null;
100 
101 
102     /***
103      * Creates a new instance of PortletTilesRequestContext.
104      *
105      * @param context The portlet context to use.
106      * @param request The request object to use.
107      * @param response The response object to use.
108      */
109     public PortletTilesRequestContext(PortletContext context, PortletRequest request,
110                                       PortletResponse response) {
111         super(context);
112         initialize(request, response);
113     }
114 
115     /***
116      * <p>Initialize (or reinitialize) this {@link PortletTilesRequestContext} instance
117      * for the specified Portlet API objects.</p>
118      *
119      * @param request  The <code>PortletRequest</code> for this request
120      * @param response The <code>PortletResponse</code> for this request
121      */
122     public void initialize(PortletRequest request,
123                            PortletResponse response) {
124         // Save the specified Portlet API object references
125         this.request = request;
126         this.response = response;
127 
128         // Perform other setup as needed
129         this.isRenderRequest = false;
130         if (request != null) {
131             if (request instanceof RenderRequest) {
132                 isRenderRequest = true;
133             }
134         }
135 
136     }
137 
138     /***
139      * <p>Release references to allocated resources acquired in
140      * <code>initialize()</code> of via subsequent processing.  After this
141      * method is called, subsequent calls to any other method than
142      * <code>initialize()</code> will return undefined results.</p>
143      */
144     public void release() {
145 
146         // Release references to allocated collections
147         header = null;
148         headerValues = null;
149         param = null;
150         paramValues = null;
151         requestScope = null;
152         sessionScope = null;
153 
154         // Release references to Portlet API objects
155         context = null;
156         request = null;
157         response = null;
158         super.release();
159 
160     }
161 
162     /***
163      * <p>Return the {@link PortletRequest} for this context.</p>
164      *
165      * @return The used portlet request.
166      */
167     public PortletRequest getRequest() {
168         return (this.request);
169     }
170 
171     /***
172      * <p>Return the {@link PortletResponse} for this context.</p>
173      *
174      * @return The used portlet response.
175      */
176     public PortletResponse getResponse() {
177         return (this.response);
178     }
179 
180     /*** {@inheritDoc} */
181     @SuppressWarnings("unchecked")
182     public Map<String, String> getHeader() {
183         if ((header == null) && (request != null)) {
184             header = Collections.EMPTY_MAP;
185         }
186         return (header);
187     }
188 
189     /*** {@inheritDoc} */
190     @SuppressWarnings("unchecked")
191     public Map<String, String[]> getHeaderValues() {
192         if ((headerValues == null) && (request != null)) {
193             headerValues = Collections.EMPTY_MAP;
194         }
195         return (headerValues);
196     }
197 
198     /*** {@inheritDoc} */
199     public Map<String, String> getParam() {
200         if ((param == null) && (request != null)) {
201             param = new PortletParamMap(request);
202         }
203         return (param);
204     }
205 
206     /*** {@inheritDoc} */
207     public Map<String, String[]> getParamValues() {
208         if ((paramValues == null) && (request != null)) {
209             paramValues = new PortletParamValuesMap(request);
210         }
211         return (paramValues);
212     }
213 
214     /*** {@inheritDoc} */
215     public Map<String, Object> getRequestScope() {
216         if ((requestScope == null) && (request != null)) {
217             requestScope = new PortletRequestScopeMap(request);
218         }
219         return (requestScope);
220     }
221 
222     /*** {@inheritDoc} */
223     public Map<String, Object> getSessionScope() {
224         if ((sessionScope == null) && (request != null)) {
225             sessionScope =
226                 new PortletSessionScopeMap(request.getPortletSession());
227         }
228         return (sessionScope);
229     }
230 
231     /*** {@inheritDoc} */
232     public void dispatch(String path) throws IOException {
233         include(path);
234     }
235 
236     /*** {@inheritDoc} */
237     public void include(String path) throws IOException {
238         if (isRenderRequest) {
239             try {
240                 PortletRequestDispatcher rd = context.getRequestDispatcher(path);
241 
242                 if (rd == null) {
243                     throw new IOException(
244                             "No portlet request dispatcher returned for path '"
245                                     + path + "'");
246                 }
247 
248                 rd.include((RenderRequest) request,
249                     (RenderResponse) response);
250             } catch (PortletException e) {
251                 throw new TilesIOException(
252                         "PortletException while including path '" + path + "'.",
253                         e);
254             }
255         }
256     }
257 
258     /*** {@inheritDoc} */
259     public Locale getRequestLocale() {
260         if (request != null) {
261             return request.getLocale();
262         } else {
263             return null;
264         }
265     }
266 
267     /*** {@inheritDoc} */
268     public boolean isUserInRole(String role) {
269         return request.isUserInRole(role);
270     }
271 }