KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 package com.scalagent.kjoram.ksoap;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InterruptedIOException JavaDoc;
30
31 import com.scalagent.kjoram.excepts.*;
32 import com.scalagent.kjoram.jms.*;
33 import com.scalagent.kjoram.JoramTracing;
34
35 import com.scalagent.ksoap.*;
36
37 /**
38  * A <code>HttpConnection</code> class allows to send AbstractJmsRequest
39  * and receive AbstractJmsReply
40  */

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

64   public AbstractJmsReply call(AbstractJmsRequest request,String JavaDoc name, int cnxId) throws Exception JavaDoc {
65       SoapObject sO;
66       Object JavaDoc result = null;
67
68       try {
69         httpConnect.reset();
70
71         if (JoramTracing.dbgClient)
72           JoramTracing.log(JoramTracing.DEBUG,
73                            "HttpConnection.call(" + request +
74                            "," + cnxId + ")");
75
76         // Transform JmsRequest in a SoapObject
77
sO = ConversionSoapHelper.getSoapObject(request,name,cnxId);
78
79         // Send the request and wait the reply
80
int timer=10;
81         while (true) {
82           try {
83             result = httpConnect.call(sO);
84
85             if (JoramTracing.dbgClient)
86               JoramTracing.log(JoramTracing.DEBUG,
87                                "HttpConnection.call result = " + result);
88             break;
89           } catch (InterruptedIOException JavaDoc iIOE) {
90             httpConnect.reset();
91             continue;
92           } catch (IOException JavaDoc ioE) {
93             ioE.printStackTrace();
94
95             //retry to connect to the proxy if it's not a CnxConnectRequest
96
if (!(request instanceof CnxConnectRequest)) {
97               if (JoramTracing.dbgClient)
98                 JoramTracing.log(JoramTracing.DEBUG,
99                                  "timer=" + timer);
100               timer++;
101               Thread.sleep(timer*1000);
102               //break;
103
} else
104               throw ioE;
105           }
106         }
107       }
108       // Catching an exception because of...
109
catch (Exception JavaDoc e) {
110         //e.printStackTrace();
111
// ... a broken connection:
112
if (e instanceof IOException JavaDoc)
113           throw e;
114         // ... an interrupted exchange:
115
else if (e instanceof InterruptedException JavaDoc)
116           throw e;
117       }
118
119       // Transform SoapObject in a JmsReply
120
AbstractJmsReply reply = (AbstractJmsReply) ConversionSoapHelper.getObject((SoapObject)result);
121       if (JoramTracing.dbgClient) {
122         JoramTracing.log(JoramTracing.DEBUG,
123                          "HttpConnection.call : reply=" + reply);
124         JoramTracing.log(JoramTracing.DEBUG,
125                          " cnxId=" + cnxId);
126         if (reply != null)
127           JoramTracing.log(JoramTracing.DEBUG,
128                            " correlationId=" +reply.getCorrelationId());
129         JoramTracing.log(JoramTracing.DEBUG,
130                          " request=" + request);
131       }
132
133       // Finally, returning the reply:
134
return reply;
135     }
136
137 }
138
Popular Tags