KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServletResponse JavaDoc;
21 import javax.servlet.ServletResponseWrapper 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.ServletResponse. It
28  * acts as an intermediary between the client and the ServletResponse instance held by the
29  * associated ServletBeanContext. It validates that attempt to access the ServletResponse
30  * only occur during the servlet request processing lifecycle, then delegates to the underlying
31  * ServletResponse instance for actual services.
32  */

33 /* package */ class ServletResponseService implements ServletResponse 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 ServletResponseWrapper JavaDoc
41     {
42         Wrapper(ServletResponseService responseService)
43         {
44             super(responseService);
45             _responseService = responseService;
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 ServletResponse JavaDoc getResponse()
55         {
56             return _responseService.getServletResponse();
57         }
58
59         ServletResponseService _responseService;
60     }
61     
62     /* package */ ServletResponseService(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     final protected ServletResponse JavaDoc getServletResponse()
86     {
87         ServletResponse JavaDoc servletResponse = getServletBeanContext().getServletResponse();
88         if (servletResponse == null)
89             throw new IllegalStateException JavaDoc("No access to ServletResponse outside request processing");
90         return servletResponse;
91     }
92
93     /**
94      * This method returns a ServletResponseWrapper instance that wraps the response service.
95      * This is useful in instances where there is code that uses the wrapper interfaces to
96      * unwrap responses to get to the 'root' response.
97      */

98     /* package */ ServletResponseWrapper JavaDoc getResponseWrapper()
99     {
100         return new Wrapper JavaDoc(this);
101     }
102
103     public java.lang.String JavaDoc getCharacterEncoding()
104     {
105         return getServletResponse().getCharacterEncoding();
106     }
107
108     public java.lang.String JavaDoc getContentType()
109     {
110         return getServletResponse().getContentType();
111     }
112
113     public javax.servlet.ServletOutputStream JavaDoc getOutputStream() throws java.io.IOException JavaDoc
114     {
115         return getServletResponse().getOutputStream();
116     }
117
118     public java.io.PrintWriter JavaDoc getWriter() throws java.io.IOException JavaDoc
119     {
120         return getServletResponse().getWriter();
121     }
122
123     public void setCharacterEncoding(java.lang.String JavaDoc charset)
124     {
125         getServletResponse().setCharacterEncoding(charset);
126     }
127
128     public void setContentLength(int len)
129     {
130         getServletResponse().setContentLength(len);
131     }
132
133     public void setContentType(java.lang.String JavaDoc type)
134     {
135         getServletResponse().setContentType(type);
136     }
137
138     public void setBufferSize(int size)
139     {
140         getServletResponse().setBufferSize(size);
141     }
142
143     public int getBufferSize()
144     {
145         return getServletResponse().getBufferSize();
146     }
147
148     public void flushBuffer() throws java.io.IOException JavaDoc
149     {
150         getServletResponse().flushBuffer();
151     }
152
153     public void resetBuffer()
154     {
155         getServletResponse().resetBuffer();
156     }
157
158     public boolean isCommitted()
159     {
160         return getServletResponse().isCommitted();
161     }
162
163     public void reset()
164     {
165         getServletResponse().reset();
166     }
167
168     public void setLocale(java.util.Locale JavaDoc loc)
169     {
170         getServletResponse().setLocale(loc);
171     }
172
173     public java.util.Locale JavaDoc getLocale()
174     {
175         return getServletResponse().getLocale();
176     }
177
178     transient private ServletBeanContext _beanContext; // never access directly
179
}
180
Popular Tags