2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.server;
22  
23  import java.io.IOException;
24  
25  import java.lang.reflect.Constructor;
26  
27  import java.util.Enumeration;
28  
29  import javax.servlet.Servlet;
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletContext;
32  import javax.servlet.ServletException;
33  import javax.servlet.ServletRequest;
34  import javax.servlet.ServletResponse;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpSession;
37  import javax.servlet.jsp.JspWriter;
38  import javax.servlet.jsp.PageContext;
39  import javax.servlet.jsp.tagext.BodyContent;
40  
41  import org.apache.cactus.ServletURL;
42  import org.apache.cactus.util.ChainedRuntimeException;
43  
44  
45  /**
46   * Abstract wrapper around <code>PageContext</code>. This class provides
47   * a common implementation of the wrapper for the different servlet API.
48   *
49   * @version $Id: AbstractPageContextWrapper.java 292559 2005-09-29 21:36:43Z kenney $
50   */
51  public abstract class AbstractPageContextWrapper extends PageContext
52  {
53      /**
54       * The real page context.
55       */
56      protected PageContext originalPageContext;
57  
58      /**
59       * The URL to simulate.
60       */
61      protected ServletURL url;
62  
63      /**
64       * Construct an <code>PageContext</code> instance that delegates
65       * it's method calls to the page context object passed as parameter and
66       * that uses the URL passed as parameter to simulate a URL from which
67       * the request would come from.
68       *
69       * @param theOriginalPageContext the real page context
70       * @param theURL the URL to simulate or <code>null</code> if none
71       */
72      public AbstractPageContextWrapper(PageContext theOriginalPageContext, 
73          ServletURL theURL)
74      {
75          this.originalPageContext = theOriginalPageContext;
76          this.url = theURL;
77      }
78      /**
79       * {@inheritDoc}
80       *  @see PageContext#newInstance()
81       */
82      public static AbstractPageContextWrapper newInstance(PageContext
83          theOriginalPageContext, ServletURL theServletURL)
84      {
85          try
86          {
87              Class clazz = Class.forName(
88                  "org.apache.cactus.server.PageContextWrapper");
89              Object[] args = new Object[] {theOriginalPageContext, 
90                  theServletURL};
91  
92              Constructor constructor = clazz.getConstructor(new Class[] {
93                  PageContext.class, ServletURL.class });
94  
95              return (AbstractPageContextWrapper) constructor.newInstance(args);
96          }
97          catch (Throwable t)
98          {
99              throw new ChainedRuntimeException(
100                 "Failed to create PageContextWrapper", t);
101         }
102     }
103 
104     // New methods ---------------------------------------------------------
105 
106     /**
107      * @return the original page context
108      * @since 1.7
109      */
110     public PageContext getOriginalPageContext()
111     {
112         return this.originalPageContext;
113     }
114     
115     // Modified overridden methods -------------------------------------------
116 
117     /**
118      * @return the Cactus wrapped servlet request that knows about the
119      *         simulated URL
120      */
121     public ServletRequest getRequest()
122     {
123         // Note: we only manage HttpServletRequest here
124         return AbstractHttpServletRequestWrapper.newInstance(
125             (HttpServletRequest) this.originalPageContext.getRequest(), 
126             this.url);
127     }
128 
129     /**
130      * @return the Cactus wrapped servlet config
131      */
132     public ServletConfig getServletConfig()
133     {
134         return AbstractServletConfigWrapper.newInstance(
135             this.originalPageContext.getServletConfig());
136     }
137 
138     /**
139      * {@inheritDoc}
140      * @return the Cactus wrapped servlet context
141      */
142     public ServletContext getServletContext()
143     {
144         return AbstractServletContextWrapper.newInstance(
145             originalPageContext.getServletContext());
146     }
147 
148     // Unmodified overridden methods -----------------------------------------
149 
150     /**
151      * {@inheritDoc}
152      * @see PageContext#findAttribute(String)
153      */
154     public Object findAttribute(String theName)
155     {
156         return this.originalPageContext.findAttribute(theName);
157     }
158 
159     /**
160      * {@inheritDoc}
161      * @see PageContext#forward(String)
162      */
163     public void forward(String theRelativeURLPath) throws ServletException, 
164         IOException
165     {
166         this.originalPageContext.forward(theRelativeURLPath);
167     }
168 
169     /**
170      * {@inheritDoc}
171      * @see PageContext#getAttribute(String)
172      */
173     public Object getAttribute(String theName)
174     {
175         return this.originalPageContext.getAttribute(theName);
176     }
177 
178     /**
179      * {@inheritDoc}
180      * @see PageContext#getAttribute(String, int)
181      */
182     public Object getAttribute(String theName, int theScope)
183     {
184         return this.originalPageContext.getAttribute(theName, theScope);
185     }
186 
187     /**
188      * {@inheritDoc}
189      * @see PageContext#getAttributeNamesInScope(int)
190      */
191     public Enumeration getAttributeNamesInScope(int theScope)
192     {
193         return this.originalPageContext.getAttributeNamesInScope(theScope);
194     }
195 
196     /**
197      * {@inheritDoc}
198      * @see PageContext#getAttributesScope(String)
199      */
200     public int getAttributesScope(String theName)
201     {
202         return this.originalPageContext.getAttributesScope(theName);
203     }
204 
205     /**
206      * {@inheritDoc}
207      * @see PageContext#getException()
208      */
209     public Exception getException()
210     {
211         return this.originalPageContext.getException();
212     }
213 
214     /**
215      * {@inheritDoc}
216      * @see PageContext#getOut()
217      */
218     public JspWriter getOut()
219     {
220         return this.originalPageContext.getOut();
221     }
222 
223     /**
224      * {@inheritDoc}
225      * @see PageContext#getPage()
226      */
227     public Object getPage()
228     {
229         return this.originalPageContext.getPage();
230     }
231 
232     /**
233      * {@inheritDoc}
234      * @see PageContext#getResponse()
235      */
236     public ServletResponse getResponse()
237     {
238         return this.originalPageContext.getResponse();
239     }
240 
241     /**
242      * {@inheritDoc}
243      * @see PageContext#getSession()
244      */
245     public HttpSession getSession()
246     {
247         return this.originalPageContext.getSession();
248     }
249 
250     /**
251      * {@inheritDoc}
252      * @see PageContext#handlePageException(Exception)
253      */
254     public void handlePageException(Exception theException)
255         throws ServletException, IOException
256     {
257         this.originalPageContext.handlePageException(theException);
258     }
259 
260     /**
261      * {@inheritDoc}
262      * @see PageContext#include(String)
263      */
264     public void include(String theRelativeURLPath) throws ServletException, 
265         IOException
266     {
267         this.originalPageContext.include(theRelativeURLPath);
268     }
269 
270     /**
271      * {@inheritDoc}
272      * @see PageContext#initialize
273      */
274     public void initialize(Servlet theServlet, ServletRequest theRequest, 
275         ServletResponse theResponse, String theErrorPageURL, 
276         boolean isSessionNeeded, int theBufferSize, boolean isAutoFlush) 
277         throws IOException, IllegalStateException, IllegalArgumentException
278     {
279         this.originalPageContext.initialize(theServlet, theRequest, 
280             theResponse, theErrorPageURL, isSessionNeeded, theBufferSize, 
281             isAutoFlush);
282     }
283 
284     /**
285      * {@inheritDoc}
286      * @see PageContext#popBody()
287      */
288     public JspWriter popBody()
289     {
290         return this.originalPageContext.popBody();
291     }
292 
293     /**
294      * {@inheritDoc}
295      * @see PageContext#pushBody()
296      */
297     public BodyContent pushBody()
298     {
299         return this.originalPageContext.pushBody();
300     }
301 
302     /**
303      * {@inheritDoc}
304      * @see PageContext#release()
305      */
306     public void release()
307     {
308         this.originalPageContext.release();
309     }
310 
311     /**
312      * {@inheritDoc}
313      * @see PageContext#removeAttribute(String)
314      */
315     public void removeAttribute(String theName)
316     {
317         this.originalPageContext.removeAttribute(theName);
318     }
319 
320     /**
321      * {@inheritDoc}
322      * @see PageContext#removeAttribute(String, int)
323      */
324     public void removeAttribute(String theName, int theScope)
325     {
326         this.originalPageContext.removeAttribute(theName, theScope);
327     }
328 
329     /**
330      * {@inheritDoc}
331      * @see PageContext#setAttribute(String, Object)
332      */
333     public void setAttribute(String theName, Object theAttribute)
334     {
335         this.originalPageContext.setAttribute(theName, theAttribute);
336     }
337 
338     /**
339      * {@inheritDoc}
340      * @see PageContext#setAttribute(String, Object)
341      */
342     public void setAttribute(String theName, Object theAttribute, int theScope)
343     {
344         this.originalPageContext.setAttribute(theName, theAttribute, theScope);
345     }
346 }