001: //========================================================================
002: //$Id: WebAppContext.java,v 1.5 2005/11/16 22:02:45 gregwilkins Exp $
003: //Copyright 2004-2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.servlet;
017:
018: import javax.servlet.RequestDispatcher;
019: import javax.servlet.ServletContext;
020:
021: import org.mortbay.jetty.HandlerContainer;
022: import org.mortbay.jetty.handler.ContextHandler;
023: import org.mortbay.jetty.handler.ErrorHandler;
024: import org.mortbay.jetty.security.SecurityHandler;
025: import org.mortbay.log.Log;
026: import org.mortbay.util.URIUtil;
027:
028: /* ------------------------------------------------------------ */
029: /** Servlet Context.
030: * This conveniance extention to the ContextHandler allows for
031: * simple construction of a context with ServletHandler and optionally
032: * session and security handlers, et.<pre>
033: * new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY);
034: * </pre>
035: * <p/>
036: * This class should have been called ServletContext, but this would have
037: * cause confusion with {@link ServletContext}.
038: */
039: public class Context extends ContextHandler {
040: public final static int SESSIONS = 1;
041: public final static int SECURITY = 2;
042: public final static int NO_SESSIONS = 0;
043: public final static int NO_SECURITY = 0;
044:
045: protected SecurityHandler _securityHandler;
046: protected ServletHandler _servletHandler;
047: protected SessionHandler _sessionHandler;
048:
049: /* ------------------------------------------------------------ */
050: public Context() {
051: this (null, null, null, null, null);
052: }
053:
054: /* ------------------------------------------------------------ */
055: public Context(int options) {
056: this (null, null, options);
057: }
058:
059: /* ------------------------------------------------------------ */
060: public Context(HandlerContainer parent, String contextPath) {
061: this (parent, null, null, null, null);
062: setContextPath(contextPath);
063: }
064:
065: /* ------------------------------------------------------------ */
066: public Context(HandlerContainer parent, String contextPath,
067: int options) {
068: this (parent, ((options & SESSIONS) != 0) ? new SessionHandler()
069: : null,
070: ((options & SECURITY) != 0) ? new SecurityHandler()
071: : null, null, null);
072: setContextPath(contextPath);
073: }
074:
075: /* ------------------------------------------------------------ */
076: public Context(HandlerContainer parent, String contextPath,
077: boolean sessions, boolean security) {
078: this (parent, contextPath, (sessions ? SESSIONS : 0)
079: | (security ? SECURITY : 0));
080: }
081:
082: /* ------------------------------------------------------------ */
083: /**
084: */
085: public Context(HandlerContainer parent,
086: SessionHandler sessionHandler,
087: SecurityHandler securityHandler,
088: ServletHandler servletHandler, ErrorHandler errorHandler) {
089: super ((ContextHandler.SContext) null);
090: _scontext = new SContext();
091: _sessionHandler = sessionHandler;
092: _securityHandler = securityHandler;
093: _servletHandler = servletHandler != null ? servletHandler
094: : new ServletHandler();
095:
096: if (_sessionHandler != null) {
097: setHandler(_sessionHandler);
098:
099: if (securityHandler != null) {
100: _sessionHandler.setHandler(_securityHandler);
101: _securityHandler.setHandler(_servletHandler);
102: } else {
103: _sessionHandler.setHandler(_servletHandler);
104: }
105: } else if (_securityHandler != null) {
106: setHandler(_securityHandler);
107: _securityHandler.setHandler(_servletHandler);
108: } else {
109: setHandler(_servletHandler);
110: }
111:
112: if (errorHandler != null)
113: setErrorHandler(errorHandler);
114:
115: if (parent != null) {
116: parent.addHandler(this );
117: }
118:
119: }
120:
121: /* ------------------------------------------------------------ */
122: /**
123: * @see org.mortbay.jetty.handler.ContextHandler#startContext()
124: */
125: protected void startContext() throws Exception {
126: super .startContext();
127:
128: // OK to Initialize servlet handler now
129: if (_servletHandler != null && _servletHandler.isStarted())
130: _servletHandler.initialize();
131: }
132:
133: /* ------------------------------------------------------------ */
134: /**
135: * @return Returns the securityHandler.
136: */
137: public SecurityHandler getSecurityHandler() {
138: return _securityHandler;
139: }
140:
141: /* ------------------------------------------------------------ */
142: /**
143: * @return Returns the servletHandler.
144: */
145: public ServletHandler getServletHandler() {
146: return _servletHandler;
147: }
148:
149: /* ------------------------------------------------------------ */
150: /**
151: * @return Returns the sessionHandler.
152: */
153: public SessionHandler getSessionHandler() {
154: return _sessionHandler;
155: }
156:
157: /* ------------------------------------------------------------ */
158: /** conveniance method to add a servlet.
159: */
160: public ServletHolder addServlet(String className, String pathSpec) {
161: return _servletHandler.addServletWithMapping(className,
162: pathSpec);
163: }
164:
165: /* ------------------------------------------------------------ */
166: /** conveniance method to add a servlet.
167: */
168: public ServletHolder addServlet(Class servlet, String pathSpec) {
169: return _servletHandler.addServletWithMapping(servlet.getName(),
170: pathSpec);
171: }
172:
173: /* ------------------------------------------------------------ */
174: /** conveniance method to add a servlet.
175: */
176: public void addServlet(ServletHolder servlet, String pathSpec) {
177: _servletHandler.addServletWithMapping(servlet, pathSpec);
178: }
179:
180: /* ------------------------------------------------------------ */
181: /** conveniance method to add a filter
182: */
183: public void addFilter(FilterHolder holder, String pathSpec,
184: int dispatches) {
185: _servletHandler.addFilterWithMapping(holder, pathSpec,
186: dispatches);
187: }
188:
189: /* ------------------------------------------------------------ */
190: /** conveniance method to add a filter
191: */
192: public FilterHolder addFilter(Class filterClass, String pathSpec,
193: int dispatches) {
194: return _servletHandler.addFilterWithMapping(filterClass,
195: pathSpec, dispatches);
196: }
197:
198: /* ------------------------------------------------------------ */
199: /** conveniance method to add a filter
200: */
201: public FilterHolder addFilter(String filterClass, String pathSpec,
202: int dispatches) {
203: return _servletHandler.addFilterWithMapping(filterClass,
204: pathSpec, dispatches);
205: }
206:
207: public class SContext extends ContextHandler.SContext {
208:
209: /* ------------------------------------------------------------ */
210: /*
211: * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
212: */
213: public RequestDispatcher getNamedDispatcher(String name) {
214: ContextHandler context = org.mortbay.jetty.servlet.Context.this ;
215: if (_servletHandler == null
216: || _servletHandler.getServlet(name) == null)
217: return null;
218: return new Dispatcher(context, name);
219: }
220:
221: /* ------------------------------------------------------------ */
222: /*
223: * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
224: */
225: public RequestDispatcher getRequestDispatcher(
226: String uriInContext) {
227: if (uriInContext == null)
228: return null;
229:
230: if (!uriInContext.startsWith("/"))
231: return null;
232:
233: try {
234: String query = null;
235: int q = 0;
236: if ((q = uriInContext.indexOf('?')) > 0) {
237: query = uriInContext.substring(q + 1);
238: uriInContext = uriInContext.substring(0, q);
239: }
240: if ((q = uriInContext.indexOf(';')) > 0)
241: uriInContext = uriInContext.substring(0, q);
242:
243: String pathInContext = URIUtil.canonicalPath(URIUtil
244: .decodePath(uriInContext));
245: String uri = URIUtil.addPaths(getContextPath(),
246: uriInContext);
247: ContextHandler context = org.mortbay.jetty.servlet.Context.this ;
248: return new Dispatcher(context, uri, pathInContext,
249: query);
250: } catch (Exception e) {
251: Log.ignore(e);
252: }
253: return null;
254: }
255: }
256:
257: }
|