KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > ksoap > HttpTransport


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 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.ksoap;
26
27 import java.io.*;
28 import javax.microedition.io.*;
29
30 import org.kxml.*;
31 import org.kxml.io.*;
32 import org.kxml.parser.*;
33 import org.kxml.wap.*;
34
35 public class HttpTransport {
36
37   String JavaDoc url;
38   String JavaDoc soapAction = "\"\"";
39
40   SoapEnvelope requestEnvelope = new SoapEnvelope();
41   SoapEnvelope responseEnvelope = new SoapEnvelope();
42
43   HttpConnection connection;
44   OutputStream os;
45   InputStream is;
46   InputStreamReader reader;
47
48   private boolean connected = false;
49   public String JavaDoc requestDump;
50   public String JavaDoc responseDump;
51   
52   public HttpTransport(String JavaDoc url, String JavaDoc soapAction) {
53     this.url = url;
54     this.soapAction = soapAction;
55   }
56
57   public Object JavaDoc call(SoapObject obj) throws IOException, InterruptedException JavaDoc {
58     requestEnvelope.setBody(obj);
59     call();
60     if (responseEnvelope.getBody() instanceof SoapFault)
61       throw((SoapFault)responseEnvelope.getBody());
62     return responseEnvelope.getBody();
63   }
64
65   public void call() throws IOException {
66     if (KSoapTracing.dbg)
67       KSoapTracing.log(KSoapTracing.DEBUG,"\n>> HttpTransport.call()");
68
69     ByteArrayOutputStream bos = new ByteArrayOutputStream ();
70     XmlWriter xw = new XmlWriter(new OutputStreamWriter (bos));
71     requestEnvelope.write(xw);
72     xw.flush();
73     bos.write('\r');
74     bos.write('\n');
75     byte [] requestData = bos.toString().getBytes();
76     bos = null;
77     xw = null;
78
79     if (KSoapTracing.dbg) {
80       requestDump = new String JavaDoc(requestData);
81       KSoapTracing.log(KSoapTracing.DEBUG,"Request : " + requestDump);
82     }
83
84     try {
85       connected = true;
86       connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE,true);
87       connection.setRequestProperty("Content-Type","text/xml");
88       connection.setRequestProperty("Content-Length",""+requestData.length);
89       connection.setRequestProperty("User-Agent","kSOAP/1.0");
90       connection.setRequestProperty("Cookie","");
91
92       connection.setRequestMethod(HttpConnection.POST);
93
94       os = connection.openOutputStream();
95       os.write(requestData,0,requestData.length);
96
97       for (int i= 0; i<5; i++) {
98         try {
99           os.close ();
100           break;
101         } catch (Throwable JavaDoc e) {
102           //System.out.println("EXCEPTION CLOSE ++++++ " + e);
103
}
104       }
105
106       requestData = null;
107       XmlParser xp = null;
108
109       is = connection.openInputStream();
110
111       while (true) {
112         try {
113           if (KSoapTracing.dbg) {
114             bos = new ByteArrayOutputStream();
115             byte [] buf = new byte [256];
116             
117             while (true) {
118               int rd = is.read(buf, 0, 256);
119               if (rd == -1) break;
120               bos.write(buf,0,rd);
121             }
122             buf = bos.toString().getBytes();
123             responseDump = new String JavaDoc(buf);
124             KSoapTracing.log(KSoapTracing.DEBUG,
125                              "HttpTransport.call() Reply : " + responseDump);
126             is.close();
127             is = new ByteArrayInputStream(buf);
128           }
129           
130           reader = new InputStreamReader(is);
131           xp = new XmlParser(reader);
132           responseEnvelope.read(xp);
133
134           if (KSoapTracing.dbg)
135             KSoapTracing.log(KSoapTracing.DEBUG,
136                              "HttpTransport.call() responce = " + responseEnvelope.getBody());
137           break;
138         } catch (Throwable JavaDoc e) {
139           if (e instanceof InterruptedIOException) {
140             //System.out.println("+++++++ EXCEPTION");
141
} else {
142             KSoapTracing.log(KSoapTracing.ERROR,e.toString());
143             if (e instanceof IOException)
144               throw (IOException)e;
145           }
146         }
147       }
148
149       if (KSoapTracing.dbg)
150         KSoapTracing.log(KSoapTracing.DEBUG,
151                          "<< HttpTransport.call()");
152     } finally {
153       if (KSoapTracing.dbg)
154         KSoapTracing.log(KSoapTracing.DEBUG,
155                          "<< HttpTransport.call()" + connected +"\n\n");
156       if (!connected) {
157         KSoapTracing.log(KSoapTracing.ERROR,"HttpTransport.call() EXCEPTION");
158         throw new InterruptedIOException();
159       }
160       reset();
161     }
162   }
163
164   public void reset() {
165     connected = false;
166     if (reader != null) {
167       try {
168         reader.close();
169       } catch (Throwable JavaDoc e) {}
170       reader = null;
171     }
172     if (is != null) {
173       try {
174         is.close();
175       } catch (Throwable JavaDoc e) {}
176       is = null;
177     }
178     if (connection != null) {
179       try {
180         connection.close();
181       } catch (Throwable JavaDoc e) {}
182       connection = null;
183     }
184   }
185   
186 }
187
Popular Tags