1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi.hotspot2.soap; 18 19 import android.net.Network; 20 import android.text.TextUtils; 21 22 import org.ksoap2.HeaderProperty; 23 import org.ksoap2.transport.ServiceConnection; 24 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.io.OutputStream; 28 import java.net.URL; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.Set; 33 34 import javax.net.ssl.HttpsURLConnection; 35 import javax.net.ssl.SSLSocketFactory; 36 37 /** 38 * A wrapper class for {@link HttpsURLConnection} that requires {@link Network} to open the 39 * https connection for SOAP message. 40 */ 41 public class HttpsServiceConnection implements ServiceConnection { 42 // TODO(117906601): find an optimal value for a connection timeout 43 public static final int DEFAULT_TIMEOUT_MS = 5000; // 5 seconds 44 private HttpsURLConnection mConnection; 45 HttpsServiceConnection(Network network, URL url)46 public HttpsServiceConnection(Network network, URL url) throws IOException { 47 mConnection = (HttpsURLConnection) network.openConnection(url); 48 mConnection.setConnectTimeout(DEFAULT_TIMEOUT_MS); 49 mConnection.setReadTimeout(DEFAULT_TIMEOUT_MS); 50 } 51 52 @Override connect()53 public void connect() throws IOException { 54 mConnection.connect(); 55 } 56 57 @Override disconnect()58 public void disconnect() { 59 mConnection.disconnect(); 60 } 61 62 @Override getResponseProperties()63 public List<HeaderProperty> getResponseProperties() { 64 Map<String, List<String>> properties = mConnection.getHeaderFields(); 65 Set<String> keys = properties.keySet(); 66 List<HeaderProperty> retList = new ArrayList<>(); 67 68 keys.forEach(key -> { 69 List<String> values = properties.get(key); 70 values.forEach(value -> retList.add(new HeaderProperty(key, value))); 71 }); 72 73 return retList; 74 } 75 76 @Override getResponseCode()77 public int getResponseCode() throws IOException { 78 return mConnection.getResponseCode(); 79 } 80 81 @Override setRequestProperty(String propertyName, String value)82 public void setRequestProperty(String propertyName, String value) { 83 // Ignore any settings of "the Connection: close" as android network uses the keep alive 84 // by default. 85 if (!TextUtils.equals("Connection", propertyName) || !TextUtils.equals("close", value)) { 86 mConnection.setRequestProperty(propertyName, value); 87 } 88 } 89 90 @Override setRequestMethod(String requestMethodType)91 public void setRequestMethod(String requestMethodType) throws IOException { 92 mConnection.setRequestMethod(requestMethodType); 93 } 94 95 @Override setFixedLengthStreamingMode(int contentLength)96 public void setFixedLengthStreamingMode(int contentLength) { 97 mConnection.setFixedLengthStreamingMode(contentLength); 98 } 99 100 @Override setChunkedStreamingMode()101 public void setChunkedStreamingMode() { 102 mConnection.setChunkedStreamingMode(0); 103 } 104 105 @Override openOutputStream()106 public OutputStream openOutputStream() throws IOException { 107 return mConnection.getOutputStream(); 108 } 109 110 @Override openInputStream()111 public InputStream openInputStream() throws IOException { 112 return mConnection.getInputStream(); 113 } 114 115 @Override getErrorStream()116 public InputStream getErrorStream() { 117 return mConnection.getErrorStream(); 118 } 119 120 @Override getHost()121 public String getHost() { 122 return mConnection.getURL().getHost(); 123 } 124 125 @Override getPort()126 public int getPort() { 127 return mConnection.getURL().getPort(); 128 } 129 130 131 @Override getPath()132 public String getPath() { 133 return mConnection.getURL().getPath(); 134 } 135 136 /** 137 * Wrapper function for {@link HttpsURLConnection#setSSLSocketFactory(SSLSocketFactory)} 138 * 139 * @param sslSocketFactory SSL Socket factory 140 */ setSSLSocketFactory(SSLSocketFactory sslSocketFactory)141 public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) { 142 mConnection.setSSLSocketFactory(sslSocketFactory); 143 } 144 } 145