KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjndi > ksoap > HttpConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2002 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * The present code contributor is ScalAgent Distributed Technologies.
21  *
22  * Initial developer(s): Nicolas Tachker (ScalAgent)
23  * Contributor(s):
24  */

25 package com.scalagent.kjndi.ksoap;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InterruptedIOException JavaDoc;
29
30 import com.scalagent.ksoap.*;
31
32 /**
33  * A <code>HttpConnection</code> class allows to send AbstractJmsRequest
34  * and receive AbstractJmsReply
35  */

36 public class HttpConnection {
37
38   /** The http Connection. */
39   protected HttpTransport httpConnect;
40   /** Server's address. */
41   protected String JavaDoc serverUrl;
42   /** to print all SOAP messages. */
43   protected boolean debug = false;
44   /** to compress data. */
45   protected boolean compress = false;
46
47
48   public HttpConnection(String JavaDoc serverUrl) {
49     this.serverUrl = serverUrl;
50
51     // Create a httpConnection
52
httpConnect = new HttpTransport(serverUrl,"ProxyService");
53   }
54
55   /**
56    * sends a request to the server.
57    *
58    * @param request The request to send.
59    * @return The server reply.
60    */

61   public Object JavaDoc call(String JavaDoc action, String JavaDoc name, Object JavaDoc object) throws Exception JavaDoc {
62       SoapObject sO;
63       Object JavaDoc result = null;
64
65       try {
66         httpConnect.reset();
67
68         if (debug) {
69           System.out.println("JNDI HttpConnection.call(" + action + "," + object + ")");
70         }
71
72         // Transform object in a SoapObject
73
sO = ConversionSoapHelper.getSoapObject(action,name,object);
74
75         // Send the request and wait the reply
76
int timer=1;
77         while (true) {
78           try {
79             result = httpConnect.call(sO);
80             break;
81           } catch (InterruptedIOException JavaDoc iIOE) {
82           } catch (IOException JavaDoc ioE) {
83             ioE.printStackTrace();
84             //retry to connect to the proxy
85
System.out.println("JNDI timer=" + timer);
86             timer++;
87             Thread.sleep(timer*1000);
88             if (timer > 1)
89               break;
90             timer++;
91           }
92         }
93       }
94       // Catching an exception because of...
95
catch (Exception JavaDoc e) {
96         Exception JavaDoc jE = null;
97         // ... a broken connection:
98
if (e instanceof IOException JavaDoc)
99           jE = new IllegalStateException JavaDoc("Connection is broken.");
100         // ... an interrupted exchange:
101
else if (e instanceof InterruptedException JavaDoc)
102           jE = new InterruptedException JavaDoc("Interrupted request.");
103         throw jE;
104       }
105
106       // Transform SoapObject in a reply
107
Object JavaDoc reply = ConversionSoapHelper.getObject((SoapObject)result);
108       if (debug) {
109         System.out.println("JNDI HttpConnection.call : " +
110                            action + " reply=" + reply + ")");
111       }
112       // Finally, returning the reply:
113
return reply;
114     }
115
116 }
117
Popular Tags