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: import java.util.Collections;
032: import java.util.List;
033:
034: import javax.servlet.Servlet;
035: import javax.servlet.ServletConfig;
036: import javax.servlet.ServletException;
037: import javax.servlet.ServletRequest;
038: import javax.servlet.ServletResponse;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: import org.cougaar.lib.web.arch.ServletRegistry;
043:
044: /**
045: * A servlet that displays all the paths in a <code>ServletRegistry</code>,
046: * for <u>leaf</u> servlet service use (within an Agent).
047: * <p>
048: * <ul>
049: * <li>"?format=html" -- generate a pretty html
050: * page (default)</li>
051: * <li>"?format=text -- generate plain text, one path
052: * name per line (instead of "?format=html")</li>
053: * <li>"?sorted=true -- sort the paths in alphabetical
054: * order (default)</li>
055: * <li>"?sorted=false" -- don't sort the paths into
056: * alphabetical order (instead of "?sorted=true")</li>
057: * </ul>
058: */
059: public class ListRegistryServlet implements Servlet {
060:
061: // read-only registry
062: private final ServletRegistry reg;
063: private final String realName;
064:
065: public ListRegistryServlet(ServletRegistry reg, String realName) {
066: this .reg = reg;
067: this .realName = realName;
068: //
069: String s = (reg == null ? "reg" : realName == null ? "realName"
070: : null);
071: if (s != null) {
072: throw new IllegalArgumentException("null " + s);
073: }
074: }
075:
076: public void service(ServletRequest sreq, ServletResponse sres)
077: throws ServletException, IOException {
078:
079: // cast
080: HttpServletRequest req = (HttpServletRequest) sreq;
081: HttpServletResponse res = (HttpServletResponse) sres;
082:
083: // scan url-parameters for:
084: // "?format=[text|html]"
085: // "?sorted=[true|false]"
086: boolean useHtml = !("text".equals(req.getParameter("format")));
087: boolean sorted = !("false".equals(req.getParameter("sorted")));
088:
089: // get the "/$name[/.*]"
090: String path = req.getRequestURI();
091: int pathLength = (path == null ? 0 : path.length());
092: String name = realName;
093: String prefix = "";
094: if (pathLength > 2 && path.charAt(0) == '/'
095: && path.charAt(1) == '$') {
096: int j = path.indexOf('/', 2);
097: if (j < 0) {
098: j = pathLength;
099: }
100: if (j > 2) {
101: name = path.substring(2, j);
102: }
103: prefix = path.substring(0, j);
104: }
105:
106: List pathList = reg.listNames();
107: int n = ((pathList != null) ? pathList.size() : 0);
108:
109: if (sorted && (n > 0)) {
110: Collections.sort(pathList);
111: }
112:
113: res.setContentType((useHtml ? "text/html" : "text/plain"));
114:
115: PrintWriter out = res.getWriter();
116:
117: if (useHtml) {
118: String title = "List of \""
119: + name
120: + "\""
121: + (name.equals(realName) ? ""
122: : (" (" + realName + ")")) + " Servlets";
123: out.print("<html><head><title>" + title
124: + "</title></head>\n" + "<body><p><h1>" + title
125: + "</h1>\n" + "<p><ol>\n");
126: for (int i = 0; i < n; i++) {
127: String pi = (String) pathList.get(i);
128: out.print("<li><a href=\"" + prefix + pi + "\">"
129: + prefix + pi + "</a></li>\n");
130: }
131: out.print("</ol></body></html>\n");
132: } else {
133: for (int i = 0; i < n; i++) {
134: String pi = (String) pathList.get(i);
135: out.println(prefix + pi);
136: }
137: }
138: out.close();
139: }
140:
141: // etc
142: private ServletConfig config;
143:
144: public void init(ServletConfig config) {
145: this .config = config;
146: }
147:
148: public ServletConfig getServletConfig() {
149: return config;
150: }
151:
152: public String getServletInfo() {
153: return "list-registry";
154: }
155:
156: public void destroy() {
157: }
158:
159: }
|