1 57 58 package org.apache.soap.transport.http; 59 60 import java.io.*; 61 import java.net.*; 62 import java.util.*; 63 import org.w3c.dom.*; 64 import org.apache.soap.util.net.*; 65 import org.apache.soap.util.xml.*; 66 import org.apache.soap.util.mime.*; 67 import org.apache.soap.*; 68 import org.apache.soap.encoding.*; 69 import org.apache.soap.encoding.soapenc.Base64; 70 import org.apache.soap.transport.*; 71 import org.apache.soap.rpc.*; 72 import javax.mail.*; 73 import javax.mail.internet.*; 74 import javax.activation.*; 75 76 85 public class SOAPHTTPConnection implements SOAPTransport { 86 private BufferedReader responseReader; 87 private Hashtable responseHeaders; 88 private SOAPContext responseSOAPContext; 89 90 private String httpProxyHost; 91 private int httpProxyPort = 80; 92 private int timeout; 93 private String userName; 94 private String password; 95 private String proxyUserName; 96 private String proxyPassword; 97 private boolean maintainSession = true; 98 private String cookieHeader; 99 private String cookieHeader2; 100 private int outputBufferSize = HTTPUtils.DEFAULT_OUTPUT_BUFFER_SIZE; 101 private Boolean tcpNoDelay = null; 102 103 108 public void setProxyHost (String host) { 109 httpProxyHost = host; 110 } 111 112 117 public void setProxyPort (int port) { 118 httpProxyPort = port; 119 } 120 121 126 public String getProxyHost () { 127 return httpProxyHost; 128 } 129 130 135 public int getProxyPort () { 136 return httpProxyPort; 137 } 138 139 142 public void setUserName (String userName) { 143 this.userName = userName; 144 } 145 146 149 public void setPassword (String password) { 150 this.password = password; 151 } 152 153 156 public void setProxyUserName (String userName) { 157 this.proxyUserName = userName; 158 } 159 160 163 public void setProxyPassword (String password) { 164 this.proxyPassword = password; 165 } 166 167 private static String encodeAuth(String userName, String password) 168 throws SOAPException 169 { 170 try 171 { 172 return Base64.encode((userName + ":" + password).getBytes("8859_1")); 173 } 174 catch (UnsupportedEncodingException e) 175 { 176 throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage(), e); 177 } 178 } 179 180 183 public void setMaintainSession (boolean maintainSession) { 184 this.maintainSession = maintainSession; 185 if (maintainSession == false) { 186 cookieHeader = null; 187 cookieHeader2 = null; 188 } 189 } 190 191 194 public boolean getMaintainSession (){ 195 return maintainSession; 196 } 197 198 204 public void setTimeout (int timeout) { 205 this.timeout = timeout; 206 } 207 208 213 public int getTimeout () { 214 return timeout; 215 } 216 217 222 public void setOutputBufferSize(int sz) { 223 outputBufferSize = sz; 224 } 225 226 231 public int getOutputBufferSize() { 232 return outputBufferSize; 233 } 234 235 240 public Boolean getTcpNoDelay() { 241 return tcpNoDelay; 242 } 243 244 248 public void setTcpNoDelay(Boolean nodelay) { 249 tcpNoDelay = nodelay; 250 } 251 252 266 public void send (URL sendTo, String action, Hashtable headers, 267 Envelope env, SOAPMappingRegistry smr, SOAPContext ctx) 268 throws SOAPException { 269 try { 270 String payload = null; 271 if (env != null) { 272 StringWriter payloadSW = new StringWriter (); 273 env.marshall (payloadSW, smr, ctx); 274 payload = payloadSW.toString (); 275 } 276 277 if (headers == null) { 278 headers = new Hashtable (); 279 } 280 if (maintainSession) { 281 if (cookieHeader2 != null) { headers.put ("Cookie2", cookieHeader2); 284 } 285 if (cookieHeader != null) { headers.put ("Cookie", cookieHeader); 287 } 288 } 289 290 headers.put (Constants.HEADER_SOAP_ACTION, 291 (action != null) ? ('\"' + action + '\"') : ""); 292 if (userName != null) { 293 headers.put (Constants.HEADER_AUTHORIZATION, 295 "Basic " + encodeAuth(userName, password)); 296 297 } 298 if (proxyUserName != null) { 299 headers.put (Constants.HEADER_PROXY_AUTHORIZATION, 301 "Basic " + encodeAuth(proxyUserName, proxyPassword)); 302 } 303 304 TransportMessage response; 305 try 306 { 307 TransportMessage msg = new TransportMessage(payload, ctx, headers); 308 msg.save(); 309 response = HTTPUtils.post (sendTo, msg, 310 timeout, httpProxyHost, httpProxyPort, 311 outputBufferSize, tcpNoDelay); 312 } catch (MessagingException me) { 313 throw new IOException ("Failed to encode mime multipart: " + me); 314 } catch (UnsupportedEncodingException uee) { 315 throw new IOException ("Failed to encode mime multipart: " + uee); 316 } 317 318 Reader envReader = response.getEnvelopeReader(); 319 if (envReader != null) 320 responseReader = new BufferedReader(envReader); 321 else 322 responseReader = null; 323 responseSOAPContext = response.getSOAPContext(); 324 responseHeaders = response.getHeaders(); 325 if (maintainSession) { 326 String hdr; 332 333 hdr = getHeaderValue (responseHeaders, "Set-Cookie2"); 334 335 if (hdr != null) { 336 cookieHeader2 = hdr; 337 int index = cookieHeader2.indexOf (';'); 338 if (index != -1) { 339 cookieHeader2 = cookieHeader2.substring (0, index); 340 } 341 } 342 343 hdr = getHeaderValue (responseHeaders, "Set-Cookie"); 344 345 if (hdr != null) { 346 cookieHeader = hdr; 347 int index = cookieHeader.indexOf (';'); 348 if (index != -1) { 349 cookieHeader = cookieHeader.substring (0, index); 350 } 351 } 352 } 353 } catch (IllegalArgumentException e) { 354 throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage(), e); 355 } catch (MessagingException e) { 356 throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage(), e); 357 } catch (IOException e) { 358 throw new SOAPException (Constants.FAULT_CODE_CLIENT, e.getMessage(), e); 359 } 360 } 361 362 369 public BufferedReader receive () { 370 return responseReader; 371 } 372 373 378 public Hashtable getHeaders () { 379 return responseHeaders; 380 } 381 382 387 public SOAPContext getResponseSOAPContext () { 388 return responseSOAPContext; 389 } 390 391 398 private static String getHeaderValue (Hashtable headers, String headerName) 399 { 400 for (Enumeration enum = headers.keys (); enum.hasMoreElements ();) { 401 String key = (String) enum.nextElement(); 402 403 if (key.equalsIgnoreCase (headerName)) { 404 return (String) headers.get(key); 405 } 406 } 407 408 return null; 409 } 410 } 411 | Popular Tags |