1/* Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany 2 * Copyright (c) 2006, James Seigel, Calgary, AB., Canada 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 * sell copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 * IN THE SOFTWARE. */ 21 22package org.ksoap2.transport; 23 24import java.io.*; 25import java.net.*; 26import java.util.Iterator; 27import java.util.LinkedList; 28import java.util.List; 29import java.util.Map; 30import java.util.Set; 31 32import org.ksoap2.HeaderProperty; 33 34/** 35 * Connection for J2SE environments. 36 */ 37public class ServiceConnectionSE implements ServiceConnection { 38 39 private HttpURLConnection connection; 40 41 /** 42 * Constructor taking the url to the endpoint for this soap communication 43 * @param url the url to open the connection to. 44 * @throws IOException 45 */ 46 public ServiceConnectionSE(String url) throws IOException { 47 this(null, url, ServiceConnection.DEFAULT_TIMEOUT); 48 } 49 50 public ServiceConnectionSE(Proxy proxy, String url) throws IOException { 51 this(proxy, url, ServiceConnection.DEFAULT_TIMEOUT); 52 } 53 54 /** 55 * Constructor taking the url to the endpoint for this soap communication 56 * @param url the url to open the connection to. 57 * @param timeout the connection and read timeout for the http connection in milliseconds 58 * @throws IOException // 20 seconds 59 */ 60 public ServiceConnectionSE(String url, int timeout) throws IOException { 61 this(null, url, timeout); 62 } 63 64 public ServiceConnectionSE(Proxy proxy, String url, int timeout) throws IOException { 65 connection = (proxy == null) 66 ? (HttpURLConnection) new URL(url).openConnection() 67 : (HttpURLConnection) new URL(url).openConnection(proxy); 68 connection.setUseCaches(false); 69 connection.setDoOutput(true); 70 connection.setDoInput(true); 71 connection.setConnectTimeout(timeout); 72 connection.setReadTimeout(timeout); // even if we connect fine we want to time out if we cant read anything.. 73 } 74 75 public void connect() throws IOException { 76 connection.connect(); 77 } 78 79 public void disconnect() { 80 connection.disconnect(); 81 } 82 83 public List getResponseProperties() { 84 Map properties = connection.getHeaderFields(); 85 Set keys = properties.keySet(); 86 List retList = new LinkedList(); 87 88 for (Iterator i = keys.iterator(); i.hasNext();) { 89 String key = (String) i.next(); 90 List values = (List) properties.get(key); 91 92 for (int j = 0; j < values.size(); j++) { 93 retList.add(new HeaderProperty(key, (String) values.get(j))); 94 } 95 } 96 97 return retList; 98 } 99 100 public void setRequestProperty(String string, String soapAction) { 101 connection.setRequestProperty(string, soapAction); 102 } 103 104 public void setRequestMethod(String requestMethod) throws IOException { 105 connection.setRequestMethod(requestMethod); 106 } 107 108 /** 109 * If the length of a HTTP request body is known ahead, sets fixed length 110 * to enable streaming without buffering. Sets after connection will cause an exception. 111 * 112 * @param contentLength the fixed length of the HTTP request body 113 * @see http://developer.android.com/reference/java/net/HttpURLConnection.html 114 **/ 115 public void setFixedLengthStreamingMode(int contentLength) { 116 connection.setFixedLengthStreamingMode(contentLength); 117 } 118 119 public OutputStream openOutputStream() throws IOException { 120 return connection.getOutputStream(); 121 } 122 123 public InputStream openInputStream() throws IOException { 124 return connection.getInputStream(); 125 } 126 127 public InputStream getErrorStream() { 128 return connection.getErrorStream(); 129 } 130 131 public String getHost() { 132 return connection.getURL().getHost(); 133 } 134 135 public int getPort() { 136 return connection.getURL().getPort(); 137 } 138 139 public String getPath() { 140 return connection.getURL().getPath(); 141 } 142} 143