001: /*
002: * <copyright>
003: *
004: * Copyright 2003-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.aggagent.servlet;
027:
028: import java.io.PrintWriter;
029: import java.net.URLEncoder;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import javax.servlet.http.HttpServletRequest;
034:
035: import org.cougaar.core.service.BlackboardService;
036: import org.cougaar.core.service.UIDService;
037: import org.cougaar.lib.aggagent.query.AggregationQuery;
038: import org.cougaar.lib.aggagent.query.Alert;
039: import org.cougaar.lib.aggagent.query.AlertDescriptor;
040: import org.cougaar.lib.aggagent.query.QueryResultAdapter;
041: import org.cougaar.lib.aggagent.test.CycleSizeAlert;
042: import org.cougaar.lib.aggagent.util.Enum.QueryType;
043:
044: public class AggregationHTMLInterface extends
045: AggregationServletInterface {
046: private String servletName;
047:
048: public AggregationHTMLInterface(BlackboardService bs,
049: SubscriptionMonitorSupport sms, String servletName,
050: UIDService uidService) {
051: super (bs, sms, uidService);
052:
053: // remove first forward slash
054: this .servletName = servletName.trim().substring(1);
055: }
056:
057: public void handleRequest(PrintWriter out,
058: HttpServletRequest request) {
059: // print the header
060: out
061: .println("<html><head><title>Aggregation Servlet</title></head>");
062:
063: if (!request.getParameterNames().hasMoreElements()) {
064: sendMainFrame(out);
065: } else {
066: out.println("<body BGCOLOR=\"#EEEEEE\">");
067:
068: // check the parameters and decide what to do
069: if (request.getParameter("CREATE_QUERY") != null)
070: publishHTMLQuery(request, out);
071: else if (request.getParameter("CREATE_QUERY_FORM") != null)
072: HTMLPresenter.sendQueryForm(servletName, out, true);
073: else if (request.getParameter("CREATE_TRAN_QUERY_FORM") != null)
074: HTMLPresenter.sendQueryForm(servletName, out, false);
075: else if (request.getParameter("REPORT_QUERY") != null)
076: generateQueryReport(request, out);
077: else if (request.getParameter("CANCEL_QUERY") != null)
078: removeQuery(request, out);
079: else if (request.getParameter("HOME") != null)
080: sendHomePage(out);
081: else if (request.getParameter("TITLE") != null)
082: sendTitle(out);
083: else if (request.getParameter("DEFAULT_ALERT") != null)
084: addDefaultAlert(out);
085: else if (request.getParameter("ACTIVE_ALERTS") != null)
086: checkActiveAlerts(out);
087: else if (request.getParameter("ADD_ALERT_FORM") != null)
088: HTMLPresenter.sendAlertForm(servletName, request, out);
089: else if (request.getParameter("ADD_ALERT") != null)
090: processAlertForm(request, out);
091: else
092: sendHomePage(out);
093:
094: out.println("</body>");
095: }
096:
097: // print trailer and flush
098: out.println("</html>");
099: out.flush();
100: }
101:
102: private void processAlertForm(HttpServletRequest request,
103: PrintWriter out) {
104: // create the alert
105: AlertDescriptor ad = HTMLPresenter.processAlertForm(request);
106:
107: // find the indicated query, if any
108: Iterator i = query(new QuerySeeker(ad.getQueryId())).iterator();
109: if (i.hasNext()) {
110: QueryResultAdapter qra = (QueryResultAdapter) i.next();
111: try {
112: Alert a = ad.createAlert();
113: qra.addAlert(a);
114: publishAdd(a);
115: checkActiveAlerts(out);
116: } catch (Exception e) {
117: out.println("<h2>Alert Request Error</h2>");
118: out.println("The Alert could not be constructed.");
119: e.printStackTrace(out);
120: }
121: } else {
122: out.println("<h2>Alert Request Error</h2>");
123: out
124: .println("The query does not exist. The alert has been cancelled.");
125: }
126: }
127:
128: private void sendMainFrame(PrintWriter out) {
129: out.println("<FRAMESET ROWS=\"10%, 90%\">");
130: out.println("<FRAME NAME=\"title\" SRC=\"" + servletName
131: + "?TITLE=foo\">");
132: out.println("<FRAMESET COLS=\"30%, 70%\">");
133: out.println("<FRAME NAME=\"menu\" SRC=\"" + servletName
134: + "?HOME=foo\">");
135: out.println("<FRAME NAME=\"data\" SRC=\"about:blank\">");
136: out.println("</FRAMESET></FRAMESET>");
137: }
138:
139: private void sendTitle(PrintWriter out) {
140: out.println("<H1 align=\"center\">Aggregation Servlet</H1>");
141: }
142:
143: private void sendHomePage(PrintWriter out) {
144: out.println("<P><h3>Persistent Queries</h3>");
145: summarizeQueries(out);
146: out.println(selfLink("Create", "CREATE_QUERY_FORM", null,
147: "data")
148: + " new persistent query");
149: out.println("<br><br><br>");
150: out.println("<P><h3>Transient Queries</h3>");
151: out.println(selfLink("Create", "CREATE_TRAN_QUERY_FORM", null,
152: "data")
153: + " new transient query");
154: out.println("<br><br><br>");
155: out.println("<P><h3>Alerts<BR>on Aggregation Agent</h3>");
156: out.println(selfLink("Add Default", "DEFAULT_ALERT", null,
157: "menu"));
158: out.println("<br><br>");
159: out.println(selfLink("Check for Alerts", "ACTIVE_ALERTS", null,
160: "data"));
161: }
162:
163: //
164: // Check for alerts on the blackboard
165: //
166: private void checkActiveAlerts(PrintWriter out) {
167: out.println("checking for active alerts ... <br>");
168: Collection c = query(new AlertSeeker());
169: out.println("Found " + c.size() + " alerts:<br><center>");
170: HTMLPresenter.sendAlertSummary(c, out);
171: }
172:
173: //
174: // Add the default Alert-ridden query to the blackboard
175: //
176: private void addDefaultAlert(PrintWriter out) {
177: QueryResultAdapter qra = new QueryResultAdapter(CycleSizeAlert
178: .createDefaultQuery(), getUIDService().nextUID());
179: Alert ale = CycleSizeAlert.getDefaultAlert();
180: qra.addAlert(ale);
181: publishAdd(qra);
182: publishAdd(ale);
183: sendHomePage(out);
184: }
185:
186: private void publishHTMLQuery(HttpServletRequest request,
187: PrintWriter out) {
188: // parse form post request
189: AggregationQuery aq = HTMLPresenter.processQueryForm(request);
190: QueryResultAdapter qra = new QueryResultAdapter(aq,
191: getUIDService().nextUID());
192: publishAdd(qra);
193:
194: if (aq.getType() == QueryType.PERSISTENT) {
195: // update menu frame with new query
196: sendHomePage(out);
197: } else {
198: if (aq.timeoutSupplied())
199: waitForAndReturnResults(qra.getID(), out, false, aq
200: .getTimeout());
201: else
202: waitForAndReturnResults(qra.getID(), out, false);
203: }
204: }
205:
206: private void removeQuery(HttpServletRequest in, PrintWriter out) {
207: String queryId = in.getParameter("SESSION_ID");
208: findAndRemoveQuery(queryId);
209: sendHomePage(out);
210: }
211:
212: private void generateQueryReport(HttpServletRequest in,
213: PrintWriter out) {
214: String id = in.getParameter("SESSION_ID");
215: QueryResultAdapter qra = findQuery(id);
216: printQueryReportPage(qra, out);
217: }
218:
219: private void summarizeQueries(PrintWriter out) {
220: Collection qs = query(new QuerySeeker());
221: Iterator i = qs.iterator();
222: if (i.hasNext()) {
223: out.println("Currently active queries:");
224: out.println("<TABLE>");
225: while (i.hasNext()) {
226: QueryResultAdapter qra = (QueryResultAdapter) i.next();
227: String name = qra.getQuery().getName();
228: String alertPragma = "ADD_ALERT_FORM";
229:
230: try {
231: if (name != null)
232: alertPragma = "QUERY="
233: + URLEncoder.encode(name, "UTF-8")
234: + "&" + alertPragma;
235: } catch (java.io.UnsupportedEncodingException e) {
236: e.printStackTrace();
237: }
238:
239: String queryViewLink = selfLink(qra.getQuery()
240: .getName()
241: + " (" + qra.getID() + ") ", "REPORT_QUERY",
242: qra.getID(), "data");
243: String queryCancelLink = selfLink("cancel",
244: "CANCEL_QUERY", qra.getID(), "menu");
245: String addAlertLink = selfLink("add alert",
246: alertPragma, qra.getID(), "data");
247:
248: out.println("<TR><TD> "
249: + queryViewLink + "</td><td> ["
250: + addAlertLink + "]" + "</TD><TD> ["
251: + queryCancelLink + "]" + "</TD></TR>");
252: }
253: out.println("</TABLE><BR>");
254: } else {
255: out.println("There are no active queries.");
256: out.println("<BR><BR>");
257: }
258: }
259:
260: private String selfLink(String text, String pragma, String id,
261: String target) {
262: String hl = null;
263:
264: try {
265: StringBuffer buf = new StringBuffer(servletName);
266: if (pragma != null) {
267: buf.append("?");
268: buf.append(pragma);
269: buf.append("=1");
270: if (id != null) {
271: buf.append("&");
272: buf.append("SESSION_ID");
273: buf.append("=");
274: buf.append(URLEncoder.encode(id, "UTF-8"));
275: }
276: }
277: hl = hyperlink(text, buf.toString(), target);
278: } catch (java.io.UnsupportedEncodingException e) {
279: e.printStackTrace();
280: }
281:
282: return hl;
283: }
284:
285: private static String hyperlink(String text, String url,
286: String target) {
287: StringBuffer buf = new StringBuffer("<a href=\"");
288: buf.append(url);
289: buf.append("\" TARGET=\"" + target + "\">");
290: buf.append(text);
291: buf.append("</a>");
292: return buf.toString();
293: }
294: }
|