001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.lib.web.tomcat;
027:
028: import java.io.IOException;
029: import java.io.PrintWriter;
030:
031: import javax.servlet.Servlet;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletException;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.ServletResponse;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: /**
040: * A simple error servlet for PAGE-NOT-FOUND messages.
041: * <p>
042: * This allows the ServletEngine to display custom
043: * error messages with a 404 error-code.
044: * <p>
045: * <b>NOTE:</b> Tomcat 4+ encodes the error message, which prevents
046: * the servlet from embedding HTML in the error response. For
047: * example, all '<' characters are converted into "&lt;"
048: * strings. Servlets that need to generate an HTML response should
049: * use (the discouraged) "setStatus(..)" instead of "sendError(..)"
050: * response method. See bug 1259 for details.
051: */
052: public class ErrorServlet implements Servlet {
053:
054: public void service(ServletRequest req, ServletResponse res)
055: throws ServletException, IOException {
056: HttpServletRequest httpReq;
057: HttpServletResponse httpRes;
058: try {
059: httpReq = (HttpServletRequest) req;
060: httpRes = (HttpServletResponse) res;
061: } catch (ClassCastException cce) {
062: // not an http request?
063: throw new ServletException("non-HTTP request or response");
064: }
065:
066: httpRes.setContentType("text/html");
067:
068: // Integer statusObj = (Integer)
069: // req.getAttribute("javax.servlet.error.status_code");
070: // int statusInt =
071: // ((statusObj != null) ? statusObj.getValue() : -1);
072: // assert (HttpServletResponse.SC_NOT_FOUND == statusInt);
073:
074: httpRes.setStatus(HttpServletResponse.SC_NOT_FOUND);
075:
076: String msg = (String) req
077: .getAttribute("javax.servlet.error.message");
078:
079: PrintWriter out = httpRes.getWriter();
080:
081: out.print("<html><head><title>" + msg
082: + " (404)</title></head>\n" + "<body><h1>" + msg
083: + " (404)</h1>\n" + "<p>"
084: + "<b>Not found request:</b> "
085: + httpReq.getRequestURI() + "</body></html>\n");
086: out.close();
087: }
088:
089: private ServletConfig config;
090:
091: public void init(ServletConfig config) {
092: this .config = config;
093: }
094:
095: public ServletConfig getServletConfig() {
096: return config;
097: }
098:
099: public String getServletInfo() {
100: return "error";
101: }
102:
103: public void destroy() {
104: // ignore
105: }
106:
107: }
|