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

1   /*
2    * $Id$
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.request.portlet;
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.Arrays;
28  import java.util.Collections;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import javax.portlet.PortletContext;
34  import javax.portlet.PortletException;
35  import javax.portlet.PortletRequestDispatcher;
36  import javax.portlet.PortletResponse;
37  import javax.portlet.PortletSession;
38  
39  import org.apache.tiles.request.AbstractClientRequest;
40  import org.apache.tiles.request.ApplicationContext;
41  import org.apache.tiles.request.attribute.Addable;
42  import org.apache.tiles.request.collection.HeaderValuesMap;
43  import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
44  import org.apache.tiles.request.collection.ScopeMap;
45  import org.apache.tiles.request.portlet.delegate.RequestDelegate;
46  import org.apache.tiles.request.portlet.delegate.ResponseDelegate;
47  import org.apache.tiles.request.portlet.extractor.HeaderExtractor;
48  import org.apache.tiles.request.portlet.extractor.RequestScopeExtractor;
49  import org.apache.tiles.request.portlet.extractor.SessionScopeExtractor;
50  
51  /**
52   * Portlet-based TilesApplicationContext implementation.
53   *
54   * @version $Rev$ $Date$
55   */
56  public class PortletRequest extends AbstractClientRequest {
57  
58      /**
59       * The native available scopes.
60       */
61      private static final List<String> SCOPES
62              = Collections.unmodifiableList(Arrays.asList(REQUEST_SCOPE, "portletSession", "session", APPLICATION_SCOPE));
63  
64      /**
65       * <p>The lazily instantiated <code>Map</code> of header name-value
66       * combinations (immutable).</p>
67       */
68      private Map<String, String> header = null;
69  
70  
71      /**
72       * <p>The lazily instantiated <code>Map</code> of header name-value
73       * combinations (write-only).</p>
74       */
75      private Addable<String> responseHeaders = null;
76  
77  
78      /**
79       * <p>The lazily instantitated <code>Map</code> of header name-values
80       * combinations (immutable).</p>
81       */
82      private Map<String, String[]> headerValues = null;
83  
84      /**
85       * The <code>PortletContext</code> for this application.
86       */
87      protected PortletContext context = null;
88  
89      /**
90       * <p>The <code>PortletRequest</code> for this request.</p>
91       */
92      protected javax.portlet.PortletRequest request = null;
93  
94      /**
95       * The delegate to get information about parameters.
96       */
97      protected RequestDelegate requestDelegate;
98  
99  
100     /**
101      * <p>The lazily instantiated <code>Map</code> of request scope
102      * attributes.</p>
103      */
104     private Map<String, Object> requestScope = null;
105 
106 
107     /**
108      * <p>The <code>PortletResponse</code> for this request.</p>
109      */
110     protected PortletResponse response = null;
111 
112     /**
113      * The delegate to get information from a response (output stream, writer, etc.).
114      */
115     protected ResponseDelegate responseDelegate;
116 
117 
118     /**
119      * <p>The lazily instantiated <code>Map</code> of session scope
120      * attributes.</p>
121      */
122     private Map<String, Object> sessionScope = null;
123 
124     /**
125      * <p>The lazily instantiated <code>Map</code> of portlet session scope
126      * attributes.</p>
127      */
128     private Map<String, Object> portletSessionScope = null;
129 
130 
131     /**
132      * Creates a new instance of PortletTilesRequestContext.
133      *
134      * @param applicationContext The Tiles application context.
135      * @param context The portlet context to use.
136      * @param request The request object to use.
137      * @param response The response object to use.
138      * @param requestDelegate The request delegate.
139      * @param responseDelegate The response delegate.
140      */
141     public PortletRequest(ApplicationContext applicationContext,
142             PortletContext context, javax.portlet.PortletRequest request,
143             PortletResponse response, RequestDelegate requestDelegate, ResponseDelegate responseDelegate) {
144         super(applicationContext);
145 
146         // Save the specified Portlet API object references
147         this.context = context;
148         this.request = request;
149         this.response = response;
150         this.requestDelegate = requestDelegate;
151         this.responseDelegate = responseDelegate;
152     }
153 
154     /**
155      * <p>Return the {@link PortletRequest} for this context.</p>
156      *
157      * @return The used portlet request.
158      */
159     public javax.portlet.PortletRequest getRequest() {
160         return (this.request);
161     }
162 
163     /**
164      * <p>Return the {@link PortletResponse} for this context.</p>
165      *
166      * @return The used portlet response.
167      */
168     public PortletResponse getResponse() {
169         return (this.response);
170     }
171 
172     /**
173      * Returns the portlet context.
174      *
175      * @return The portlet context.
176      */
177     public PortletContext getPortletContext() {
178         return context;
179     }
180 
181     /** {@inheritDoc} */
182     public Map<String, String> getHeader() {
183         if ((header == null) && (request != null)) {
184             header = new ReadOnlyEnumerationMap<String>(new HeaderExtractor(request, null));
185         }
186         return (header);
187     }
188 
189     /** {@inheritDoc} */
190     public Addable<String> getResponseHeaders() {
191         if ((responseHeaders == null) && (request != null)) {
192             responseHeaders = new HeaderExtractor(null, response);
193         }
194         return (responseHeaders);
195     }
196 
197     /** {@inheritDoc} */
198     public Map<String, String[]> getHeaderValues() {
199         if ((headerValues == null) && (request != null)) {
200             headerValues = new HeaderValuesMap(new HeaderExtractor(request, response));
201         }
202         return (headerValues);
203     }
204 
205     /** {@inheritDoc} */
206     public Map<String, Object> getRequestScope() {
207         if ((requestScope == null) && (request != null)) {
208             requestScope = new ScopeMap(new RequestScopeExtractor(request));
209         }
210         return (requestScope);
211     }
212 
213     /** {@inheritDoc} */
214     public Map<String, Object> getSessionScope() {
215         if ((sessionScope == null) && (request != null)) {
216             sessionScope = new ScopeMap(new SessionScopeExtractor(request,
217                     PortletSession.APPLICATION_SCOPE));
218         }
219         return (sessionScope);
220     }
221 
222     /** {@inheritDoc} */
223     public Map<String, Object> getPortletSessionScope() {
224         if ((portletSessionScope == null) && (request != null)) {
225             portletSessionScope = new ScopeMap(new SessionScopeExtractor(
226                     request, PortletSession.APPLICATION_SCOPE));
227         }
228         return (portletSessionScope);
229     }
230 
231     @Override
232     public List<String> getAvailableScopes() {
233         return SCOPES;
234     }
235 
236     /** {@inheritDoc} */
237     public Locale getRequestLocale() {
238         return request.getLocale();
239     }
240 
241     @Override
242     public Map<String, String> getParam() {
243         return requestDelegate.getParam();
244     }
245 
246     @Override
247     public Map<String, String[]> getParamValues() {
248         return requestDelegate.getParamValues();
249     }
250 
251     /** {@inheritDoc} */
252     public boolean isUserInRole(String role) {
253         return request.isUserInRole(role);
254     }
255 
256     @Override
257     public OutputStream getOutputStream() throws IOException {
258         return responseDelegate.getOutputStream();
259     }
260 
261     @Override
262     public PrintWriter getPrintWriter() throws IOException {
263         return responseDelegate.getPrintWriter();
264     }
265 
266     @Override
267     public Writer getWriter() throws IOException {
268         return responseDelegate.getWriter();
269     }
270 
271     @Override
272     public boolean isResponseCommitted() {
273         return responseDelegate.isResponseCommitted();
274     }
275 
276     @Override
277     public void setContentType(String contentType) {
278         responseDelegate.setContentType(contentType);
279     }
280 
281     /** {@inheritDoc} */
282     public void doForward(String path) throws IOException {
283         if (responseDelegate.isResponseCommitted()) {
284             doInclude(path);
285             return;
286         }
287 
288         try {
289             PortletRequestDispatcher rd = getPortletContext()
290                     .getRequestDispatcher(path);
291 
292             if (rd == null) {
293                 throw new IOException(
294                         "No portlet request dispatcher returned for path '"
295                                 + path + "'");
296             }
297 
298             rd.forward(request, response);
299         } catch (PortletException e) {
300             throw new IOException("PortletException while including path '"
301                     + path + "'.", e);
302         }
303     }
304 
305     /** {@inheritDoc} */
306     public void doInclude(String path) throws IOException {
307         try {
308             PortletRequestDispatcher rd = getPortletContext()
309                     .getRequestDispatcher(path);
310 
311             if (rd == null) {
312                 throw new IOException(
313                         "No portlet request dispatcher returned for path '"
314                                 + path + "'");
315             }
316 
317             rd.include(request, response);
318         } catch (PortletException e) {
319             throw new IOException("PortletException while including path '"
320                     + path + "'.", e);
321         }
322     }
323 
324     @Override
325     public Map<String, Object> getContext(String scope) {
326         if(REQUEST_SCOPE.equals(scope)){
327             return getRequestScope();
328         }else if("session".equals(scope)){
329             return getSessionScope();
330         }else if("portletSession".equals(scope)){
331             return getPortletSessionScope();
332         }else if(APPLICATION_SCOPE.equals(scope)){
333             return getApplicationScope();
334         }
335         throw new IllegalArgumentException(scope + " does not exist. Call getAvailableScopes() first to check.");
336     }
337 }