001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/core/StandardWrapperValve.java,v 1.34 2002/03/15 19:12:49 remm Exp $
003: * $Revision: 1.34 $
004: * $Date: 2002/03/15 19:12:49 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.core;
065:
066: import java.io.IOException;
067: import java.io.PrintWriter;
068: import javax.servlet.FilterConfig;
069: import javax.servlet.RequestDispatcher;
070: import javax.servlet.Servlet;
071: import javax.servlet.ServletContext;
072: import javax.servlet.ServletException;
073: import javax.servlet.ServletOutputStream;
074: import javax.servlet.ServletRequest;
075: import javax.servlet.ServletResponse;
076: import javax.servlet.UnavailableException;
077: import javax.servlet.http.HttpServlet;
078: import javax.servlet.http.HttpServletRequest;
079: import javax.servlet.http.HttpServletResponse;
080: import org.apache.catalina.Container;
081: import org.apache.catalina.Context;
082: import org.apache.catalina.Globals;
083: import org.apache.catalina.HttpRequest;
084: import org.apache.catalina.HttpResponse;
085: import org.apache.catalina.Logger;
086: import org.apache.catalina.Request;
087: import org.apache.catalina.Response;
088: import org.apache.catalina.ValveContext;
089: import org.apache.catalina.Wrapper;
090: import org.apache.catalina.deploy.ErrorPage;
091: import org.apache.catalina.deploy.FilterDef;
092: import org.apache.catalina.deploy.FilterMap;
093: import org.apache.catalina.util.InstanceSupport;
094: import org.apache.catalina.util.RequestUtil;
095: import org.apache.catalina.util.StringManager;
096: import org.apache.catalina.valves.ValveBase;
097:
098: /**
099: * Valve that implements the default basic behavior for the
100: * <code>StandardWrapper</code> container implementation.
101: *
102: * @author Craig R. McClanahan
103: * @version $Revision: 1.34 $ $Date: 2002/03/15 19:12:49 $
104: */
105:
106: final class StandardWrapperValve extends ValveBase {
107:
108: // ----------------------------------------------------- Instance Variables
109:
110: /**
111: * The debugging detail level for this component.
112: */
113: private int debug = 0;
114:
115: /**
116: * The filter definition for our container-provided filter.
117: */
118: private FilterDef filterDef = null;
119:
120: /**
121: * The descriptive information related to this implementation.
122: */
123: private static final String info = "org.apache.catalina.core.StandardWrapperValve/1.0";
124:
125: /**
126: * The string manager for this package.
127: */
128: private static final StringManager sm = StringManager
129: .getManager(Constants.Package);
130:
131: // ------------------------------------------------------------- Properties
132:
133: /**
134: * Return descriptive information about this Valve implementation.
135: */
136: public String getInfo() {
137:
138: return (info);
139:
140: }
141:
142: // --------------------------------------------------------- Public Methods
143:
144: /**
145: * Invoke the servlet we are managing, respecting the rules regarding
146: * servlet lifecycle and SingleThreadModel support.
147: *
148: * @param request Request to be processed
149: * @param response Response to be produced
150: * @param valveContext Valve context used to forward to the next Valve
151: *
152: * @exception IOException if an input/output error occurred
153: * @exception ServletException if a servlet error occurred
154: */
155: public void invoke(Request request, Response response,
156: ValveContext valveContext) throws IOException,
157: ServletException {
158:
159: // Initialize local variables we may need
160: boolean unavailable = false;
161: Throwable throwable = null;
162: StandardWrapper wrapper = (StandardWrapper) getContainer();
163: ServletRequest sreq = request.getRequest();
164: ServletResponse sres = response.getResponse();
165: Servlet servlet = null;
166: HttpServletRequest hreq = null;
167: if (sreq instanceof HttpServletRequest)
168: hreq = (HttpServletRequest) sreq;
169: HttpServletResponse hres = null;
170: if (sres instanceof HttpServletResponse)
171: hres = (HttpServletResponse) sres;
172:
173: // Check for the application being marked unavailable
174: if (!((Context) wrapper.getParent()).getAvailable()) {
175: hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
176: sm.getString("standardContext.isUnavailable"));
177: unavailable = true;
178: }
179:
180: // Check for the servlet being marked unavailable
181: if (!unavailable && wrapper.isUnavailable()) {
182: log(sm.getString("standardWrapper.isUnavailable", wrapper
183: .getName()));
184: if (hres == null) {
185: ; // NOTE - Not much we can do generically
186: } else {
187: long available = wrapper.getAvailable();
188: if ((available > 0L) && (available < Long.MAX_VALUE))
189: hres.setDateHeader("Retry-After", available);
190: hres.sendError(
191: HttpServletResponse.SC_SERVICE_UNAVAILABLE,
192: sm.getString("standardWrapper.isUnavailable",
193: wrapper.getName()));
194: }
195: unavailable = true;
196: }
197:
198: // Allocate a servlet instance to process this request
199: try {
200: if (!unavailable) {
201: servlet = wrapper.allocate();
202: }
203: } catch (ServletException e) {
204: log(sm.getString("standardWrapper.allocateException",
205: wrapper.getName()), e);
206: throwable = e;
207: exception(request, response, e);
208: servlet = null;
209: } catch (Throwable e) {
210: log(sm.getString("standardWrapper.allocateException",
211: wrapper.getName()), e);
212: throwable = e;
213: exception(request, response, e);
214: servlet = null;
215: }
216:
217: // Acknowlege the request
218: try {
219: response.sendAcknowledgement();
220: } catch (IOException e) {
221: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
222: log(sm.getString("standardWrapper.acknowledgeException",
223: wrapper.getName()), e);
224: throwable = e;
225: exception(request, response, e);
226: } catch (Throwable e) {
227: log(sm.getString("standardWrapper.acknowledgeException",
228: wrapper.getName()), e);
229: throwable = e;
230: exception(request, response, e);
231: servlet = null;
232: }
233:
234: // Create the filter chain for this request
235: ApplicationFilterChain filterChain = createFilterChain(request,
236: servlet);
237:
238: // Call the filter chain for this request
239: // NOTE: This also calls the servlet's service() method
240: try {
241: String jspFile = wrapper.getJspFile();
242: if (jspFile != null)
243: sreq.setAttribute(Globals.JSP_FILE_ATTR, jspFile);
244: else
245: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
246: if ((servlet != null) && (filterChain != null)) {
247: filterChain.doFilter(sreq, sres);
248: }
249: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
250: } catch (IOException e) {
251: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
252: log(sm.getString("standardWrapper.serviceException",
253: wrapper.getName()), e);
254: throwable = e;
255: exception(request, response, e);
256: } catch (UnavailableException e) {
257: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
258: log(sm.getString("standardWrapper.serviceException",
259: wrapper.getName()), e);
260: // throwable = e;
261: // exception(request, response, e);
262: wrapper.unavailable(e);
263: long available = wrapper.getAvailable();
264: if ((available > 0L) && (available < Long.MAX_VALUE))
265: hres.setDateHeader("Retry-After", available);
266: hres.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
267: sm.getString("standardWrapper.isUnavailable",
268: wrapper.getName()));
269: // Do not save exception in 'throwable', because we
270: // do not want to do exception(request, response, e) processing
271: } catch (ServletException e) {
272: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
273: log(sm.getString("standardWrapper.serviceException",
274: wrapper.getName()), e);
275: throwable = e;
276: exception(request, response, e);
277: } catch (Throwable e) {
278: sreq.removeAttribute(Globals.JSP_FILE_ATTR);
279: log(sm.getString("standardWrapper.serviceException",
280: wrapper.getName()), e);
281: throwable = e;
282: exception(request, response, e);
283: }
284:
285: // Release the filter chain (if any) for this request
286: try {
287: if (filterChain != null)
288: filterChain.release();
289: } catch (Throwable e) {
290: log(sm.getString("standardWrapper.releaseFilters", wrapper
291: .getName()), e);
292: if (throwable == null) {
293: throwable = e;
294: exception(request, response, e);
295: }
296: }
297:
298: // Deallocate the allocated servlet instance
299: try {
300: if (servlet != null) {
301: wrapper.deallocate(servlet);
302: }
303: } catch (Throwable e) {
304: log(sm.getString("standardWrapper.deallocateException",
305: wrapper.getName()), e);
306: if (throwable == null) {
307: throwable = e;
308: exception(request, response, e);
309: }
310: }
311:
312: // If this servlet has been marked permanently unavailable,
313: // unload it and release this instance
314: try {
315: if ((servlet != null)
316: && (wrapper.getAvailable() == Long.MAX_VALUE)) {
317: wrapper.unload();
318: }
319: } catch (Throwable e) {
320: log(sm.getString("standardWrapper.unloadException", wrapper
321: .getName()), e);
322: if (throwable == null) {
323: throwable = e;
324: exception(request, response, e);
325: }
326: }
327:
328: }
329:
330: // -------------------------------------------------------- Private Methods
331:
332: /**
333: * Construct and return a FilterChain implementation that will wrap the
334: * execution of the specified servlet instance. If we should not execute
335: * a filter chain at all, return <code>null</code>.
336: * <p>
337: * <strong>FIXME</strong> - Pool the chain instances!
338: *
339: * @param request The servlet request we are processing
340: * @param servlet The servlet instance to be wrapped
341: */
342: private ApplicationFilterChain createFilterChain(Request request,
343: Servlet servlet) {
344:
345: // If there is no servlet to execute, return null
346: if (servlet == null)
347: return (null);
348:
349: // Create and initialize a filter chain object
350: ApplicationFilterChain filterChain = new ApplicationFilterChain();
351: filterChain.setServlet(servlet);
352: StandardWrapper wrapper = (StandardWrapper) getContainer();
353: filterChain.setSupport(wrapper.getInstanceSupport());
354:
355: // Acquire the filter mappings for this Context
356: StandardContext context = (StandardContext) wrapper.getParent();
357: FilterMap filterMaps[] = context.findFilterMaps();
358:
359: // If there are no filter mappings, we are done
360: if ((filterMaps == null) || (filterMaps.length == 0))
361: return (filterChain);
362: // if (debug >= 1)
363: // log("createFilterChain: Processing " + filterMaps.length +
364: // " filter map entries");
365:
366: // Acquire the information we will need to match filter mappings
367: String requestPath = null;
368: if (request instanceof HttpRequest) {
369: HttpServletRequest hreq = (HttpServletRequest) request
370: .getRequest();
371: String contextPath = hreq.getContextPath();
372: if (contextPath == null)
373: contextPath = "";
374: String requestURI = ((HttpRequest) request)
375: .getDecodedRequestURI();
376: if (requestURI.length() >= contextPath.length())
377: requestPath = requestURI
378: .substring(contextPath.length());
379: }
380: String servletName = wrapper.getName();
381: // if (debug >= 1) {
382: // log(" requestPath=" + requestPath);
383: // log(" servletName=" + servletName);
384: // }
385: int n = 0;
386:
387: // Add the relevant path-mapped filters to this filter chain
388: for (int i = 0; i < filterMaps.length; i++) {
389: // if (debug >= 2)
390: // log(" Checking path-mapped filter '" +
391: // filterMaps[i] + "'");
392: if (!matchFiltersURL(filterMaps[i], requestPath))
393: continue;
394: ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context
395: .findFilterConfig(filterMaps[i].getFilterName());
396: if (filterConfig == null) {
397: // if (debug >= 2)
398: // log(" Missing path-mapped filter '" +
399: // filterMaps[i] + "'");
400: ; // FIXME - log configuration problem
401: continue;
402: }
403: // if (debug >= 2)
404: // log(" Adding path-mapped filter '" +
405: // filterConfig.getFilterName() + "'");
406: filterChain.addFilter(filterConfig);
407: n++;
408: }
409:
410: // Add filters that match on servlet name second
411: for (int i = 0; i < filterMaps.length; i++) {
412: // if (debug >= 2)
413: // log(" Checking servlet-mapped filter '" +
414: // filterMaps[i] + "'");
415: if (!matchFiltersServlet(filterMaps[i], servletName))
416: continue;
417: ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) context
418: .findFilterConfig(filterMaps[i].getFilterName());
419: if (filterConfig == null) {
420: // if (debug >= 2)
421: // log(" Missing servlet-mapped filter '" +
422: // filterMaps[i] + "'");
423: ; // FIXME - log configuration problem
424: continue;
425: }
426: // if (debug >= 2)
427: // log(" Adding servlet-mapped filter '" +
428: // filterMaps[i] + "'");
429: filterChain.addFilter(filterConfig);
430: n++;
431: }
432:
433: // Return the completed filter chain
434: // if (debug >= 2)
435: // log(" Returning chain with " + n + " filters");
436: return (filterChain);
437:
438: }
439:
440: /**
441: * Handle the specified ServletException encountered while processing
442: * the specified Request to produce the specified Response. Any
443: * exceptions that occur during generation of the exception report are
444: * logged and swallowed.
445: *
446: * @param request The request being processed
447: * @param response The response being generated
448: * @param exception The exception that occurred (which possibly wraps
449: * a root cause exception
450: */
451: private void exception(Request request, Response response,
452: Throwable exception) {
453:
454: ServletRequest sreq = request.getRequest();
455: sreq.setAttribute(Globals.EXCEPTION_ATTR, exception);
456:
457: ServletResponse sresponse = response.getResponse();
458: if (sresponse instanceof HttpServletResponse)
459: ((HttpServletResponse) sresponse)
460: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
461:
462: }
463:
464: /**
465: * Log a message on the Logger associated with our Container (if any)
466: *
467: * @param message Message to be logged
468: */
469: private void log(String message) {
470:
471: Logger logger = null;
472: if (container != null)
473: logger = container.getLogger();
474: if (logger != null)
475: logger.log("StandardWrapperValve[" + container.getName()
476: + "]: " + message);
477: else {
478: String containerName = null;
479: if (container != null)
480: containerName = container.getName();
481: System.out.println("StandardWrapperValve[" + containerName
482: + "]: " + message);
483: }
484:
485: }
486:
487: /**
488: * Log a message on the Logger associated with our Container (if any)
489: *
490: * @param message Message to be logged
491: * @param throwable Associated exception
492: */
493: private void log(String message, Throwable throwable) {
494:
495: Logger logger = null;
496: if (container != null)
497: logger = container.getLogger();
498: if (logger != null)
499: logger.log("StandardWrapperValve[" + container.getName()
500: + "]: " + message, throwable);
501: else {
502: String containerName = null;
503: if (container != null)
504: containerName = container.getName();
505: System.out.println("StandardWrapperValve[" + containerName
506: + "]: " + message);
507: System.out.println("" + throwable);
508: throwable.printStackTrace(System.out);
509: }
510:
511: }
512:
513: /**
514: * Return <code>true</code> if the specified servlet name matches
515: * the requirements of the specified filter mapping; otherwise
516: * return <code>false</code>.
517: *
518: * @param filterMap Filter mapping being checked
519: * @param servletName Servlet name being checked
520: */
521: private boolean matchFiltersServlet(FilterMap filterMap,
522: String servletName) {
523:
524: // if (debug >= 3)
525: // log(" Matching servlet name '" + servletName +
526: // "' against mapping " + filterMap);
527:
528: if (servletName == null)
529: return (false);
530: else
531: return (servletName.equals(filterMap.getServletName()));
532:
533: }
534:
535: /**
536: * Return <code>true</code> if the context-relative request path
537: * matches the requirements of the specified filter mapping;
538: * otherwise, return <code>null</code>.
539: *
540: * @param filterMap Filter mapping being checked
541: * @param requestPath Context-relative request path of this request
542: */
543: private boolean matchFiltersURL(FilterMap filterMap,
544: String requestPath) {
545:
546: // if (debug >= 3)
547: // log(" Matching request path '" + requestPath +
548: // "' against mapping " + filterMap);
549:
550: if (requestPath == null)
551: return (false);
552:
553: // Match on context relative request path
554: String testPath = filterMap.getURLPattern();
555: if (testPath == null)
556: return (false);
557:
558: // Case 1 - Exact Match
559: if (testPath.equals(requestPath))
560: return (true);
561:
562: // Case 2 - Path Match ("/.../*")
563: if (testPath.equals("/*"))
564: return (true); // Optimize a common case
565: if (testPath.endsWith("/*")) {
566: String comparePath = requestPath;
567: while (true) {
568: if (testPath.equals(comparePath + "/*"))
569: return (true);
570: int slash = comparePath.lastIndexOf('/');
571: if (slash < 0)
572: break;
573: comparePath = comparePath.substring(0, slash);
574: }
575: return (false);
576: }
577:
578: // Case 3 - Extension Match
579: if (testPath.startsWith("*.")) {
580: int slash = requestPath.lastIndexOf('/');
581: int period = requestPath.lastIndexOf('.');
582: if ((slash >= 0) && (period > slash))
583: return (testPath.equals("*."
584: + requestPath.substring(period + 1)));
585: }
586:
587: // Case 4 - "Default" Match
588: return (false); // NOTE - Not relevant for selecting filters
589:
590: }
591:
592: }
|