org/objectweb/proactive/core/body/http/util/HttpMessageSender.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.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     //    private int port
00057 
00062     public HttpMessageSender(String url) {
00063         this.url = url;
00064         //        this.port =  port;       
00065     }
00066 
00071     public Object sendMessage(HttpMessage message) throws HTTPRemoteException {
00072         byte[] bytes = HttpMarshaller.marshallObject(message);
00073 
00074         String url_;
00075 
00076         //        try {
00077         //            url_ = UrlBuilder.checkUrl(url);
00078         //            System.out.println("************* " + url_);
00079         //        } catch (UnknownHostException e1) {
00080         //            // TODO Auto-generated catch block
00081         //            e1.printStackTrace();
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             //            if (port == 0) {
00096             //                 System.out.println(url.length() + " -- " + url);
00097             //                 port = Integer.parseInt(url.substring(lastIndex + 1,
00098             //                                lastIndex + 5));
00099             //                }
00100             //
00101             //            if (lastIndex == 4) {
00102             //                  url = url + ":" + port;
00103             //        }
00104             URL u = new URL(url + SERVICE_REQUEST_URI);
00105 
00106             //connection to the specified url
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             //write data in the stream
00118             BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
00119 
00120             out.write(bytes);
00121             out.flush();
00122             out.close();
00123 
00124             //Get data returned in the connection
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             //            if (returnedObject instanceof Exception)
00139             //                throw (Exception)returnedObject;
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 }

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