KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > ServletHttpContext


1 // ========================================================================
2
// $Id: ServletHttpContext.java,v 1.27 2005/04/13 16:30:47 janb Exp $
3
// Copyright 2001-2004 Mort Bay Consulting Pty. Ltd.
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
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.io.IOException JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.mortbay.http.HttpContext;
26 import org.mortbay.http.HttpException;
27 import org.mortbay.http.HttpRequest;
28 import org.mortbay.http.HttpResponse;
29
30 /* ------------------------------------------------------------ */
31 /** ServletHttpContext.
32  * Extends HttpContext with conveniance methods for adding servlets.
33  * Enforces a single ServletHandler per context.
34  * @version $Id: ServletHttpContext.java,v 1.27 2005/04/13 16:30:47 janb Exp $
35  * @author Greg Wilkins (gregw)
36  */

37 public class ServletHttpContext extends HttpContext
38 {
39     private HashMap JavaDoc _localeEncodingMap = new HashMap JavaDoc();
40     private ServletHandler _servletHandler=null;
41     
42     /* ------------------------------------------------------------ */
43     /** Constructor.
44      */

45     public ServletHttpContext()
46     {
47         super();
48     }
49     
50     /* ------------------------------------------------------------ */
51     /**
52      * @return The ServletContext.
53      */

54     public ServletContext JavaDoc getServletContext()
55     {
56         ServletHandler shandler=getServletHandler();
57         if (shandler!=null)
58             return shandler.getServletContext();
59         throw new IllegalStateException JavaDoc();
60     }
61     
62     /* ------------------------------------------------------------ */
63     /** Get the context ServletHandler.
64      * Conveniance method. If no ServletHandler exists, a new one is added to
65      * the context.
66      * @return ServletHandler
67      */

68     public synchronized ServletHandler getServletHandler()
69     {
70         if (_servletHandler==null)
71             _servletHandler=(ServletHandler) getHandler(ServletHandler.class);
72         if (_servletHandler==null)
73         {
74             _servletHandler=new ServletHandler();
75             addHandler(_servletHandler);
76         }
77         return _servletHandler;
78     }
79     
80     /* ------------------------------------------------------------ */
81     /** Add a servlet to the context.
82      * Conveniance method.
83      * If no ServletHandler is found in the context, a new one is added.
84      * @param pathSpec The pathspec within the context
85      * @param className The classname of the servlet.
86      * @return The ServletHolder.
87      * @exception ClassNotFoundException
88      * @exception InstantiationException
89      * @exception IllegalAccessException
90      */

91     public synchronized ServletHolder addServlet(String JavaDoc pathSpec,
92                                                  String JavaDoc className)
93         throws ClassNotFoundException JavaDoc,
94                InstantiationException JavaDoc,
95                IllegalAccessException JavaDoc
96     {
97         return addServlet(className,pathSpec,className);
98     }
99     
100     /* ------------------------------------------------------------ */
101     /** Add a servlet to the context.
102      * If no ServletHandler is found in the context, a new one is added.
103      * @param name The name of the servlet.
104      * @param pathSpec The pathspec within the context
105      * @param className The classname of the servlet.
106      * @return The ServletHolder.
107      * @exception ClassNotFoundException
108      * @exception InstantiationException
109      * @exception IllegalAccessException
110      */

111     public synchronized ServletHolder addServlet(String JavaDoc name,
112                                                  String JavaDoc pathSpec,
113                                                  String JavaDoc className)
114         throws ClassNotFoundException JavaDoc,
115                InstantiationException JavaDoc,
116                IllegalAccessException JavaDoc
117     {
118         return getServletHandler().addServlet(name,pathSpec,className,null);
119     }
120
121     /* ------------------------------------------------------------ */
122     protected boolean jSecurityCheck(String JavaDoc pathInContext,
123                                      HttpRequest request,
124                                      HttpResponse response)
125             throws IOException JavaDoc
126     {
127         if (getAuthenticator() instanceof FormAuthenticator &&
128             pathInContext.endsWith(FormAuthenticator.__J_SECURITY_CHECK) &&
129             getAuthenticator().authenticate(getRealm(),
130                                                         pathInContext,
131                                                         request,
132                                                         response)==null)
133             return false;
134         return true;
135     }
136     
137     /* ------------------------------------------------------------ */
138     public boolean checkSecurityConstraints(String JavaDoc pathInContext,
139                                             HttpRequest request,
140                                             HttpResponse response)
141             throws HttpException, IOException JavaDoc
142     {
143         if (!super.checkSecurityConstraints(pathInContext,request,response) ||
144             ! jSecurityCheck(pathInContext,request,response))
145             return false;
146         
147         return true;
148     }
149     
150     /* ------------------------------------------------------------ */
151     public void addLocaleEncoding(String JavaDoc locale,String JavaDoc encoding)
152     {
153         _localeEncodingMap.put(locale, encoding);
154     }
155     
156     /* ------------------------------------------------------------ */
157     /**
158      * Get the character encoding for a locale. The full locale name is first
159      * looked up in the map of encodings. If no encoding is found, then the
160      * locale language is looked up.
161      *
162      * @param locale a <code>Locale</code> value
163      * @return a <code>String</code> representing the character encoding for
164      * the locale or null if none found.
165      */

166     public String JavaDoc getLocaleEncoding(Locale JavaDoc locale)
167     {
168         String JavaDoc encoding = (String JavaDoc)_localeEncodingMap.get(locale.toString());
169         if (encoding==null)
170             encoding = (String JavaDoc)_localeEncodingMap.get(locale.getLanguage());
171         return encoding;
172     }
173     
174     /* ------------------------------------------------------------ */
175     public String JavaDoc toString()
176     {
177         return "Servlet"+super.toString();
178     }
179
180     /* ------------------------------------------------------------ */
181     /* send servlet response error
182      *
183      */

184     public void sendError(HttpResponse response,int code,String JavaDoc msg)
185         throws IOException JavaDoc
186     {
187         Object JavaDoc wrapper = response.getWrapper();
188         if (wrapper!=null && wrapper instanceof HttpServletResponse JavaDoc)
189             ((HttpServletResponse JavaDoc)wrapper).sendError(code,msg);
190         else
191             super.sendError(response,code,msg);
192     }
193
194     /* ------------------------------------------------------------ */
195     public void destroy()
196     {
197         super.destroy();
198         if (_localeEncodingMap!=null)
199             _localeEncodingMap.clear();
200         _localeEncodingMap=null;
201     }
202     
203
204     /* ------------------------------------------------------------ */
205     /*
206      * @see org.mortbay.http.HttpContext#enterContextScope(org.mortbay.http.HttpRequest, org.mortbay.http.HttpResponse)
207      */

208     public Object JavaDoc enterContextScope(HttpRequest request, HttpResponse response)
209     {
210         // Make sure servlet wrappers exist for request/response objects
211
ServletHttpRequest srequest = (ServletHttpRequest) request.getWrapper();
212         ServletHttpResponse sresponse = (ServletHttpResponse) response.getWrapper();
213         if (srequest==null)
214         {
215             // Build the request and response.
216
srequest = new ServletHttpRequest(getServletHandler(),null,request);
217             sresponse = new ServletHttpResponse(srequest,response);
218             request.setWrapper(srequest);
219             response.setWrapper(sresponse);
220         }
221         
222         return super.enterContextScope(request,response);
223     }
224     
225     /* ------------------------------------------------------------ */
226     /*
227      * @see org.mortbay.util.Container#doStop()
228      */

229     protected void doStop() throws Exception JavaDoc
230     {
231         super.doStop();
232         _servletHandler=null;
233     }
234 }
235
Popular Tags