001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.servlet;
020:
021: import javax.servlet.ServletRequest;
022: import javax.servlet.ServletRequestWrapper;
023:
024: import org.apache.beehive.controls.api.context.ControlContainerContext;
025: import org.apache.beehive.controls.api.context.ControlThreadContext;
026:
027: /**
028: * This class is the contextual service implementation for javax.servlet.ServletRequest. It
029: * acts as an intermediary between the client and the ServletRequest instance held by the
030: * associated ServletBeanContext. It validates that attempt to access the ServletRequest
031: * only occur during the servlet request processing lifecycle, then delegates to the underlying
032: * ServletRequest instance for actual services.
033: */
034: /* package */class ServletRequestService implements ServletRequest {
035: /**
036: * This static helper class derives from javax.servlet.ServletRequestWrapper and can
037: * be used to wrap a ServletRequestService instance in cases where the client expects
038: * to be able to use the standard wrapper interfaces to unwrap requests.
039: */
040: private static class Wrapper extends ServletRequestWrapper {
041: Wrapper(ServletRequestService requestService) {
042: super (requestService);
043: _requestService = requestService;
044: }
045:
046: /**
047: * Overrides the default ServletRequestWrapper.getRequest implementation. Rather
048: * than just returning the request passed into the constructor (i.e. the request
049: * service), it will go unwrap step further and return the current (active) http
050: * request.
051: */
052: public ServletRequest getRequest() {
053: return _requestService.getServletRequest();
054: }
055:
056: ServletRequestService _requestService;
057: }
058:
059: /* package */ServletRequestService(ServletBeanContext beanContext) {
060: _beanContext = beanContext;
061: }
062:
063: /**
064: * This method retrieves the current ServletBeanContext for the service. It is capable
065: * of reassociating with the current active context, if the service instance has been
066: * serialized/deserialized.
067: */
068: final protected ServletBeanContext getServletBeanContext() {
069: if (_beanContext == null) {
070: ControlContainerContext ccc = ControlThreadContext
071: .getContext();
072: if (!(ccc instanceof ServletBeanContext))
073: throw new IllegalStateException(
074: "No ServletBeanContext available");
075:
076: _beanContext = (ServletBeanContext) ccc;
077: }
078: return _beanContext;
079: }
080:
081: /**
082: * This method returns a ServletRequestWrapper instance that wraps the request service.
083: * This is useful in instances where there is code that uses the wrapper interfaces to
084: * unwrap requests to get to the 'root' request.
085: */
086: /* package */ServletRequestWrapper getRequestWrapper() {
087: return new Wrapper(this );
088: }
089:
090: final protected ServletRequest getServletRequest() {
091: ServletRequest servletRequest = getServletBeanContext()
092: .getServletRequest();
093: if (servletRequest == null)
094: throw new IllegalStateException(
095: "No access to ServletRequest outside request processing");
096: return servletRequest;
097: }
098:
099: public java.lang.Object getAttribute(java.lang.String name) {
100: return getServletRequest().getAttribute(name);
101: }
102:
103: public java.util.Enumeration getAttributeNames() {
104: return getServletRequest().getAttributeNames();
105: }
106:
107: public java.lang.String getCharacterEncoding() {
108: return getServletRequest().getCharacterEncoding();
109: }
110:
111: public void setCharacterEncoding(java.lang.String env)
112: throws java.io.UnsupportedEncodingException {
113: getServletRequest().setCharacterEncoding(env);
114: }
115:
116: public int getContentLength() {
117: return getServletRequest().getContentLength();
118: }
119:
120: public java.lang.String getContentType() {
121: return getServletRequest().getContentType();
122: }
123:
124: public javax.servlet.ServletInputStream getInputStream()
125: throws java.io.IOException {
126: return getServletRequest().getInputStream();
127: }
128:
129: public java.lang.String getParameter(java.lang.String name) {
130: return getServletRequest().getParameter(name);
131: }
132:
133: public java.util.Enumeration getParameterNames() {
134: return getServletRequest().getParameterNames();
135: }
136:
137: public java.lang.String[] getParameterValues(java.lang.String name) {
138: return getServletRequest().getParameterValues(name);
139: }
140:
141: public java.util.Map getParameterMap() {
142: return getServletRequest().getParameterMap();
143: }
144:
145: public java.lang.String getProtocol() {
146: return getServletRequest().getProtocol();
147: }
148:
149: public java.lang.String getScheme() {
150: return getServletRequest().getScheme();
151: }
152:
153: public java.lang.String getServerName() {
154: return getServletRequest().getServerName();
155: }
156:
157: public int getServerPort() {
158: return getServletRequest().getServerPort();
159: }
160:
161: public int getLocalPort() {
162: return getServletRequest().getLocalPort();
163: }
164:
165: public java.lang.String getLocalAddr() {
166: return getServletRequest().getLocalAddr();
167: }
168:
169: public java.lang.String getLocalName() {
170: return getServletRequest().getLocalName();
171: }
172:
173: public java.io.BufferedReader getReader()
174: throws java.io.IOException {
175: return getServletRequest().getReader();
176: }
177:
178: public java.lang.String getRemoteAddr() {
179: return getServletRequest().getRemoteAddr();
180: }
181:
182: public java.lang.String getRemoteHost() {
183: return getServletRequest().getRemoteHost();
184: }
185:
186: public int getRemotePort() {
187: return getServletRequest().getRemotePort();
188: }
189:
190: public void setAttribute(java.lang.String name, java.lang.Object o) {
191: getServletRequest().setAttribute(name, o);
192: }
193:
194: public void removeAttribute(java.lang.String name) {
195: getServletRequest().removeAttribute(name);
196: }
197:
198: public java.util.Locale getLocale() {
199: return getServletRequest().getLocale();
200: }
201:
202: public java.util.Enumeration getLocales() {
203: return getServletRequest().getLocales();
204: }
205:
206: public boolean isSecure() {
207: return getServletRequest().isSecure();
208: }
209:
210: public javax.servlet.RequestDispatcher getRequestDispatcher(
211: java.lang.String path) {
212: return getServletRequest().getRequestDispatcher(path);
213: }
214:
215: public java.lang.String getRealPath(java.lang.String path) {
216: return getServletRequest().getRealPath(path);
217: }
218:
219: transient private ServletBeanContext _beanContext; // never access directly
220: }
|