001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package echowebserver;
016:
017: import java.net.*;
018: import java.io.*;
019: import org.quickserver.net.server.*;
020: import java.util.logging.*;
021:
022: /**
023: * EchoWSCommandHandler
024: * @author Akshathkumar Shetty
025: */
026: public class EchoWSCommandHandler implements ClientCommandHandler {
027: private static Logger logger = Logger
028: .getLogger(EchoWSCommandHandler.class.getName());
029:
030: private static String header = null;
031: private static String trailer = null;
032:
033: public static void init(QuickServer quickserevr) {
034: StringBuffer sb = new StringBuffer();
035: sb.append("<!DOCTYPE HTML PUBLIC ");
036: sb.append("\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
037: sb.append("<HTML>\n");
038: sb.append("<HEAD>\n");
039: sb.append(" <TITLE>" + quickserevr.getName());
040: sb.append(" Results</TITLE>\n");
041: sb.append("</HEAD>\n" + "\n");
042: sb.append("<BODY>\n");
043: sb.append("<H1 ALIGN=\"CENTER\">" + quickserevr.getName());
044: sb.append(" Results</H1>\n");
045: sb.append("<table width=\"100%\"><tr><td><PRE>");
046: header = sb.toString();
047:
048: sb.setLength(0); //clear
049: sb.append("</PRE></td></tr></table>\n");
050: sb.append("</BODY>\n");
051: sb.append("</HTML>");
052: trailer = sb.toString();
053: }
054:
055: public void gotConnected(ClientHandler handler)
056: throws SocketTimeoutException, IOException {
057: //handler.setDataMode(DataMode.BYTE, DataType.IN); //doing from xml
058: logger.fine("Connection opened : " + handler.getHostAddress());
059: }
060:
061: public void lostConnection(ClientHandler handler)
062: throws IOException {
063: logger.fine("Connection lost : " + handler.getHostAddress());
064: }
065:
066: public void closingConnection(ClientHandler handler)
067: throws IOException {
068: handler.sendSystemMsg("Connection closed: "
069: + handler.getHostAddress(), Level.FINE);
070: }
071:
072: public void handleCommand(ClientHandler handler, String command)
073: throws SocketTimeoutException, IOException {
074: EchoWSData data = (EchoWSData) handler.getClientData();
075:
076: data.addInput(command);
077:
078: if (data.isRequestComplete()) {
079: logger.fine("Request Complete");
080: sendOutput(handler, data);
081: return;
082: }
083: }
084:
085: //helper methods
086: private void sendOutput(ClientHandler handler, EchoWSData data)
087: throws IOException {
088: StringBuffer sb = new StringBuffer();
089: sb.append(header);
090: sb.append(data.getDataForOutput());
091: sb.append(trailer);
092:
093: handler.sendClientMsg("HTTP/1.0 200 OK");
094: handler.sendClientMsg("Server: "
095: + handler.getServer().getName());
096: handler.sendClientMsg("Content-Type: text/html");
097: handler.sendClientMsg("Content-Length: " + sb.length());
098: handler.sendClientMsg("");
099: handler.sendClientMsg(sb.toString());
100:
101: handler.closeConnection();
102: }
103:
104: }
|