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 javax.servlet.RequestDispatcher;
26 import javax.servlet.ServletException;
27 import javax.servlet.ServletRequest;
28 import javax.servlet.ServletResponse;
29
30 /**
31 * Wrapper around <code>RequestDispatcher</code> which overrides the
32 * <code>forward()</code> and <code>include</code> methods to use the original
33 * HTTP request object instead of the simulated one used by Cactus.
34 *
35 * @version $Id: RequestDispatcherWrapper.java 292559 2005-09-29 21:36:43Z kenney $
36 */
37 public class RequestDispatcherWrapper implements RequestDispatcher
38 {
39 /**
40 * The original request dispatcher object.
41 */
42 private RequestDispatcher originalDispatcher;
43
44 /**
45 * @param theOriginalDispatcher the original request dispatcher object
46 */
47 public RequestDispatcherWrapper(RequestDispatcher theOriginalDispatcher)
48 {
49 this.originalDispatcher = theOriginalDispatcher;
50 }
51
52 /**
53 * Call the original <code>RequestDispatcher</code> <code>forward()</code>
54 * method but with the original HTTP request (not the simulation one which
55 * would make the servlet engine choke !).
56 *
57 * @param theRequest the simulation HTTP request
58 * @param theResponse the original HTTP response
59 * @exception IOException {@link RequestDispatcher#forward}
60 * @exception ServletException {@link RequestDispatcher#forward}
61 */
62 public void forward(ServletRequest theRequest, ServletResponse theResponse)
63 throws IOException, ServletException
64 {
65 // Always pass the original request to the forward() call.
66 if (theRequest instanceof AbstractHttpServletRequestWrapper)
67 {
68 AbstractHttpServletRequestWrapper request =
69 (AbstractHttpServletRequestWrapper) theRequest;
70
71 this.originalDispatcher.forward(request.getOriginalRequest(),
72 theResponse);
73 }
74 else
75 {
76 this.originalDispatcher.forward(theRequest, theResponse);
77 }
78 }
79
80 /**
81 * Call the original <code>RequestDispatcher</code> <code>include()</code>
82 * method but with the original HTTP request (not the simulation one which
83 * would make the servlet engine choke !).
84 *
85 * @param theRequest the simulation HTTP request
86 * @param theResponse the original HTTP response
87 * @exception IOException {@link RequestDispatcher#forward}
88 * @exception ServletException {@link RequestDispatcher#forward}
89 */
90 public void include(ServletRequest theRequest, ServletResponse theResponse)
91 throws IOException, ServletException
92 {
93 // Always pass the original request to the forward() call.
94 if (theRequest instanceof AbstractHttpServletRequestWrapper)
95 {
96 AbstractHttpServletRequestWrapper request =
97 (AbstractHttpServletRequestWrapper) theRequest;
98
99 this.originalDispatcher.include(request.getOriginalRequest(),
100 theResponse);
101 }
102 else
103 {
104 this.originalDispatcher.include(theRequest, theResponse);
105 }
106 }
107 }