1 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 ; 55 import java.io.InputStream ; 56 import java.io.OutputStream ; 57 import java.net.Socket ; 58 import java.net.URI ; 59 import java.util.Properties ; 60 61 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 socket) throws ServiceException, IOException { 81 84 InputStream in = socket.getInputStream(); 85 88 OutputStream out = socket.getOutputStream(); 89 90 try { 91 URI socketURI = new URI ("http://" + socket.getLocalAddress().getHostName() + ":" + socket.getLocalPort()); 93 processRequest(socketURI, in, out); 94 } catch (Throwable 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 t) { 107 log.error("Encountered problem while closing connection with client: " 108 + t.getMessage()); 109 } 110 } 111 } 112 113 public void service(InputStream in, OutputStream out) throws ServiceException, IOException { 114 throw new UnsupportedOperationException ("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 getName() { 125 return "httpd"; 126 } 127 128 public int getPort() { 129 return 0; 130 } 131 132 public String getIP() { 133 return ""; 134 } 135 136 142 public void init(Properties props) throws Exception { 143 } 144 145 151 private void processRequest(URI socketURI, InputStream in, OutputStream out) { 152 HttpResponseImpl response = null; 153 try { 154 response = process(socketURI, in); 155 156 } catch (Throwable t) { 157 response = HttpResponseImpl.createError(t.getMessage(), t); 158 } finally { 159 try { 160 response.writeMessage(out); 161 } catch (Throwable t2) { 162 log.error("Could not write response", t2); 163 } 164 } 165 166 } 167 168 private HttpResponseImpl process(URI socketURI, InputStream 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 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 uri = null; 186 String 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 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 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 |