org/objectweb/proactive/core/rmi/HTTPRequestHandler.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.rmi;
00032 
00033 import java.io.BufferedInputStream;
00034 import java.io.DataOutputStream;
00035 import java.io.IOException;
00036 import java.io.InputStream;
00037 import java.io.OutputStream;
00038 import java.net.Socket;
00039 
00040 import javax.servlet.http.HttpServletResponse;
00041 
00042 import org.apache.log4j.Logger;
00043 import org.objectweb.proactive.core.body.http.util.HttpMarshaller;
00044 import org.objectweb.proactive.core.body.http.util.HttpUtils;
00045 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00046 import org.objectweb.proactive.core.util.log.Loggers;
00047 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00048 
00049 
00058 public class HTTPRequestHandler extends Thread {
00059     protected static Logger logger = ProActiveLogger.getLogger(Loggers.CLASSLOADING);
00060     private Socket socket;
00061     private String paths;
00062     private InputStream in;
00063     private OutputStream out;
00064     private RequestInfo reqInfo;
00065     private HttpServletResponse response;
00066 
00067     public HTTPRequestHandler(Socket socket, String paths)
00068         throws IOException {
00069         this(socket.getInputStream(), socket.getOutputStream());
00070         this.socket = socket;
00071         this.paths = paths;
00072     }
00073 
00074     public HTTPRequestHandler(InputStream in, OutputStream out,
00075         RequestInfo reqInfo, HttpServletResponse response) {
00076         this(in, out, reqInfo);
00077         this.response = response;
00078     }
00079 
00080     public HTTPRequestHandler(InputStream in, OutputStream out,
00081         RequestInfo reqInfo) {
00082         this(in, out);
00083         this.reqInfo = reqInfo;
00084     }
00085 
00086     public HTTPRequestHandler(InputStream in, OutputStream out) {
00087         this.in = in;
00088         this.out = out;
00089     }
00090 
00091     //    public void test () {
00092     //        
00093     //        /* -------------------------------------------------------------------------- */
00097     //      int b;
00098     //      try {
00099     //          
00100     //          b = in.read();
00101     //          
00102     //          System.out.println("B = " + b);
00103     //          int count = 0;
00104     //          while (b != -1 ) {
00105     //              System.out.print((char)b);
00106     //              b = (byte)in.read();
00107     //              count++;
00108     //          }
00109     //          System.out.println("==> count = " + count);
00110     //      } catch (IOException e1) {
00111     //          // TODO Auto-generated catch block
00112     //          e1.printStackTrace();
00113     //      }
00114     //    }
00115     public void run() {
00116         HTTPInputStream httpIn = null;
00117         DataOutputStream dOut = null;
00118         String responseHeaders = "";
00119         String statusLine = null;
00120         String contentType = null;
00121         byte[] bytes = null;
00122 
00123         try {
00124             dOut = new java.io.DataOutputStream(this.out);
00125 
00126             // Get the headers information in order to determine what is the requested service
00127             httpIn = new HTTPInputStream(new BufferedInputStream(this.in));
00128 
00129             try {
00130 
00131                 /* ----------------- Here we get the request headers  */
00132                 if (this.reqInfo == null) {
00133                     this.reqInfo = new RequestInfo();
00134                     reqInfo.read(httpIn);
00135                 }
00136 
00137                 if (!reqInfo.hasInfos()) {
00138                     return;
00139                 }
00140 
00141                 /* fix the jini class loader problem */
00142                 if (this.reqInfo.getPreferredList()) {
00143                     statusLine = "HTTP/1.1 200 OK";
00144                     bytes = "PreferredResources-Version: 1.0\nPreferred: false".getBytes();
00145                 }
00146                 // If  there is no field ClassFileName then it is a call to the
00147                 // ProActive Request via HTTP
00148                 else if (this.reqInfo.getClassFileName() == null) {
00149                     HTTPProcess process = new HTTPProcess(httpIn, this.reqInfo);
00150                     Object o = process.getBytes();
00151                     bytes = HttpMarshaller.marshallObject(o);
00152 
00154                     statusLine = "HTTP/1.1 200 OK";
00155                     contentType = HttpUtils.SERVICE_REQUEST_CONTENT_TYPE;
00156                 } else {
00157                     // ClassServer request
00158                     FileProcess fp = new FileProcess(paths, this.reqInfo);
00159                     bytes = fp.getBytes();
00160                     statusLine = "HTTP/1.1 200 OK";
00161                     contentType = "application/java";
00162                 }
00163             } catch (ClassNotFoundException e) {
00164                 logger.info("ClassServer failed to load class " +
00165                     this.reqInfo.getClassFileName());
00166                 statusLine = "HTTP/1.1 404 " + e.getMessage();
00167                 contentType = "text/plain";
00168                 bytes = new byte[0];
00169             } catch (IOException e) { // Including HTTPRemoteException & co
00170                 statusLine = "HTTP/1.1 500 " + e.getMessage();
00171                 contentType = "text/plain";
00172                 bytes = new byte[0];
00173             }
00174 
00175             if (!ProActiveConfiguration.osgiServletEnabled()) {
00176                 dOut.writeBytes(statusLine + "\r\n");
00177                 dOut.writeBytes("Content-Length: " + bytes.length + "\r\n");
00178                 dOut.writeBytes("Content-Type: " + contentType + "\r\n");
00179                 dOut.writeBytes(responseHeaders);
00180                 dOut.writeBytes("\r\n");
00181             } else {
00182                 response.setContentLength(bytes.length);
00183                 response.setContentType(contentType);
00184             }
00185             dOut.write(bytes);
00186             dOut.flush();
00187 
00188             if (reqInfo.getClassFileName() != null) {
00189                 if (logger.isDebugEnabled()) {
00190                     logger.info("ClassServer sent class " +
00191                         reqInfo.getClassFileName() + " successfully");
00192                 }
00193             }
00194         } catch (IOException e) {
00195             // If there is an error when writing the reply,
00196             // nothing can be told to the caller...
00197             e.printStackTrace();
00198         } finally {
00199             try {
00200                 if (logger.isDebugEnabled()) {
00201                     logger.debug("Closing socket " + this.socket);
00202                 }
00203                 dOut.close();
00204                 out.close();
00205 
00206                 if (socket != null) {
00207                     socket.close();
00208                 }
00209             } catch (java.io.IOException e) {
00210                 e.printStackTrace();
00211             }
00212         }
00213     }
00214 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1