001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.planning.servlet;
028:
029: import java.io.FilterOutputStream;
030: import java.io.IOException;
031: import java.io.ObjectOutputStream;
032: import java.io.OutputStream;
033: import java.io.OutputStreamWriter;
034: import java.io.PrintWriter;
035:
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.cougaar.core.servlet.ServletUtil;
041: import org.cougaar.core.servlet.SimpleServletSupport;
042: import org.cougaar.planning.servlet.data.xml.XMLWriter;
043: import org.cougaar.planning.servlet.data.xml.XMLable;
044:
045: /**
046: * <pre>
047: * ServletWorker is the base class for all servlet workers.
048: *
049: * ServletWorkers assume that the result can be returned in any of
050: * three formats : html, xml, or serialized java object. These
051: * formats are specified by the URL format parameter, i.e.
052: * http://localhost:8800/$AGENT_NAME/SERVLET_NAME?format=html
053: *
054: * The format parameter is set in the getSettings method. This should
055: * be called from a subclass if overridden.
056: *
057: * Defines the writeResponse method, which returns the type of result
058: * indicated by the format parameter to the output stream of the servlet
059: * response.
060: *
061: * Note that if you make a mistake and don't provide a value for a
062: * parameter, e.g.
063: * http://localhost:8800/$AGENT_NAME/SERVLET_NAME?format
064: * ^^^^ - no value
065: * the ServletUtil object will throw an exception.
066: *
067: * </pre>
068: */
069: public class ServletWorker {
070: public static final boolean DEBUG = false;
071:
072: public static boolean VERBOSE = false;
073: public static final int FORMAT_DATA = 0;
074: public static final int FORMAT_XML = 1;
075: public static final int FORMAT_HTML = 2;
076:
077: static {
078: VERBOSE = Boolean
079: .getBoolean("org.cougaar.mlm.ui.psp.transit.ServletWorker.verbose");
080: }
081:
082: protected int format;
083:
084: /**
085: * Main method. <p>
086: * Most of the work is done in getHierarchy.
087: * This method mainly checks that the parameters are the right
088: * number and sets the format and recurse fields.
089: * <p>
090: * Uses the ServletUtil to parse the parameters.
091: * @see org.cougaar.core.servlet.ServletUtil#parseParams
092: */
093: public void execute(HttpServletRequest request,
094: HttpServletResponse response, SimpleServletSupport support)
095: throws IOException, ServletException {
096: ServletUtil.ParamVisitor vis = new ServletUtil.ParamVisitor() {
097: public void setParam(String name, String value) {
098: getSettings(name, value);
099: }
100: };
101:
102: // visit the URL parameters
103: ServletUtil.parseParams(vis, request);
104:
105: // generate our response.
106: }
107:
108: /**
109: * <pre>
110: * sets format
111: *
112: * format is either data, xml, or html
113: *
114: * see class description for what these values mean
115: * </pre>
116: */
117: protected void getSettings(String name, String value) {
118: if (eq("format", name)) {
119: if (eq("data", value)) {
120: format = FORMAT_DATA;
121: } else if (eq("xml", value)) {
122: format = FORMAT_XML;
123: } else if (eq("html", value)) {
124: format = FORMAT_HTML;
125: }
126: // stay backwards-compatable
127: } else if (eq("data", name)) {
128: format = FORMAT_DATA;
129: } else if (eq("xml", name)) {
130: format = FORMAT_XML;
131: } else if (eq("html", name)) {
132: format = FORMAT_HTML;
133: }
134: }
135:
136: protected boolean isHtmlFormat() {
137: return (format == FORMAT_HTML);
138: }
139:
140: protected boolean isXmlFormat() {
141: return (format == FORMAT_XML);
142: }
143:
144: protected boolean isDataFormat() {
145: return (format == FORMAT_DATA);
146: }
147:
148: protected String getPrefix() {
149: return "";
150: }
151:
152: /**
153: * Write XMLable result to output. <p>
154: *
155: * Output format is either data, xml, or html <p>
156: *
157: * For an example of how this is used, see the following references.
158: * @see org.cougaar.planning.servlet.HierarchyWorker#getHierarchy
159: * @see org.cougaar.planning.servlet.HierarchyWorker#writeResponse
160: */
161: protected void writeResponse(XMLable result, OutputStream out,
162: HttpServletRequest request, SimpleServletSupport support,
163: int format) {
164: // write data
165: try {
166: if (format == FORMAT_DATA) {
167: // serialize
168: ObjectOutputStream oos = new ObjectOutputStream(out);
169: oos.writeObject(result);
170: oos.flush();
171: } else {
172: // xml or html-wrapped xml
173: XMLWriter w;
174: PrintWriter writer = new PrintWriter(out);
175:
176: if (format == FORMAT_HTML) {
177: // wrapped xml
178: writer.println("<HTML><HEAD><TITLE> " + getPrefix()
179: + support.getAgentIdentifier()
180: + "</TITLE></HEAD><BODY>\n"
181: + "<H2><CENTER>" + getPrefix()
182: + support.getAgentIdentifier()
183: + "</CENTER></H2><p><pre>\n");
184: writer.flush();
185: w = new XMLWriter(new OutputStreamWriter(
186: new XMLtoHTMLOutputStream(out)), true);
187: } else {
188: // raw xml
189: writer.println("<?xml version='1.0' ?>");
190: writer.flush();
191: w = new XMLWriter(new OutputStreamWriter(out));
192: }
193: // write as xml
194: result.toXML(w);
195: w.flush();
196: if (format == FORMAT_HTML) {
197: writer.println("\n</pre></BODY></HTML>\n");
198: }
199: }
200: } catch (IOException e) {
201: e.printStackTrace();
202: }
203: }
204:
205: /**
206: * Convert XML to HTML-friendly output.
207: *
208: * Taken from PSP_PlanView. For Internet Explorer this isn't such
209: * a big deal...
210: */
211: protected static class XMLtoHTMLOutputStream extends
212: FilterOutputStream {
213: protected static final byte[] LESS_THAN;
214: protected static final byte[] GREATER_THAN;
215: static {
216: LESS_THAN = "<font color=green><".getBytes();
217: GREATER_THAN = "></font>".getBytes();
218: }
219:
220: public XMLtoHTMLOutputStream(OutputStream o) {
221: super (o);
222: }
223:
224: public void write(int b) throws IOException {
225: if (b == '<') {
226: out.write(LESS_THAN);
227: } else if (b == '>') {
228: out.write(GREATER_THAN);
229: } else {
230: out.write(b);
231: }
232: }
233: }
234:
235: public static final boolean eq(String a, String b) {
236: return a.regionMatches(true, 0, b, 0, a.length());
237: }
238: }
|