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.mts;
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.node.NodeIdentificationService;
037: import org.cougaar.core.service.ServletService;
038:
039: /**
040: * Basic {@link javax.servlet.Servlet} with a refresh URL-parameter.
041: */
042: public abstract class BaseServlet extends HttpServlet {
043: private MessageAddress nodeID;
044:
045: public BaseServlet(ServiceBroker sb) {
046: ServletService servletService = (ServletService) sb.getService(
047: this , ServletService.class, null);
048: if (servletService == null) {
049: throw new RuntimeException(
050: "Unable to obtain ServletService");
051: }
052:
053: NodeIdentificationService node_id_svc = (NodeIdentificationService) sb
054: .getService(this , NodeIdentificationService.class, null);
055: nodeID = node_id_svc.getMessageAddress();
056:
057: // register our servlet
058: try {
059: servletService.register(getPath(), this );
060: } catch (Exception e) {
061: throw new RuntimeException(
062: "Unable to register servlet at path <" + getPath()
063: + ">: " + e.getMessage());
064: }
065: }
066:
067: protected abstract String getPath();
068:
069: protected abstract String getTitle();
070:
071: protected abstract void printPage(HttpServletRequest request,
072: PrintWriter out);
073:
074: public MessageAddress getNodeID() {
075: return nodeID;
076: }
077:
078: public void doGet(HttpServletRequest request,
079: HttpServletResponse response) throws java.io.IOException {
080: String refresh = request.getParameter("refresh");
081: int refreshSeconds = ((refresh != null) ? Integer
082: .parseInt(refresh) : 0);
083:
084: response.setContentType("text/html");
085: PrintWriter out = response.getWriter();
086:
087: out.print("<html><HEAD>");
088: if (refreshSeconds > 0) {
089: out.print("<META HTTP-EQUIV=\"refresh\" content=\"");
090: out.print(refreshSeconds);
091: out.print("\">");
092: }
093: out.print("<TITLE>");
094: out.print(getTitle());
095: out.print("</TITLE></HEAD><body><H1>");
096: out.print(getTitle());
097: out.print("</H1>");
098:
099: out.print("Date: ");
100: out.print(new java.util.Date() + "\n");
101:
102: printPage(request, out);
103: out.print("<p><p><br>RefreshSeconds: ");
104: out.print(refreshSeconds);
105:
106: out.print("</body></html>\n");
107:
108: out.close();
109: }
110: }
|