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.core.servlet;
028:
029: import java.io.PrintWriter;
030:
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.cougaar.core.component.ServiceBroker;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.node.NodeIdentificationService;
038: import org.cougaar.core.service.ServletService;
039:
040: /**
041: * Base class for serlvets that generate a per-agent data page.
042: * <p>
043: * The page is split into three frames, with the agent selection
044: * at the top, data in the middle, and a refresh button at the
045: * bottom. The user can select a different agent and press the
046: * refresh button to refresh the data page. The bottom frame
047: * also allows the user to set a timer-based data refresh, where
048: * zero stops the timer.
049: *
050: * @see org.cougaar.core.servlet.ServletMultiFrameset adds a
051: * second drop-down list for additional data filtering
052: */
053: public abstract class ServletFrameset extends HttpServlet {
054:
055: // url paramter
056: protected static final String FRAME = "frame";
057:
058: // values for the FRAME url parameter
059: protected static final String DATA_FRAME = "dataFrame";
060: protected static final String BOTTOM_FRAME = "bottomFrame";
061: protected static final String AGENT_FRAME = "agentFrame";
062:
063: protected static final String BOTTOM_FORM = "bottomForm";
064:
065: // url parameter for periodic data refresh
066: protected static final String REFRESH_FIELD_PARAM = "refresh";
067:
068: private MessageAddress nodeAddr;
069:
070: public ServletFrameset(ServiceBroker sb) {
071: // which node are we in?
072: NodeIdentificationService nis = (NodeIdentificationService) sb
073: .getService(this , NodeIdentificationService.class, null);
074: nodeAddr = nis.getMessageAddress();
075: sb.releaseService(this , NodeIdentificationService.class, nis);
076:
077: // Register our servlet.
078: ServletService servletService = (ServletService) sb.getService(
079: this , ServletService.class, null);
080: if (servletService == null) {
081: throw new RuntimeException(
082: "Unable to obtain ServletService");
083: }
084: try {
085: servletService.register(getPath(), this );
086: } catch (Exception e) {
087: throw new RuntimeException(
088: "Unable to register servlet at path <" + getPath()
089: + ">: " + e.getMessage());
090: }
091: }
092:
093: /** @return the local node's address */
094: protected final MessageAddress getNodeID() {
095: return nodeAddr;
096: }
097:
098: /** Get the page title */
099: public abstract String getTitle();
100:
101: /** Get the servlet path */
102: public abstract String getPath();
103:
104: /** Print the data contents. */
105: public abstract void printPage(HttpServletRequest request,
106: PrintWriter out);
107:
108: /** Print additional text for the bottom frame */
109: public void printBottomPage(HttpServletRequest request,
110: PrintWriter out) {
111: }
112:
113: //
114: // The rest is fine for most subclasses
115: //
116:
117: protected int topPercentage() {
118: return 10;
119: }
120:
121: protected int dataPercentage() {
122: return 80;
123: }
124:
125: protected int bottomPercentage() {
126: return 10;
127: }
128:
129: protected String getMiddleFrame() {
130: return DATA_FRAME;
131: }
132:
133: private void printRefreshForm(HttpServletRequest request,
134: PrintWriter out) {
135:
136: // get the refresh value and let the user select a new value
137: int refreshSeconds = 0;
138: String refresh = request.getParameter(REFRESH_FIELD_PARAM);
139: if (refresh != null) {
140: try {
141: refreshSeconds = Integer.parseInt(refresh);
142: } catch (Exception e) {
143: // If no good time specified, use default of 0
144: }
145: }
146:
147: out
148: .print("<table><tr>"
149: + "<td valign=\"middle\">Refresh (in seconds):</td>"
150: + "<td valign=\"middle\"><input type=\"text\" size=3 name=\""
151: + REFRESH_FIELD_PARAM + "\"" + " value=\"");
152: out.print(refreshSeconds);
153: out.print("\"" + "></td>" + "<td valign=\"middle\">"
154: + "<input type=\"submit\" name=\"action\""
155: + " value=\"Refresh\"></td>" + "</tr></table>");
156: }
157:
158: protected void writeJavascript(PrintWriter out) {
159: out.print("<script language=\"JavaScript\">\n" + "<!--\n"
160: + "function mySubmit() {\n"
161: + " // make sure an agent was selected\n"
162: + " var topObj = top." + AGENT_FRAME
163: + ".document.agent.name;\n"
164: + " var encAgent = topObj.value;\n"
165: + " if (encAgent.charAt(0) == '.') {\n"
166: + " alert(\"Please select an agent name\")\n"
167: + " return false;\n" + " }\n" + " document."
168: + BOTTOM_FORM + ".target=\"" + DATA_FRAME + "\"\n"
169: + " document." + BOTTOM_FORM
170: + ".action=\"/$\"+encAgent+\"" + getPath() + "\"\n"
171: + " return true\n" + "}\n" + "// -->\n"
172: + "</script>\n");
173: }
174:
175: // not abstract, but a no-op by default
176: protected void printAdditionalBottomFields(PrintWriter out) {
177: }
178:
179: private void printBottomFrame(HttpServletRequest request,
180: PrintWriter out) {
181: out.print("<html><HEAD>");
182:
183: // write javascript
184: writeJavascript(out);
185:
186: // begin form
187: out.print("<form name=\"" + BOTTOM_FORM + "\" method=\"get\" "
188: + "onSubmit=\"return mySubmit()\">\n"
189: + "<input type=hidden name=\"" + FRAME + "\" value=\""
190: + DATA_FRAME + "\">\n");
191:
192: printAdditionalBottomFields(out);
193:
194: printRefreshForm(request, out);
195:
196: // end form
197: out.print("</form>");
198:
199: printBottomPage(request, out);
200:
201: out.print("</body></html>\n");
202: }
203:
204: private void printDataFrame(HttpServletRequest request,
205: PrintWriter out) {
206:
207: out.print("<html><head>");
208:
209: // get the refresh value and make this page refresh
210: String refresh = request.getParameter(REFRESH_FIELD_PARAM);
211: if (refresh != null) {
212: int refreshSeconds = 0;
213: try {
214: refreshSeconds = Integer.parseInt(refresh);
215: } catch (Exception e) {
216: // If not good time specified, use the default of 0
217: }
218: if (refreshSeconds > 0) {
219: out.print("<META HTTP-EQUIV=\"refresh\" content=\"");
220: out.print(refreshSeconds);
221: out.print("\">");
222: }
223: }
224:
225: out.print("<title>");
226: out.print(getTitle());
227: out.print("</title></head><body><h1>");
228: out.print(getTitle());
229: out.print("</h1>");
230:
231: out.print("Node: " + nodeAddr + "<br>");
232: out.print("Date: ");
233: out.print(new java.util.Date());
234:
235: printPage(request, out);
236:
237: out.print("</body></html>\n");
238: }
239:
240: private void printOuterFrame(HttpServletRequest request,
241: PrintWriter out) {
242: // Header
243: out.print("<html><head><title>");
244: out.print(getTitle());
245: out.print("</title></head>");
246:
247: // Frameset
248: out.print("<frameset rows=\"");
249: out.print(topPercentage());
250: out.print("%,");
251: out.print(dataPercentage());
252: out.print("%,");
253: out.print(bottomPercentage());
254: out.print("%\">\n");
255:
256: // Top
257: out.print("<frame src=\"/agents?format=select&suffix=");
258: out.print(nodeAddr);
259: out.print("\" name=\"agentFrame\">\n");
260:
261: String middleFrame = getMiddleFrame();
262:
263: // Middle
264: out.print("<frame src=\"");
265: out.print(request.getRequestURI());
266: out.print("?");
267: out.print(FRAME);
268: out.print("=");
269: out.print(middleFrame);
270: out.print("\" name=\"");
271: out.print(middleFrame);
272: out.print("\">\n");
273:
274: // Bottom
275: out.print("<frame src=\"");
276: out.print(request.getRequestURI());
277: out.print("?");
278: out.print(FRAME);
279: out.print("=");
280: out.print(BOTTOM_FRAME);
281: out.print("\" name=\"");
282: out.print(BOTTOM_FRAME);
283: out.print("\">\n");
284:
285: // End frameset
286: out.print("</frameset>\n");
287:
288: // Frameless browser hack
289: out.print("<noframes>Please enable frame support</noframes>");
290:
291: // End
292: out.print("</html>\n");
293: }
294:
295: protected void printFrame(String frame, HttpServletRequest request,
296: PrintWriter out) {
297: if (DATA_FRAME.equals(frame)) {
298: printDataFrame(request, out);
299: } else if (BOTTOM_FRAME.equals(frame)) {
300: printBottomFrame(request, out);
301: } else {
302: printOuterFrame(request, out);
303: }
304: }
305:
306: public final void doGet(HttpServletRequest request,
307: HttpServletResponse response) throws java.io.IOException {
308: response.setContentType("text/html");
309: try {
310: PrintWriter out = response.getWriter();
311: String frame = request.getParameter(FRAME);
312: printFrame(frame, request, out);
313: out.close();
314: } catch (Exception e) {
315: e.printStackTrace();
316: }
317: }
318:
319: }
|