KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echowebserver > EchoWSCommandHandler


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

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 /**
23  * EchoWSCommandHandler
24  * @author Akshathkumar Shetty
25  */

26 public class EchoWSCommandHandler implements ClientCommandHandler {
27     private static Logger logger =
28             Logger.getLogger(EchoWSCommandHandler.class.getName());
29     
30     private static String JavaDoc header = null;
31     private static String JavaDoc trailer = null;
32
33     public static void init(QuickServer quickserevr) {
34         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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); //clear
49
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         //handler.setDataMode(DataMode.BYTE, DataType.IN); //doing from xml
59
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 JavaDoc 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     //helper methods
85
private void sendOutput(ClientHandler handler, EchoWSData data) throws IOException {
86         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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