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

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