1 14 15 package echowebserver; 16 17 import java.net.*; 18 import java.io.*; 19 import org.quickserver.net.server.*; 20 import java.util.logging.*; 21 22 26 public class EchoWSCommandHandler implements ClientCommandHandler { 27 private static Logger logger = 28 Logger.getLogger(EchoWSCommandHandler.class.getName()); 29 30 private static String header = null; 31 private static String trailer = null; 32 33 public static void init(QuickServer quickserevr) { 34 StringBuffer sb = new StringBuffer (); 35 sb.append("<!DOCTYPE HTML PUBLIC "); 36 sb.append("\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"); 37 sb.append("<HTML>\n"); 38 sb.append("<HEAD>\n"); 39 sb.append(" <TITLE>" +quickserevr.getName()); 40 sb.append(" Results</TITLE>\n"); 41 sb.append("</HEAD>\n" +"\n"); 42 sb.append("<BODY>\n"); 43 sb.append("<H1 ALIGN=\"CENTER\">"+quickserevr.getName()); 44 sb.append(" Results</H1>\n"); 45 sb.append("<table width=\"100%\"><tr><td><PRE>"); 46 header = sb.toString(); 47 48 sb.setLength(0); sb.append("</PRE></td></tr></table>\n"); 50 sb.append("</BODY>\n"); 51 sb.append("</HTML>"); 52 trailer = sb.toString(); 53 } 54 55 56 public void gotConnected(ClientHandler handler) 57 throws SocketTimeoutException, IOException { 58 logger.fine("Connection opened : "+handler.getHostAddress()); 60 } 61 public void lostConnection(ClientHandler handler) 62 throws IOException { 63 logger.fine("Connection lost : "+handler.getHostAddress()); 64 } 65 public void closingConnection(ClientHandler handler) 66 throws IOException { 67 handler.sendSystemMsg("Connection closed: "+handler.getHostAddress(), Level.FINE); 68 } 69 70 public void handleCommand(ClientHandler handler, String command) 71 throws SocketTimeoutException, IOException { 72 EchoWSData data = (EchoWSData)handler.getClientData(); 73 74 data.addInput(command); 75 76 if(data.isRequestComplete()) { 77 logger.fine("Request Complete"); 78 sendOutput(handler, data); 79 return; 80 } 81 } 82 83 84 private void sendOutput(ClientHandler handler, EchoWSData data) throws IOException { 86 StringBuffer sb = new StringBuffer (); 87 sb.append(header); 88 sb.append(data.getDataForOutput()); 89 sb.append(trailer); 90 91 handler.sendClientMsg("HTTP/1.0 200 OK"); 92 handler.sendClientMsg("Server: " + handler.getServer().getName()); 93 handler.sendClientMsg("Content-Type: text/html"); 94 handler.sendClientMsg("Content-Length: "+sb.length()); 95 handler.sendClientMsg(""); 96 handler.sendClientMsg(sb.toString()); 97 98 handler.closeConnection(); 99 } 100 101 } 102 | Popular Tags |