KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > WebWorker


1 package com.quadcap.http.server22;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.ByteArrayOutputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.io.OutputStreamWriter JavaDoc;
49 import java.io.PrintStream JavaDoc;
50 import java.io.PrintWriter JavaDoc;
51
52 import java.net.InetAddress JavaDoc;
53 import java.net.Socket JavaDoc;
54 import java.net.SocketException JavaDoc;
55
56 import java.util.Enumeration JavaDoc;
57 import java.util.Hashtable JavaDoc;
58 import java.util.Vector JavaDoc;
59
60 import javax.servlet.RequestDispatcher JavaDoc;
61 import javax.servlet.Servlet JavaDoc;
62 import javax.servlet.ServletException JavaDoc;
63 import javax.servlet.SingleThreadModel JavaDoc;
64
65 import javax.servlet.http.HttpServlet JavaDoc;
66 import javax.servlet.http.HttpServletResponse JavaDoc;
67
68 import com.quadcap.io.LogInputStream;
69 import com.quadcap.io.LogOutputStream;
70 import com.quadcap.io.IO;
71
72 import com.quadcap.util.Config;
73 import com.quadcap.util.Debug;
74
75 import com.quadcap.net.server.Server;
76 import com.quadcap.net.server.Worker;
77
78 /**
79  * A Worker is a thread spawned by the server to handle a single client
80  * connection.
81  *
82  * @author Stan Bailes
83  */

