001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.httpd;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.Socket;
022: import java.net.URI;
023: import java.util.Properties;
024:
025: import org.apache.openejb.OpenEJBException;
026: import org.apache.openejb.loader.SystemInstance;
027: import org.apache.openejb.server.ServiceException;
028: import org.apache.openejb.util.LogCategory;
029: import org.apache.openejb.util.Logger;
030:
031: /**
032: * This is the main class for the web administration. It takes care of the
033: * processing from the browser, sockets and threading.
034: *
035: * @since 11/25/2001
036: */
037: public class OpenEJBHttpServer implements HttpServer {
038: private static final Logger log = Logger
039: .getInstance(LogCategory.HTTPSERVER,
040: "org.apache.openejb.util.resources");
041:
042: private HttpListener listener;
043:
044: public OpenEJBHttpServer() {
045: this (getHttpListenerRegistry());
046: }
047:
048: public static HttpListenerRegistry getHttpListenerRegistry() {
049: SystemInstance systemInstance = SystemInstance.get();
050: HttpListenerRegistry registry = systemInstance
051: .getComponent(HttpListenerRegistry.class);
052: if (registry == null) {
053: registry = new HttpListenerRegistry();
054: systemInstance.setComponent(HttpListenerRegistry.class,
055: registry);
056: }
057: return registry;
058: }
059:
060: public OpenEJBHttpServer(HttpListener listener) {
061: this .listener = listener;
062: }
063:
064: public HttpListener getListener() {
065: return listener;
066: }
067:
068: public void service(Socket socket) throws ServiceException,
069: IOException {
070: /**
071: * The InputStream used to receive incoming messages from the client.
072: */
073: InputStream in = socket.getInputStream();
074: /**
075: * The OutputStream used to send outgoing response messages to the client.
076: */
077: OutputStream out = socket.getOutputStream();
078:
079: try {
080: //TODO: if ssl change to https
081: URI socketURI = new URI("http://"
082: + socket.getLocalAddress().getHostName() + ":"
083: + socket.getLocalPort());
084: processRequest(socketURI, in, out);
085: } catch (Throwable e) {
086: log.error("Unexpected error", e);
087: } finally {
088: try {
089: if (out != null) {
090: out.flush();
091: out.close();
092: }
093: if (in != null)
094: in.close();
095: if (socket != null)
096: socket.close();
097: } catch (Throwable t) {
098: log
099: .error("Encountered problem while closing connection with client: "
100: + t.getMessage());
101: }
102: }
103: }
104:
105: public void service(InputStream in, OutputStream out)
106: throws ServiceException, IOException {
107: throw new UnsupportedOperationException(
108: "Method not implemented: service(InputStream in, OutputStream out)");
109: }
110:
111: public void init(Properties props) throws Exception {
112: }
113:
114: public void start() throws ServiceException {
115: }
116:
117: public void stop() throws ServiceException {
118: }
119:
120: public String getName() {
121: return "httpd";
122: }
123:
124: public int getPort() {
125: return 0;
126: }
127:
128: public String getIP() {
129: return "";
130: }
131:
132: /**
133: * takes care of processing requests and creating the webadmin ejb's
134: *
135: * @param in the input stream from the browser
136: * @param out the output stream to the browser
137: */
138: private void processRequest(URI socketURI, InputStream in,
139: OutputStream out) {
140: HttpResponseImpl response = null;
141: try {
142: response = process(socketURI, in);
143:
144: } catch (Throwable t) {
145: response = HttpResponseImpl.createError(t.getMessage(), t);
146: } finally {
147: try {
148: response.writeMessage(out);
149: } catch (Throwable t2) {
150: log.error("Could not write response", t2);
151: }
152: }
153:
154: }
155:
156: private HttpResponseImpl process(URI socketURI, InputStream in)
157: throws OpenEJBException {
158: HttpRequestImpl req = new HttpRequestImpl(socketURI);
159: HttpResponseImpl res = new HttpResponseImpl();
160:
161: try {
162: req.readMessage(in);
163: res.setRequest(req);
164: } catch (Throwable t) {
165: res.setCode(400);
166: res.setResponseString("Could not read the request");
167: res.getPrintWriter().println(t.getMessage());
168: t.printStackTrace(res.getPrintWriter());
169: log.error("BAD REQUEST", t);
170: throw new OpenEJBException("Could not read the request.\n"
171: + t.getClass().getName() + ":\n" + t.getMessage(),
172: t);
173: }
174:
175: URI uri;
176: String location = null;
177: try {
178: uri = req.getURI();
179: location = uri.getPath();
180: int querry = location.indexOf("?");
181: if (querry != -1) {
182: location = location.substring(0, querry);
183: }
184: } catch (Throwable t) {
185: throw new OpenEJBException(
186: "Could not determine the module " + location + "\n"
187: + t.getClass().getName() + ":\n"
188: + t.getMessage());
189: }
190:
191: try {
192: listener.onMessage(req, res);
193: } catch (Throwable t) {
194: throw new OpenEJBException(
195: "Error occurred while executing the module "
196: + location + "\n" + t.getClass().getName()
197: + ":\n" + t.getMessage(), t);
198: }
199:
200: return res;
201: }
202: }
|