KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > httpd > HttpServer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact openejb@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: HttpServer.java,v 1.7 2005/09/06 08:09:45 dblevins Exp $
44  */

45 package org.openejb.server.httpd;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import org.openejb.OpenEJBException;
50 import org.openejb.server.ServerService;
51 import org.openejb.server.ServiceException;
52 import sun.net.www.protocol.http.HttpURLConnection;
53
54 import java.io.IOException JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.io.OutputStream JavaDoc;
57 import java.net.Socket JavaDoc;
58 import java.net.URI JavaDoc;
59 import java.util.Properties JavaDoc;
60
61 /**
62  * This is the main class for the web administration. It takes care of the
63  * processing from the browser, sockets and threading.
64  *
65  * @since 11/25/2001
66  */

67 public class HttpServer implements ServerService {
68
69     private static Log log = LogFactory.getLog(HttpServer.class);
70
71     private HttpListener listener;
72
73     public HttpServer() {
74     }
75
76     public HttpServer(HttpListener listener) {
77         this.listener = listener;
78     }
79
80     public void service(Socket JavaDoc socket) throws ServiceException, IOException JavaDoc {
81         /**
82          * The InputStream used to receive incoming messages from the client.
83          */

84         InputStream JavaDoc in = socket.getInputStream();
85         /**
86          * The OutputStream used to send outgoing response messages to the client.
87          */

88         OutputStream JavaDoc out = socket.getOutputStream();
89
90         try {
91             //TODO: if ssl change to https
92
URI JavaDoc socketURI = new URI JavaDoc("http://" + socket.getLocalAddress().getHostName() + ":" + socket.getLocalPort());
93             processRequest(socketURI, in, out);
94         } catch (Throwable JavaDoc e) {
95             log.error("Unexpected error", e);
96         } finally {
97             try {
98                 if (out != null) {
99                     out.flush();
100                     out.close();
101                 }
102                 if (in != null)
103                     in.close();
104                 if (socket != null)
105                     socket.close();
106             } catch (Throwable JavaDoc t) {
107                 log.error("Encountered problem while closing connection with client: "
108                         + t.getMessage());
109             }
110         }
111     }
112
113     public void service(InputStream JavaDoc in, OutputStream JavaDoc out) throws ServiceException, IOException JavaDoc {
114         throw new UnsupportedOperationException JavaDoc("Method not implemented: service(InputStream in, OutputStream out)");
115     }
116
117
118     public void start() throws ServiceException {
119     }
120
121     public void stop() throws ServiceException {
122     }
123
124     public String JavaDoc getName() {
125         return "httpd";
126     }
127
128     public int getPort() {
129         return 0;
130     }
131
132     public String JavaDoc getIP() {
133         return "";
134     }
135
136     /**
137      * Initalizes this instance and takes care of starting things up
138      *
139      * @param props a properties instance for system properties
140      * @throws Exception if an exeption is thrown
141      */

142     public void init(Properties JavaDoc props) throws Exception JavaDoc {
143     }
144
145     /**
146      * takes care of processing requests and creating the webadmin ejb's
147      *
148      * @param in the input stream from the browser
149      * @param out the output stream to the browser
150      */

151     private void processRequest(URI JavaDoc socketURI, InputStream JavaDoc in, OutputStream JavaDoc out) {
152         HttpResponseImpl response = null;
153         try {
154             response = process(socketURI, in);
155
156         } catch (Throwable JavaDoc t) {
157             response = HttpResponseImpl.createError(t.getMessage(), t);
158         } finally {
159             try {
160                 response.writeMessage(out);
161             } catch (Throwable JavaDoc t2) {
162                 log.error("Could not write response", t2);
163             }
164         }
165
166     }
167
168     private HttpResponseImpl process(URI JavaDoc socketURI, InputStream JavaDoc in) throws OpenEJBException {
169
170         HttpRequestImpl req = new HttpRequestImpl(socketURI);
171         HttpResponseImpl res = new HttpResponseImpl();
172
173         try {
174             req.readMessage(in);
175             res.setRequest(req);
176         } catch (Throwable JavaDoc t) {
177             res.setCode(HttpURLConnection.HTTP_BAD_REQUEST);
178             res.setResponseString("Could not read the request");
179             res.getPrintWriter().println(t.getMessage());
180             t.printStackTrace(res.getPrintWriter());
181             log.error("BAD REQUEST", t);
182             throw new OpenEJBException("Could not read the request.\n" + t.getClass().getName() + ":\n" + t.getMessage(), t);
183         }
184
185         URI JavaDoc uri = null;
186         String JavaDoc location = null;
187         try {
188             uri = req.getURI();
189             location = uri.getPath();
190             int querry = location.indexOf("?");
191             if (querry != -1) {
192                 location = location.substring(0, querry);
193             }
194         } catch (Throwable JavaDoc t) {
195             throw new OpenEJBException("Could not determine the module " + location + "\n" + t.getClass().getName() + ":\n" + t.getMessage());
196         }
197
198         try {
199             listener.onMessage(req, res);
200         } catch (Throwable JavaDoc t) {
201             throw new OpenEJBException("Error occurred while executing the module " + location + "\n" + t.getClass().getName() + ":\n" + t.getMessage(), t);
202         }
203
204         return res;
205     }
206 }
207
Popular Tags