KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > soap > client > HttpSOAPConnection


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.soap.client;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import javax.xml.soap.MessageFactory JavaDoc;
26 import javax.xml.soap.MimeHeaders JavaDoc;
27 import javax.xml.soap.SOAPConnection JavaDoc;
28 import javax.xml.soap.SOAPException JavaDoc;
29 import javax.xml.soap.SOAPMessage JavaDoc;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import org.apache.commons.httpclient.*;
34 import org.apache.commons.httpclient.methods.PostMethod;
35 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
36
37 import sun.misc.BASE64Encoder;
38 import javax.xml.soap.MimeHeader JavaDoc;
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41
42 /**
43 * @author Dipendra Pokhrel
44 *
45 * SOAPConnection implementation based on jakarta's HttpClient
46 */

47 public class HttpSOAPConnection extends SOAPConnection JavaDoc {
48
49     public void close() throws SOAPException JavaDoc {
50         // TODO Auto-generated method stub
51

52     }
53
54     public SOAPMessage JavaDoc call(SOAPMessage JavaDoc message, Object JavaDoc endPoint)
55             throws SOAPException JavaDoc {
56         
57         URL JavaDoc url = null;
58         
59         if (endPoint instanceof String JavaDoc)
60             try {
61                 url = new URL JavaDoc((String JavaDoc)endPoint);
62             } catch (MalformedURLException JavaDoc ex) {
63                 ex.printStackTrace();
64                 throw new SOAPException JavaDoc("Mailformed url", ex);
65             }
66         else if (endPoint instanceof URL JavaDoc)
67             url = (URL JavaDoc) endPoint;
68         else
69             throw new SOAPException JavaDoc("Bad URL type");
70         
71         
72         PostMethod post = new PostMethod(url.toString());
73         HttpClient client = new HttpClient();
74
75         MimeHeaders JavaDoc headers = message.getMimeHeaders();
76         Iterator JavaDoc it = headers.getAllHeaders();
77         
78         boolean hasAuth = false; // true if we find explicit Auth header
79
boolean isFailure = false;
80         
81         while (it.hasNext()) {
82             MimeHeader JavaDoc header = (MimeHeader JavaDoc) it.next();
83
84             String JavaDoc[] values = headers.getHeader(header.getName());
85
86             if (values.length == 1)
87                 post.setRequestHeader(header.getName(), header.getValue());
88             else {
89                 StringBuffer JavaDoc concat = new StringBuffer JavaDoc();
90                 int i = 0;
91                 while (i < values.length) {
92                     if (i != 0)
93                         concat.append(',');
94                     concat.append(values[i]);
95                     i++;
96                 }
97
98                 post.setRequestHeader(header.getName(), concat.toString());
99             }
100
101             if ("Authorization".equals(header.getName()))
102                 hasAuth = true;
103         }
104
105         if (!hasAuth && url.getUserInfo() != null) {
106             post.setRequestHeader("Authorization", "Basic "
107                     + new BASE64Encoder().encode(url.getUserInfo().getBytes()));
108         }
109         
110         try {
111             ByteArrayOutputStream JavaDoc outstream = new ByteArrayOutputStream JavaDoc();
112             message.writeTo(outstream);
113             ByteArrayInputStream JavaDoc instream = new ByteArrayInputStream JavaDoc(outstream
114                     .toByteArray());
115
116             post.setRequestEntity(new InputStreamRequestEntity(instream,
117                     outstream.size()));
118
119             client.executeMethod(post);
120             if (post.getStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
121                 isFailure = true;
122             } else if((post.getStatusCode()/100) != 2) {
123                 throw new SOAPException JavaDoc("Bad response: " + post.getStatusLine().toString());
124             }
125                     
126             headers = new MimeHeaders JavaDoc();
127             String JavaDoc key, value;
128
129             Header JavaDoc[] responseHeaders = post.getResponseHeaders();
130             for (int i = 0; i < responseHeaders.length; i++) {
131
132                 key = responseHeaders[i].getName();
133                 value = responseHeaders[i].getValue();
134
135                 if (key == null && value == null)
136                     break;
137
138                 if (key != null) {
139                     StringTokenizer JavaDoc values = new StringTokenizer JavaDoc(value, ",");
140                     while (values.hasMoreTokens())
141                         headers.addHeader(key, values.nextToken().trim());
142                 }
143             }
144             //ByteArrayInputStream in = new ByteArrayInputStream(body.getBytes());
145
return MessageFactory.newInstance().createMessage(headers, post.getResponseBodyAsStream());
146
147         } catch (SOAPException JavaDoc ex) {
148             ex.printStackTrace();
149             throw ex;
150         } catch (Exception JavaDoc ex) {
151             ex.printStackTrace();
152             throw new SOAPException JavaDoc(ex);
153         } finally {
154             post.releaseConnection();
155         }
156
157     }
158
159 }
160
Popular Tags