KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > servlet > ServletRequestService


1 package org.apache.beehive.controls.runtime.servlet;
2 /*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * $Header:$
18  */

19
20 import javax.servlet.ServletRequest JavaDoc;
21 import javax.servlet.ServletRequestWrapper JavaDoc;
22
23 import org.apache.beehive.controls.api.context.ControlContainerContext;
24 import org.apache.beehive.controls.api.context.ControlThreadContext;
25
26 /**
27  * This class is the contextual service implementation for javax.servlet.ServletRequest. It
28  * acts as an intermediary between the client and the ServletRequest instance held by the
29  * associated ServletBeanContext. It validates that attempt to access the ServletRequest
30  * only occur during the servlet request processing lifecycle, then delegates to the underlying
31  * ServletRequest instance for actual services.
32  */

33 /* package */ class ServletRequestService implements ServletRequest JavaDoc
34 {
35     /**
36      * This static helper class derives from javax.servlet.ServletRequestWrapper and can
37      * be used to wrap a ServletRequestService instance in cases where the client expects
38      * to be able to use the standard wrapper interfaces to unwrap requests.
39      */

40     private static class Wrapper extends ServletRequestWrapper JavaDoc
41     {
42         Wrapper(ServletRequestService requestService)
43         {
44             super(requestService);
45             _requestService = requestService;
46         }
47
48         /**
49          * Overrides the default ServletRequestWrapper.getRequest implementation. Rather
50          * than just returning the request passed into the constructor (i.e. the request
51          * service), it will go unwrap step further and return the current (active) http
52          * request.
53          */

54         public ServletRequest JavaDoc getRequest()
55         {
56             return _requestService.getServletRequest();
57         }
58
59         ServletRequestService _requestService;
60     }
61     
62     /* package */ ServletRequestService(ServletBeanContext beanContext)
63     {
64         _beanContext = beanContext;
65     }
66
67     /**
68      * This method retrieves the current ServletBeanContext for the service. It is capable
69      * of reassociating with the current active context, if the service instance has been
70      * serialized/deserialized.
71      */

72     final protected ServletBeanContext getServletBeanContext()
73     {
74         if (_beanContext == null)
75         {
76             ControlContainerContext ccc = ControlThreadContext.getContext();
77             if (! (ccc instanceof ServletBeanContext))
78                 throw new IllegalStateException JavaDoc("No ServletBeanContext available");
79
80             _beanContext = (ServletBeanContext)ccc;
81         }
82         return _beanContext;
83     }
84
85     /**
86      * This method returns a ServletRequestWrapper instance that wraps the request service.
87      * This is useful in instances where there is code that uses the wrapper interfaces to
88      * unwrap requests to get to the 'root' request.
89      */

90     /* package */ ServletRequestWrapper JavaDoc getRequestWrapper()
91     {
92         return new Wrapper JavaDoc(this);
93     }
94
95     final protected ServletRequest JavaDoc getServletRequest()
96     {
97         ServletRequest JavaDoc servletRequest = getServletBeanContext().getServletRequest();
98         if (servletRequest == null)
99             throw new IllegalStateException JavaDoc("No access to ServletRequest outside request processing");
100         return servletRequest;
101     }
102
103     public java.lang.Object JavaDoc getAttribute(java.lang.String JavaDoc name)
104     {
105         return getServletRequest().getAttribute(name);
106     }
107
108     public java.util.Enumeration JavaDoc getAttributeNames()
109     {
110         return getServletRequest().getAttributeNames();
111     }
112
113     public java.lang.String JavaDoc getCharacterEncoding()
114     {
115         return getServletRequest().getCharacterEncoding();
116     }
117
118     public void setCharacterEncoding(java.lang.String JavaDoc env)
119                 throws java.io.UnsupportedEncodingException JavaDoc
120     {
121         getServletRequest().setCharacterEncoding(env);
122     }
123
124     public int getContentLength()
125     {
126         return getServletRequest().getContentLength();
127     }
128
129     public java.lang.String JavaDoc getContentType()
130     {
131         return getServletRequest().getContentType();
132     }
133
134     public javax.servlet.ServletInputStream JavaDoc getInputStream() throws java.io.IOException JavaDoc
135     {
136         return getServletRequest().getInputStream();
137     }
138
139     public java.lang.String JavaDoc getParameter(java.lang.String JavaDoc name)
140     {
141         return getServletRequest().getParameter(name);
142     }
143
144     public java.util.Enumeration JavaDoc getParameterNames()
145     {
146         return getServletRequest().getParameterNames();
147     }
148
149     public java.lang.String JavaDoc[] getParameterValues(java.lang.String JavaDoc name)
150     {
151         return getServletRequest().getParameterValues(name);
152     }
153
154     public java.util.Map JavaDoc getParameterMap()
155     {
156         return getServletRequest().getParameterMap();
157     }
158
159     public java.lang.String JavaDoc getProtocol()
160     {
161         return getServletRequest().getProtocol();
162     }
163
164     public java.lang.String JavaDoc getScheme()
165     {
166         return getServletRequest().getScheme();
167     }
168
169     public java.lang.String JavaDoc getServerName()
170     {
171         return getServletRequest().getServerName();
172     }
173
174     public int getServerPort()
175     {
176         return getServletRequest().getServerPort();
177     }
178
179     public int getLocalPort()
180     {
181         return getServletRequest().getLocalPort();
182     }
183
184     public java.lang.String JavaDoc getLocalAddr()
185     {
186         return getServletRequest().getLocalAddr();
187     }
188
189     public java.lang.String JavaDoc getLocalName()
190     {
191         return getServletRequest().getLocalName();
192     }
193
194
195     public java.io.BufferedReader JavaDoc getReader() throws java.io.IOException JavaDoc
196     {
197         return getServletRequest().getReader();
198     }
199
200     public java.lang.String JavaDoc getRemoteAddr()
201     {
202         return getServletRequest().getRemoteAddr();
203     }
204
205     public java.lang.String JavaDoc getRemoteHost()
206     {
207         return getServletRequest().getRemoteHost();
208     }
209
210     public int getRemotePort()
211     {
212         return getServletRequest().getRemotePort();
213     }
214
215     public void setAttribute(java.lang.String JavaDoc name, java.lang.Object JavaDoc o)
216     {
217         getServletRequest().setAttribute(name, o);
218     }
219
220     public void removeAttribute(java.lang.String JavaDoc name)
221     {
222         getServletRequest().removeAttribute(name);
223     }
224
225     public java.util.Locale JavaDoc getLocale()
226     {
227         return getServletRequest().getLocale();
228     }
229
230     public java.util.Enumeration JavaDoc getLocales()
231     {
232         return getServletRequest().getLocales();
233     }
234
235     public boolean isSecure()
236     {
237         return getServletRequest().isSecure();
238     }
239
240     public javax.servlet.RequestDispatcher JavaDoc getRequestDispatcher(java.lang.String JavaDoc path)
241     {
242         return getServletRequest().getRequestDispatcher(path);
243     }
244
245     public java.lang.String JavaDoc getRealPath(java.lang.String JavaDoc path)
246     {
247         return getServletRequest().getRealPath(path);
248     }
249
250     transient private ServletBeanContext _beanContext; // never access directly
251
}
252
Popular Tags