1
2package org.ksoap2.transport;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7import java.net.URL;
8import java.util.Iterator;
9import java.util.LinkedList;
10import java.util.Map;
11import java.util.List;
12import java.util.Set;
13
14import javax.net.ssl.HostnameVerifier;
15import javax.net.ssl.SSLSocketFactory;
16import javax.net.ssl.HttpsURLConnection;
17//import com.android.okhttp.internal.http.HttpsURLConnectionImpl;
18import org.ksoap2.HeaderProperty;
19
20/**
21 * HttpsServiceConnectionSE is a service connection that uses a https url connection and requires explicit setting of
22 * host, port and file.
23 *
24 * The explicit setting is necessary since pure url passing and letting the Java URL class parse the string does not
25 * work properly on Android.
26 *
27 * Links for reference:
28 * @see "http://stackoverflow.com/questions/2820284/ssl-on-android-strange-issue"
29 * @see "http://stackoverflow.com/questions/2899079/custom-ssl-handling-stopped-working-on-android-2-2-froyo"
30 * @see "http://code.google.com/p/android/issues/detail?id=2690"
31 * @see "http://code.google.com/p/android/issues/detail?id=2764"
32 *
33 * @see "https://gist.github.com/908048" There can be problems with the
34 * certificate of theof the server on older android versions. You can disable
35 * SSL for the versions only e.g. with an approach like this.
36 *
37 * @author Manfred Moser <manfred@simpligility.com>
38 */
39public class HttpsServiceConnectionSE implements ServiceConnection {
40
41    private HttpsURLConnection connection;
42
43    /**
44     * Create the transport with the supplied parameters.
45     * @param host the name of the host e.g. webservices.somewhere.com
46     * @param port the http port to connect on
47     * @param file the path to the file on the webserver that represents the
48     * webservice e.g. /api/services/myservice.jsp
49     * @param timeout the timeout for the connection in milliseconds
50     * @throws IOException
51     */
52    public HttpsServiceConnectionSE(String host, int port, String file,
53            int timeout) throws IOException {
54        connection = (HttpsURLConnection) new URL(HttpsTransportSE.PROTOCOL, host, port, file)
55                .openConnection();
56        updateConnectionParameters(timeout);
57    }
58
59    private void updateConnectionParameters(int timeout) {
60        connection.setConnectTimeout(timeout);
61        connection.setReadTimeout(timeout); // even if we connect fine we want to time out if we cant read anything..
62        connection.setUseCaches(false);
63        connection.setDoOutput(true);
64        connection.setDoInput(true);
65    }
66
67    public void connect() throws IOException {
68        connection.connect();
69    }
70
71    public void disconnect() {
72        connection.disconnect();
73    }
74
75    public List getResponseProperties() {
76        Map properties = connection.getHeaderFields();
77        Set keys = properties.keySet();
78        List retList = new LinkedList();
79
80        for (Iterator i = keys.iterator(); i.hasNext();) {
81            String key = (String) i.next();
82            List values = (List) properties.get(key);
83
84            for (int j = 0; j < values.size(); j++) {
85                retList.add(new HeaderProperty(key, (String) values.get(j)));
86            }
87        }
88
89        return retList;
90    }
91
92    public void setRequestProperty(String key, String value) {
93        connection.setRequestProperty(key, value);
94    }
95
96    public void setRequestMethod(String requestMethod) throws IOException {
97        connection.setRequestMethod(requestMethod);
98    }
99
100    public void setFixedLengthStreamingMode(int contentLength) {
101        connection.setFixedLengthStreamingMode(contentLength);
102    }
103
104    public OutputStream openOutputStream() throws IOException {
105        return connection.getOutputStream();
106    }
107
108    public InputStream openInputStream() throws IOException {
109        return connection.getInputStream();
110    }
111
112    public InputStream getErrorStream() {
113        return connection.getErrorStream();
114    }
115
116    public String getHost() {
117        return connection.getURL().getHost();
118    }
119
120    public int getPort() {
121        return connection.getURL().getPort();
122    }
123
124    public String getPath() {
125        return connection.getURL().getPath();
126    }
127
128    public void setSSLSocketFactory(SSLSocketFactory sf) {
129        connection.setSSLSocketFactory(sf);
130    }
131
132    public void setHostnameVerifier(HostnameVerifier v) {
133        connection.setHostnameVerifier(v);
134    }
135
136}
137