00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00092
00093
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
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
00127 httpIn = new HTTPInputStream(new BufferedInputStream(this.in));
00128
00129 try {
00130
00131
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
00142 if (this.reqInfo.getPreferredList()) {
00143 statusLine = "HTTP/1.1 200 OK";
00144 bytes = "PreferredResources-Version: 1.0\nPreferred: false".getBytes();
00145 }
00146
00147
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
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) {
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
00196
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 }