1
16 package okhttp3.internal.platform;
17
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.List;
21 import javax.annotation.Nullable;
22 import javax.net.ssl.SSLParameters;
23 import javax.net.ssl.SSLSocket;
24 import javax.net.ssl.SSLSocketFactory;
25 import javax.net.ssl.X509TrustManager;
26 import okhttp3.Protocol;
27
28
29 final class Jdk9Platform extends Platform {
30 final Method setProtocolMethod;
31 final Method getProtocolMethod;
32
33 Jdk9Platform(Method setProtocolMethod, Method getProtocolMethod) {
34 this.setProtocolMethod = setProtocolMethod;
35 this.getProtocolMethod = getProtocolMethod;
36 }
37
38 @Override
39 public void configureTlsExtensions(SSLSocket sslSocket, String hostname,
40 List<Protocol> protocols) {
41 try {
42 SSLParameters sslParameters = sslSocket.getSSLParameters();
43
44 List<String> names = alpnProtocolNames(protocols);
45
46 setProtocolMethod.invoke(sslParameters,
47 new Object[] {names.toArray(new String[names.size()])});
48
49 sslSocket.setSSLParameters(sslParameters);
50 } catch (IllegalAccessException | InvocationTargetException e) {
51 throw new AssertionError("failed to set SSL parameters", e);
52 }
53 }
54
55 @Override
56 public @Nullable String getSelectedProtocol(SSLSocket socket) {
57 try {
58 String protocol = (String) getProtocolMethod.invoke(socket);
59
60
61
62 if (protocol == null || protocol.equals("")) {
63 return null;
64 }
65
66 return protocol;
67 } catch (InvocationTargetException e) {
68 if (e.getCause() instanceof UnsupportedOperationException) {
69
70
71 return null;
72 }
73
74 throw new AssertionError("failed to get ALPN selected protocol", e);
75 } catch (IllegalAccessException e) {
76 throw new AssertionError("failed to get ALPN selected protocol", e);
77 }
78 }
79
80 @Override public X509TrustManager trustManager(SSLSocketFactory sslSocketFactory) {
81
82
83
84
85 throw new UnsupportedOperationException(
86 "clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+");
87 }
88
89 public static Jdk9Platform buildIfSupported() {
90
91 try {
92 Method setProtocolMethod =
93 SSLParameters.class.getMethod("setApplicationProtocols", String[].class);
94 Method getProtocolMethod = SSLSocket.class.getMethod("getApplicationProtocol");
95
96 return new Jdk9Platform(setProtocolMethod, getProtocolMethod);
97 } catch (NoSuchMethodException ignored) {
98
99 }
100
101 return null;
102 }
103 }
104