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

1   /*
2    * $Id: JspRequest.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.jsp;
22  
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.io.Writer;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Map;
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.jsp.PageContext;
35  
36  import org.apache.tiles.request.AbstractViewRequest;
37  import org.apache.tiles.request.ApplicationContext;
38  import org.apache.tiles.request.DispatchRequest;
39  import org.apache.tiles.request.collection.ScopeMap;
40  import org.apache.tiles.request.jsp.extractor.ScopeExtractor;
41  import org.apache.tiles.request.jsp.extractor.SessionScopeExtractor;
42  import org.apache.tiles.request.servlet.ServletRequest;
43  import org.apache.tiles.request.servlet.ServletUtil;
44  
45  /**
46   * Context implementation used for executing tiles within a
47   * jsp tag library.
48   *
49   * @version $Rev: 1375743 $ $Date: 2012-08-22 06:05:58 +1000 (Wed, 22 Aug 2012) $
50   */
51  public class JspRequest extends AbstractViewRequest {
52  
53      /**
54       * The native available scopes.
55       */
56      private static final List<String> SCOPES
57              = Collections.unmodifiableList(Arrays.asList("page", REQUEST_SCOPE, "session", APPLICATION_SCOPE));
58  
59      /**
60       * The current page context.
61       */
62      private PageContext pageContext;
63  
64      /**
65       * <p>The lazily instantiated <code>Map</code> of page scope
66       * attributes.</p>
67       */
68      private Map<String, Object> pageScope = null;
69  
70      /**
71       * <p>The lazily instantiated <code>Map</code> of request scope
72       * attributes.</p>
73       */
74      private Map<String, Object> requestScope = null;
75  
76      /**
77       * <p>The lazily instantiated <code>Map</code> of session scope
78       * attributes.</p>
79       */
80      private Map<String, Object> sessionScope = null;
81  
82      /**
83       * <p>The lazily instantiated <code>Map</code> of application scope
84       * attributes.</p>
85       */
86      private Map<String, Object> applicationScope = null;
87  
88      /**
89       * Creates a JSP request.
90       *
91       * @param applicationContext The application context.
92       * @param pageContext The page context.
93       * @return A new JSP request.
94       */
95      public static JspRequest createServletJspRequest(ApplicationContext applicationContext, PageContext pageContext) {
96          return new JspRequest(new ServletRequest(
97                  applicationContext, (HttpServletRequest) pageContext
98                          .getRequest(), (HttpServletResponse) pageContext
99                          .getResponse()), pageContext);
100     }
101 
102     /**
103      * Constructor.
104      *
105      * @param enclosedRequest The request that is wrapped here.
106      * @param pageContext The page context to use.
107      */
108     public JspRequest(DispatchRequest enclosedRequest,
109             PageContext pageContext) {
110         super(enclosedRequest);
111         this.pageContext = pageContext;
112     }
113 
114     @Override
115     public List<String> getAvailableScopes() {
116         return SCOPES;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     protected void doInclude(String path) throws IOException {
122         try {
123             pageContext.include(path, false);
124         } catch (ServletException e) {
125             throw ServletUtil.wrapServletException(e, "JSPException including path '"
126                     + path + "'.");
127         }
128     }
129 
130     /** {@inheritDoc} */
131     @Override
132     public PrintWriter getPrintWriter() {
133         return new JspPrintWriterAdapter(pageContext.getOut());
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public Writer getWriter() {
139         return pageContext.getOut();
140     }
141 
142     /**
143      * Returns the page scope.
144      *
145      * @return The page scope.
146      */
147     public Map<String, Object> getPageScope() {
148         if ((pageScope == null) && (pageContext != null)) {
149             pageScope = new ScopeMap(new ScopeExtractor(pageContext,
150                     PageContext.PAGE_SCOPE));
151         }
152         return (pageScope);
153     }
154 
155     /**
156      * Returns the request scope.
157      *
158      * @return The request scope.
159      */
160     public Map<String, Object> getRequestScope() {
161         if ((requestScope == null) && (pageContext != null)) {
162             requestScope = new ScopeMap(new ScopeExtractor(pageContext,
163                     PageContext.REQUEST_SCOPE));
164         }
165         return (requestScope);
166     }
167 
168     /**
169      * Returns the session scope.
170      *
171      * @return The session scope.
172      */
173     public Map<String, Object> getSessionScope() {
174         if ((sessionScope == null) && (pageContext != null)) {
175             sessionScope = new ScopeMap(new SessionScopeExtractor(pageContext));
176         }
177         return (sessionScope);
178     }
179 
180     /**
181      * Returns the application scope.
182      *
183      * @return The application scope.
184      */
185     public Map<String, Object> getApplicationScope() {
186         if ((applicationScope == null) && (pageContext != null)) {
187             applicationScope = new ScopeMap(new ScopeExtractor(pageContext,
188                     PageContext.APPLICATION_SCOPE));
189         }
190         return (applicationScope);
191     }
192 
193     /**
194      * Returns the page context that originated the request.
195      *
196      * @return The page context.
197      */
198     public PageContext getPageContext() {
199         return pageContext;
200     }
201 
202     @Override
203     public Map<String, Object> getContext(String scope) {
204         if("page".equals(scope)){
205             return getPageScope();
206         }else if(REQUEST_SCOPE.equals(scope)){
207             return getRequestScope();
208         }else if("session".equals(scope)){
209             return getSessionScope();
210         }else if(APPLICATION_SCOPE.equals(scope)){
211             return getApplicationScope();
212         }
213         throw new IllegalArgumentException(scope + " does not exist. Call getAvailableScopes() first to check.");
214     }
215 }