84 public class WebWorker extends Worker {
85     WebServer s;
86
87     HttpInputStream is = new HttpInputStream();
88     HttpOutputStream os = new HttpOutputStream();
89     HttpRequest req = new HttpRequest(this);
90     HttpResponse res = new HttpResponse(this);
91
92     /** Construct a Worker attached to the specified socket. */
93     public void init(Server server, Object JavaDoc context) {
94         super.init(server, context);
95         this.s = (WebServer)context;
96         res.setFixedHeader("Server", s.getServerInfo());
97         req.setResponse(res);
98     }
99
100     /**
101      * Handle the client session.
102      */

103     public void doSession() {
104         checkPermission();
105         boolean close = true;
106         //Jni j1 = new Jni("WebWorker");
107
int cnt = 0;
108         do {
109             String JavaDoc uri = "";
110             String JavaDoc proto = HttpRequest.proto_10;
111             try {
112                 //j1.reset();
113
//Debug.println("[" + this + "].doSession(" + cnt + ")");
114
cnt++;
115                 is.reset(getInputStream());
116                 //j1.dump("is.reset");
117
os.reset(getOutputStream());
118                 //j1.dump("os.reset");
119
try {
120                     req.reset(is);
121                 } catch (SocketException JavaDoc e) {
122                     try {
123                         os.reallyClose();
124                     } catch (Throwable JavaDoc t) {}
125                     return;
126                 }
127                 //j1.dump("req.reset");
128
if (req.badRequest()) {
129                     try {
130                         os.reallyClose();
131                     } catch (Throwable JavaDoc t) {}
132                     return;
133                 }
134                 res.reset(os);
135                 //j1.dump("res.reset");
136
req.setResponse(res);
137                 //j1.dump("req.setResponse");
138

139                 uri = req.getRequestURI();
140                 //j1.dump("getRequestURI");
141
WebApplication app = s.getContext(uri);
142                 //j1.dump("getContext(uri)");
143
//j1.dump("getHeader(Connection)");
144
proto = req.getProtocol();
145                 //j1.dump("getProtocol");
146
if (proto == HttpRequest.proto_11) {
147                     String JavaDoc conn = req.getHeader("Connection");
148                     close = false;
149                     if (conn != null) {
150                         //Debug.println("conn = " + conn);
151
close = conn.equalsIgnoreCase("Close");
152                     }
153                 }
154                 //j1.dump("proto check");
155

156                 // XXX but res.reset will trash this!!!!
157
res.setProtocol(proto);
158                 res.setKeepAlive(!close);
159                 //j1.dump("set Connection Header");
160
if (app != null) {
161                     String JavaDoc subPath = uri.substring(app.getContextPath().length());
162                     HttpDispatcher rd = (HttpDispatcher)app.getRequestDispatcher(subPath);
163                     if (rd != null) {
164                         try {
165                             rd.service(req, res);
166                         } catch (Throwable JavaDoc th) {
167                             Debug.print(th);
168                             showException(th, req, res);
169                         } finally {
170                             if (res.getContentLength() < 0) close = true;
171                             if (Trace.level() > 0) {
172                                 Debug.println("[" + req.getMethod() + " " +
173                                               req.getRequestURI() + "] " +
174                                               res.sc + " " + res.sm);
175                             }
176                             //j1.dump("rd.service()");
177
res.flush();
178                             //j1.dump("res.flush()");
179
}
180                     } else {
181                         res.sendError(HttpServletResponse.SC_NOT_FOUND,
182                                       "Not Found (request dispatcher)");
183                     }
184                 } else {
185                     res.sendError(HttpServletResponse.SC_NOT_FOUND,
186                                   "Not Found (webapp)");
187                 }
188             } catch (SocketException JavaDoc e) {
189                 close = true;
190             } catch (Throwable JavaDoc e) {
191                 Debug.println(1, e.toString());
192                 e.printStackTrace(Debug.debugStream);
193                 close = true;
194             } finally {
195                 String JavaDoc qs = req.getQueryString();
196                 if (qs == null) {
197                     qs = "";
198                 } else {
199                     qs = "?" + qs;
200                 }
201                 s.requestLog(getRemoteHost() + " " + getRemoteAddr() + " " +
202                              req.getMethod() + " " + uri + qs + " " + res.sc);
203             }
204                 
205         } while (!close);
206
207         //j1.dump("loop done");
208
if (os != null) {
209             try {
210                 os.close();
211             } catch (Exception JavaDoc e1) {}
212         }
213         //j1.dump("os.close()");
214
}
215
216     final void showException(Throwable JavaDoc t, HttpRequest req, HttpResponse res) {
217         try {
218             res.reset();
219             res.setContentType("text/html");
220             PrintWriter JavaDoc pw = null;
221             if (res.getOutputStreamCalled) {
222                 OutputStream JavaDoc os1 = res.getOutputStream();
223                 OutputStreamWriter JavaDoc w = new OutputStreamWriter JavaDoc(os1);
224                 pw = new PrintWriter JavaDoc(w);
225             } else {
226                 pw = res.getWriter();
227             }
228             pw.println("<h3>Servlet Exception Report</h3>");
229             pw.println("<pre>");
230             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
231             PrintStream JavaDoc p = new PrintStream JavaDoc(bos);
232             t.printStackTrace(p);
233             if (t instanceof ServletException JavaDoc) {
234                 Throwable JavaDoc t1 = ((ServletException JavaDoc)t).getRootCause();
235                 if (t1 != null) {
236                     p.println("");
237                     p.println("<b>Root Cause</b>:");
238                     t1.printStackTrace(p);
239                 }
240             }
241                                   
242             p.flush();
243             pw.println(bos.toString());
244             bos = null;
245             p = null;
246             pw.println("contextPath = " + req.getContextPath());
247             pw.println("servletPath = " + req.getServletPath());
248             pw.println("pathInfo = " + req.getPathInfo());
249             pw.println("Headers:");
250             Enumeration JavaDoc e1 = req.getHeaderNames();
251             while (e1.hasMoreElements()) {
252                 String JavaDoc name = e1.nextElement().toString();
253                 Enumeration JavaDoc e2 = req.getHeaders(name);
254                 while (e2.hasMoreElements()) {
255                     String JavaDoc val = e2.nextElement().toString();
256                     pw.println(name + ": " + val);
257                 }
258             }
259             pw.println("</pre>");
260         } catch (Throwable JavaDoc th) {
261             Debug.print(th);
262         }
263     }
264
265     /**
266      * Get the output stream.
267      */

268     public HttpOutputStream getHttpOutputStream() { return os; }
269
270     /**
271      * Get the input stream.
272      */

273     public HttpInputStream getHttpInputStream() { return is; }
274
275     /**
276      * Who is my http server?
277      */

278     public WebServer getHttpServer() { return s; }
279
280     void checkPermission() {
281         if (String.valueOf(System.getProperty("public")).equals("true")) {
282             return;
283         }
284         if (String.valueOf(Config.getProperty("public")).equals("true")) {
285             return;
286         }
287         InetAddress JavaDoc ia = socket.getInetAddress();
288         if (ia.toString().equals("/127.0.0.1")) return;
289         try {
290             if (ia.equals(InetAddress.getLocalHost())) {
291                 return;
292             }
293         } catch (Throwable JavaDoc t) {}
294         throw new RuntimeException JavaDoc("No permission: " + ia);
295     }
296 }
297
Popular Tags