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.arch.leaf;
027:
028: import java.io.IOException;
029:
030: import javax.servlet.Servlet;
031: import javax.servlet.ServletConfig;
032: import javax.servlet.ServletException;
033: import javax.servlet.ServletRequest;
034: import javax.servlet.ServletResponse;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletRequestWrapper;
037:
038: import org.cougaar.lib.web.arch.ServletRegistry;
039: import org.cougaar.lib.web.arch.util.PrefixMatch;
040:
041: /**
042: * This <code>Servlet</code> forwards service requests to the locally
043: * registered ("leaf") servlets, typically intra-agent servlets.
044: * <p>
045: * If the request-path is not registered then the provided
046: * "unknownPathServlet" is used.
047: */
048: public class LeafServlet implements Servlet {
049:
050: /**
051: * Local (path, Servlet) registry.
052: */
053: private final ServletRegistry servletReg;
054:
055: /**
056: * Servlet to handle unknown paths.
057: *
058: * @see ErrorServlet
059: */
060: private final Servlet unknownPathServlet;
061:
062: public LeafServlet(ServletRegistry servletReg,
063: Servlet unknownPathServlet) {
064: this .servletReg = servletReg;
065: this .unknownPathServlet = unknownPathServlet;
066:
067: // null-check
068: if (servletReg == null) {
069: throw new NullPointerException();
070: } else if (unknownPathServlet == null) {
071: throw new NullPointerException();
072: }
073:
074: try {
075: unknownPathServlet.init(getServletConfig());
076: } catch (ServletException se) {
077: throw new RuntimeException(
078: "Unable to initialize the unknown-path-servlet: "
079: + se.getMessage());
080: }
081: }
082:
083: public void init(ServletConfig config) throws ServletException {
084: servletReg.init(config);
085: }
086:
087: public ServletConfig getServletConfig() {
088: return servletReg.getServletConfig();
089: }
090:
091: public String getServletInfo() {
092: return "leaf-servlet";
093: }
094:
095: public void service(ServletRequest req, ServletResponse res)
096: throws ServletException, IOException {
097: HttpServletRequest hreq;
098: try {
099: hreq = (HttpServletRequest) req;
100: } catch (ClassCastException cce) {
101: // not an HTTP request?
102: throw new ServletException("non-HTTP request or response");
103: }
104:
105: // get the path
106: String path = hreq.getRequestURI();
107:
108: // look for "[/$[~]name][/innerPath]"
109: String innerPath = path;
110: int pathLength = (path == null ? 0 : path.length());
111: if (pathLength >= 2 && path.charAt(0) == '/'
112: && path.charAt(1) == '$') {
113: int i = path.indexOf('/', 2);
114: if (i < 0) {
115: i = pathLength;
116: }
117: innerPath = path.substring(i);
118: }
119:
120: // find the matching servlet
121: Object o = servletReg.get(innerPath);
122: Servlet servlet;
123: String pathInfo = null;
124: String servletPath = path;
125: if (o == null) {
126: servlet = unknownPathServlet; // no such path
127: } else if (o instanceof Servlet) {
128: servlet = (Servlet) o;
129: } else {
130: PrefixMatch pm = (PrefixMatch) o;
131: servlet = (Servlet) pm.getValue();
132: servletPath = pm.getPrefix();
133: pathInfo = pm.getTail();
134: }
135:
136: // override the request
137: ServletRequest sr = new MyRequestWrapper(hreq, pathInfo,
138: servletPath);
139:
140: // invoke the servlet
141: servlet.service(sr, res);
142: }
143:
144: public void destroy() {
145: // ignore -- we're using the dummy config...
146: }
147:
148: private static final class MyRequestWrapper extends
149: HttpServletRequestWrapper {
150: private final String pathInfo;
151: private final String servletPath;
152:
153: public MyRequestWrapper(HttpServletRequest req,
154: String pathInfo, String servletPath) {
155: super (req);
156: this .pathInfo = pathInfo;
157: this .servletPath = servletPath;
158: }
159:
160: public String getPathInfo() {
161: return pathInfo;
162: }
163:
164: public String getServletPath() {
165: return servletPath;
166: }
167: }
168: }
|