Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » httpd » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.server.httpd;
   18   
   19   import java.io.IOException;
   20   import java.io.InputStream;
   21   import java.io.OutputStream;
   22   import java.net.Socket;
   23   import java.net.URI;
   24   import java.util.Properties;
   25   
   26   import org.apache.openejb.OpenEJBException;
   27   import org.apache.openejb.loader.SystemInstance;
   28   import org.apache.openejb.server.ServiceException;
   29   import org.apache.openejb.util.LogCategory;
   30   import org.apache.openejb.util.Logger;
   31   
   32   /**
   33    * This is the main class for the web administration.  It takes care of the
   34    * processing from the browser, sockets and threading.
   35    *
   36    * @since 11/25/2001
   37    */
   38   public class OpenEJBHttpServer implements HttpServer {
   39       private static final Logger log = Logger.getInstance(LogCategory.HTTPSERVER, "org.apache.openejb.util.resources");
   40   
   41       private HttpListener listener;
   42   
   43       public OpenEJBHttpServer() {
   44           this(getHttpListenerRegistry());
   45       }
   46   
   47       public static HttpListenerRegistry getHttpListenerRegistry() {
   48           SystemInstance systemInstance = SystemInstance.get();
   49           HttpListenerRegistry registry = systemInstance.getComponent(HttpListenerRegistry.class);
   50           if (registry == null){
   51               registry = new HttpListenerRegistry();
   52               systemInstance.setComponent(HttpListenerRegistry.class, registry);
   53           }
   54           return registry;
   55       }
   56   
   57       public OpenEJBHttpServer(HttpListener listener) {
   58           this.listener = listener;
   59       }
   60   
   61       public HttpListener getListener() {
   62           return listener;
   63       }
   64   
   65       public void service(Socket socket) throws ServiceException, IOException {
   66           /**
   67            * The InputStream used to receive incoming messages from the client.
   68            */
   69           InputStream in = socket.getInputStream();
   70           /**
   71            * The OutputStream used to send outgoing response messages to the client.
   72            */
   73           OutputStream out = socket.getOutputStream();
   74   
   75           try {
   76               //TODO: if ssl change to https
   77               URI socketURI = new URI("http://" + socket.getLocalAddress().getHostName() + ":" + socket.getLocalPort());
   78               processRequest(socketURI, in, out);
   79           } catch (Throwable e) {
   80               log.error("Unexpected error", e);
   81           } finally {
   82               try {
   83                   if (out != null) {
   84                       out.flush();
   85                       out.close();
   86                   }
   87                   if (in != null)
   88                       in.close();
   89                   if (socket != null)
   90                       socket.close();
   91               } catch (Throwable t) {
   92                   log.error("Encountered problem while closing connection with client: "
   93                           + t.getMessage());
   94               }
   95           }
   96       }
   97   
   98       public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
   99           throw new UnsupportedOperationException("Method not implemented: service(InputStream in, OutputStream out)");
  100       }
  101   
  102       public void init(Properties props) throws Exception {
  103       }
  104   
  105       public void start() throws ServiceException {
  106       }
  107   
  108       public void stop() throws ServiceException {
  109       }
  110   
  111       public String getName() {
  112           return "httpd";
  113       }
  114   
  115       public int getPort() {
  116           return 0;
  117       }
  118   
  119       public String getIP() {
  120           return "";
  121       }
  122   
  123       /**
  124        * takes care of processing requests and creating the webadmin ejb's
  125        *
  126        * @param in     the input stream from the browser
  127        * @param out    the output stream to the browser
  128        */
  129       private void processRequest(URI socketURI, InputStream in, OutputStream out) {
  130           HttpResponseImpl response = null;
  131           try {
  132               response = process(socketURI, in);
  133   
  134           } catch (Throwable t) {
  135               response = HttpResponseImpl.createError(t.getMessage(), t);
  136           } finally {
  137               try {
  138                   response.writeMessage(out);
  139               } catch (Throwable t2) {
  140                   log.error("Could not write response", t2);
  141               }
  142           }
  143   
  144       }
  145   
  146       private HttpResponseImpl process(URI socketURI, InputStream in) throws OpenEJBException {
  147           HttpRequestImpl req = new HttpRequestImpl(socketURI);
  148           HttpResponseImpl res = new HttpResponseImpl();
  149   
  150           try {
  151               req.readMessage(in);
  152               res.setRequest(req);
  153           } catch (Throwable t) {
  154               res.setCode(400);
  155               res.setResponseString("Could not read the request");
  156               res.getPrintWriter().println(t.getMessage());
  157               t.printStackTrace(res.getPrintWriter());
  158               log.error("BAD REQUEST", t);
  159               throw new OpenEJBException("Could not read the request.\n" + t.getClass().getName() + ":\n" + t.getMessage(), t);
  160           }
  161   
  162           URI uri;
  163           String location = null;
  164           try {
  165               uri = req.getURI();
  166               location = uri.getPath();
  167               int querry = location.indexOf("?");
  168               if (querry != -1) {
  169                   location = location.substring(0, querry);
  170               }
  171           } catch (Throwable t) {
  172               throw new OpenEJBException("Could not determine the module " + location + "\n" + t.getClass().getName() + ":\n" + t.getMessage());
  173           }
  174   
  175           try {
  176               listener.onMessage(req, res);
  177           } catch (Throwable t) {
  178               throw new OpenEJBException("Error occurred while executing the module " + location + "\n" + t.getClass().getName() + ":\n" + t.getMessage(), t);
  179           }
  180   
  181           return res;
  182       }
  183   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » httpd » [javadoc | source]