001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.adaptor.http;
009:
010: import java.net.Socket; //import java.net.SocketException;
011: import java.io.InputStreamReader;
012: import java.io.BufferedReader;
013: import java.io.IOException;
014: import java.io.BufferedWriter;
015: import java.io.OutputStreamWriter;
016: import java.io.PrintWriter;
017: import java.io.StringWriter;
018: import java.util.Date;
019:
020: /**
021: * response a connection of one communication
022: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
023: */
024:
025: public class HttpThread extends Thread {
026: private HttpServer server;
027: private Socket csocket;
028: private boolean running = true;
029:
030: // private PrintWriter out = null;
031: // private BufferedReader in = null;
032:
033: public HttpThread(HttpServer server, Socket csocket) {
034: this .server = server;
035: this .csocket = csocket;
036: }
037:
038: /**
039: * @ todo implements run method
040: */
041: public void run() {
042: BufferedReader in = null;
043: try {
044: in = new BufferedReader(new InputStreamReader(csocket
045: .getInputStream()));
046: } catch (IOException e) {
047: e.printStackTrace();
048: running = false;
049: }
050:
051: // String page = "starting...";
052: HttpRequest request = new HttpRequest(this );
053: int line_no = 1;
054: while (running) {
055: try {
056: String line = in.readLine();
057: if (line == null) { // line == null, break the loop
058: running = false;
059: break;
060: }
061: request.parseHeadLine(line, line_no);
062: line_no++;
063: // System.out.println("HttpThread:67 " + line);
064: if (line.trim().length() == 0) { // a blank line, loop end
065: running = false;
066: }
067:
068: Thread.sleep(50L);
069: } catch (Exception e) {
070: running = false;
071: Thread.currentThread().interrupt();
072: return;
073: }
074:
075: }
076:
077: PrintWriter out = null;
078: try {
079: out = new PrintWriter(new BufferedWriter(
080: new OutputStreamWriter(csocket.getOutputStream())));
081: String page = new HttpResponse(request).output(); // get the result page
082: out.println(page);
083: out.flush();
084: Thread.sleep(50L); // wait 500ms for flush
085: csocket.close();
086:
087: } catch (Exception e) {
088: out.println(generateHeader());
089: out.println("<h2>Error Occured</h2>");
090: out.println("<hr>");
091: out.println("<pre>");
092: // out.println(e.getMessage());
093: e.printStackTrace(out);
094: // out.println(e.getLocalizedMessage());
095: // e.printStackTrace(out);
096:
097: out.println("</pre>");
098: out.flush();
099: } finally {
100: try {
101: csocket.close();
102: } catch (IOException e) {
103: }
104: }
105:
106: }
107:
108: public HttpServer getServer() {
109: return server;
110: }
111:
112: private String generateHeader() {
113: StringWriter out = new StringWriter();
114: out.write("HTTP/1.1 200 OK" + HttpConstants.CRLF);
115: out.write("Content-Type: text/html" + HttpConstants.CRLF);
116: out.write("Date: " + new Date() + HttpConstants.CRLF);
117: out.write(HttpConstants.CRLF);
118: return out.getBuffer().toString();
119: }
120:
121: public static void main(String[] args) {
122:
123: }
124: }
|