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:
027: package org.cougaar.lib.web.service;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031:
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletConfig;
034: import javax.servlet.ServletException;
035: import javax.servlet.ServletRequest;
036: import javax.servlet.ServletResponse;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: /**
041: * A "/$name[/.*]" level servlet that:<ol>
042: * <li>generates a help message for "/$name" and "/$name/"
043: * requests, and</li>
044: * <li>generates an error message for all other (unknown)
045: * paths</li>
046: * </ol>.
047: */
048: public class UnknownLeafPathServlet implements Servlet {
049:
050: private final String realName;
051:
052: public UnknownLeafPathServlet(String realName) {
053: this .realName = realName;
054:
055: String s = (realName == null ? "realName" : null);
056: if (s != null) {
057: throw new IllegalArgumentException("null " + s);
058: }
059: }
060:
061: public void service(ServletRequest sreq, ServletResponse sres)
062: throws ServletException, IOException {
063:
064: // cast
065: HttpServletRequest req = (HttpServletRequest) sreq;
066: HttpServletResponse res = (HttpServletResponse) sres;
067:
068: // get the "/$name[/.*]"
069: String path = req.getRequestURI();
070:
071: String name = realName;
072: String trimPath = null;
073:
074: int pathLength = (path == null ? 0 : path.length());
075: if (pathLength > 1) {
076: if (pathLength >= 2 && path.charAt(0) == '/'
077: && path.charAt(1) == '$') {
078: int j = path.indexOf('/', 2);
079: if (j < 0) {
080: j = pathLength;
081: } else if (j < (pathLength - 1)) {
082: trimPath = path.substring(j);
083: }
084: if (j > 2) {
085: name = path.substring(2, j);
086: }
087: } else {
088: trimPath = path;
089: }
090: }
091:
092: boolean is_error = (trimPath != null);
093:
094: res.setContentType("text/html");
095: if (is_error) {
096: // generate an HTML error response, with a 404 error code.
097: //
098: // use "setStatus" instead of "sendError" -- see bug 1259
099: res.setStatus(HttpServletResponse.SC_NOT_FOUND);
100: res.addHeader("Cougaar-error", "path");
101: }
102:
103: PrintWriter out = res.getWriter();
104:
105: String title = (is_error ? "\"" + path + "\" Not Found on "
106: : "")
107: + "Agent \""
108: + name
109: + "\""
110: + (name.equals(realName) ? "" : " (" + realName + ")");
111:
112: out.print("<html><head><title>"
113: + title
114: + "</title></head><body>\n"
115: + (is_error ? ("<p><h1>" + title + "</h1>\n") : ("<h2>"
116: + title + "</h2>\n")) + "<p>Options:<ul>\n"
117: + "<li><a href=\"/$" + name + "/list\">"
118: + "List <b>paths</b> in agent \"" + name + "\""
119: + "</a></li><p>\n" + "<li><a href=\"/$" + name
120: + "/agents\">"
121: + "List <b>local agents</b> co-located with agent \""
122: + name + "\"" + "</a></li><p>\n" + "<li><a href=\"/$"
123: + name + "/agents?suffix=.\">"
124: + "List <b>all agents</b> in the society"
125: + "</a></li>\n" + "</ul>\n" + "</body></html>");
126: out.close();
127: }
128:
129: // etc
130: private ServletConfig config;
131:
132: public void init(ServletConfig config) {
133: this .config = config;
134: }
135:
136: public ServletConfig getServletConfig() {
137: return config;
138: }
139:
140: public String getServletInfo() {
141: return "unknown-leaf-path";
142: }
143:
144: public void destroy() {
145: }
146:
147: }
|