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.body.http.util;
00032 
00033 import java.io.BufferedInputStream;
00034 import java.io.BufferedOutputStream;
00035 import java.io.DataInputStream;
00036 import java.io.IOException;
00037 import java.net.ConnectException;
00038 import java.net.HttpURLConnection;
00039 import java.net.URL;
00040 import java.net.UnknownHostException;
00041 
00042 import org.objectweb.proactive.core.body.http.util.exceptions.HTTPRemoteException;
00043 
00044 
00051 public class HttpMessageSender {
00052     public static final String SERVICE_REQUEST_URI = "/ProActiveHTTP";
00053     public static final String SERVICE_REQUEST_CONTENT_TYPE = "application/java";
00054     private String url;
00055 
00056     
00057 
00062     public HttpMessageSender(String url) {
00063         this.url = url;
00064         
00065     }
00066 
00071     public Object sendMessage(HttpMessage message) throws HTTPRemoteException {
00072         byte[] bytes = HttpMarshaller.marshallObject(message);
00073 
00074         String url_;
00075 
00076         
00077         
00078         
00079         
00080         
00081         
00082         
00083         try {
00084             String nodename = null;
00085             if (!url.startsWith("http:")) {
00086                 url = "http:" + url;
00087             }
00088             int lastslash = url.lastIndexOf('/');
00089             if (lastslash > 6) {
00090                 nodename = url.substring(lastslash);
00091                 url = url.substring(0, lastslash);
00092             }
00093             int lastIndex = url.lastIndexOf(":");
00094 
00095             
00096             
00097             
00098             
00099             
00100             
00101             
00102             
00103             
00104             URL u = new URL(url + SERVICE_REQUEST_URI);
00105 
00106             
00107             HttpURLConnection connection = (HttpURLConnection) u.openConnection();
00108             connection.setDoOutput(true);
00109             connection.setDoInput(true);
00110             connection.setRequestMethod("GET");
00111             connection.setRequestProperty("Content-Length", "" + bytes.length);
00112             connection.setRequestProperty("Content-Type",
00113                 SERVICE_REQUEST_CONTENT_TYPE);
00114             connection.setUseCaches(false);
00115             connection.connect();
00116 
00117             
00118             BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
00119 
00120             out.write(bytes);
00121             out.flush();
00122             out.close();
00123 
00124             
00125             DataInputStream in = null;
00126 
00127             in = new DataInputStream(new BufferedInputStream(
00128                         connection.getInputStream()));
00129 
00130             int a = connection.getContentLength();
00131 
00132             byte[] b = new byte[a];
00133 
00134             in.readFully(b);
00135             Object returnedObject = HttpMarshaller.unmarshallObject(b);
00136             return returnedObject;
00137 
00138             
00139             
00140         } catch (ConnectException e) {
00141             throw new HTTPRemoteException(
00142                 "Error while connecting the remote host: " + url, e);
00143         } catch (UnknownHostException e) {
00144             throw new HTTPRemoteException("Unknown remote host: " + url, e);
00145         } catch (IOException e) {
00146             throw new HTTPRemoteException(
00147                 "Error during connection with remote host" + url, e);
00148         }
00149     }
00150 }