1 20 package org.openi.soap.client; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 25 import javax.xml.soap.MessageFactory ; 26 import javax.xml.soap.MimeHeaders ; 27 import javax.xml.soap.SOAPConnection ; 28 import javax.xml.soap.SOAPException ; 29 import javax.xml.soap.SOAPMessage ; 30 31 import java.util.Iterator ; 32 import java.util.StringTokenizer ; 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 ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.ByteArrayInputStream ; 41 42 47 public class HttpSOAPConnection extends SOAPConnection { 48 49 public void close() throws SOAPException { 50 52 } 53 54 public SOAPMessage call(SOAPMessage message, Object endPoint) 55 throws SOAPException { 56 57 URL url = null; 58 59 if (endPoint instanceof String ) 60 try { 61 url = new URL ((String )endPoint); 62 } catch (MalformedURLException ex) { 63 ex.printStackTrace(); 64 throw new SOAPException ("Mailformed url", ex); 65 } 66 else if (endPoint instanceof URL ) 67 url = (URL ) endPoint; 68 else 69 throw new SOAPException ("Bad URL type"); 70 71 72 PostMethod post = new PostMethod(url.toString()); 73 HttpClient client = new HttpClient(); 74 75 MimeHeaders headers = message.getMimeHeaders(); 76 Iterator it = headers.getAllHeaders(); 77 78 boolean hasAuth = false; boolean isFailure = false; 80 81 while (it.hasNext()) { 82 MimeHeader header = (MimeHeader ) it.next(); 83 84 String [] values = headers.getHeader(header.getName()); 85 86 if (values.length == 1) 87 post.setRequestHeader(header.getName(), header.getValue()); 88 else { 89 StringBuffer concat = new StringBuffer (); 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 outstream = new ByteArrayOutputStream (); 112 message.writeTo(outstream); 113 ByteArrayInputStream instream = new ByteArrayInputStream (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 ("Bad response: " + post.getStatusLine().toString()); 124 } 125 126 headers = new MimeHeaders (); 127 String key, value; 128 129 Header [] 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 values = new StringTokenizer (value, ","); 140 while (values.hasMoreTokens()) 141 headers.addHeader(key, values.nextToken().trim()); 142 } 143 } 144 return MessageFactory.newInstance().createMessage(headers, post.getResponseBodyAsStream()); 146 147 } catch (SOAPException ex) { 148 ex.printStackTrace(); 149 throw ex; 150 } catch (Exception ex) { 151 ex.printStackTrace(); 152 throw new SOAPException (ex); 153 } finally { 154 post.releaseConnection(); 155 } 156 157 } 158 159 } 160 | Popular Tags |