/okhttp-tests/src/test/java/okhttp3/DelegatingSSLSocket.java

https://gitlab.com/JoshLucid/okhttp · Java · 338 lines · 248 code · 71 blank · 19 comment · 0 complexity · 699c4fdd0de37fc796a457c071a657f5 MD5 · raw file

  1. /*
  2. * Copyright 2014 Square Inc.
  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. package okhttp3;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.net.InetAddress;
  22. import java.net.Socket;
  23. import java.net.SocketAddress;
  24. import java.net.SocketException;
  25. import java.net.SocketOption;
  26. import java.nio.channels.SocketChannel;
  27. import java.util.Set;
  28. import javax.net.ssl.HandshakeCompletedListener;
  29. import javax.net.ssl.SSLParameters;
  30. import javax.net.ssl.SSLSession;
  31. import javax.net.ssl.SSLSocket;
  32. /**
  33. * An {@link javax.net.ssl.SSLSocket} that delegates all calls.
  34. */
  35. public abstract class DelegatingSSLSocket extends SSLSocket {
  36. protected final SSLSocket delegate;
  37. public DelegatingSSLSocket(SSLSocket delegate) {
  38. this.delegate = delegate;
  39. }
  40. @Override public void shutdownInput() throws IOException {
  41. delegate.shutdownInput();
  42. }
  43. @Override public void shutdownOutput() throws IOException {
  44. delegate.shutdownOutput();
  45. }
  46. @Override public String[] getSupportedCipherSuites() {
  47. return delegate.getSupportedCipherSuites();
  48. }
  49. @Override public String[] getEnabledCipherSuites() {
  50. return delegate.getEnabledCipherSuites();
  51. }
  52. @Override public void setEnabledCipherSuites(String[] suites) {
  53. delegate.setEnabledCipherSuites(suites);
  54. }
  55. @Override public String[] getSupportedProtocols() {
  56. return delegate.getSupportedProtocols();
  57. }
  58. @Override public String[] getEnabledProtocols() {
  59. return delegate.getEnabledProtocols();
  60. }
  61. @Override public void setEnabledProtocols(String[] protocols) {
  62. delegate.setEnabledProtocols(protocols);
  63. }
  64. @Override public SSLSession getSession() {
  65. return delegate.getSession();
  66. }
  67. @Override public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
  68. delegate.addHandshakeCompletedListener(listener);
  69. }
  70. @Override public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
  71. delegate.removeHandshakeCompletedListener(listener);
  72. }
  73. @Override public void startHandshake() throws IOException {
  74. delegate.startHandshake();
  75. }
  76. @Override public void setUseClientMode(boolean mode) {
  77. delegate.setUseClientMode(mode);
  78. }
  79. @Override public boolean getUseClientMode() {
  80. return delegate.getUseClientMode();
  81. }
  82. @Override public void setNeedClientAuth(boolean need) {
  83. delegate.setNeedClientAuth(need);
  84. }
  85. @Override public void setWantClientAuth(boolean want) {
  86. delegate.setWantClientAuth(want);
  87. }
  88. @Override public boolean getNeedClientAuth() {
  89. return delegate.getNeedClientAuth();
  90. }
  91. @Override public boolean getWantClientAuth() {
  92. return delegate.getWantClientAuth();
  93. }
  94. @Override public void setEnableSessionCreation(boolean flag) {
  95. delegate.setEnableSessionCreation(flag);
  96. }
  97. @Override public boolean getEnableSessionCreation() {
  98. return delegate.getEnableSessionCreation();
  99. }
  100. @Override public SSLParameters getSSLParameters() {
  101. return delegate.getSSLParameters();
  102. }
  103. @Override public void setSSLParameters(SSLParameters p) {
  104. delegate.setSSLParameters(p);
  105. }
  106. @Override public void close() throws IOException {
  107. delegate.close();
  108. }
  109. @Override public InetAddress getInetAddress() {
  110. return delegate.getInetAddress();
  111. }
  112. @Override public InputStream getInputStream() throws IOException {
  113. return delegate.getInputStream();
  114. }
  115. @Override public boolean getKeepAlive() throws SocketException {
  116. return delegate.getKeepAlive();
  117. }
  118. @Override public InetAddress getLocalAddress() {
  119. return delegate.getLocalAddress();
  120. }
  121. @Override public int getLocalPort() {
  122. return delegate.getLocalPort();
  123. }
  124. @Override public OutputStream getOutputStream() throws IOException {
  125. return delegate.getOutputStream();
  126. }
  127. @Override public int getPort() {
  128. return delegate.getPort();
  129. }
  130. @Override public int getSoLinger() throws SocketException {
  131. return delegate.getSoLinger();
  132. }
  133. @Override public int getReceiveBufferSize() throws SocketException {
  134. return delegate.getReceiveBufferSize();
  135. }
  136. @Override public int getSendBufferSize() throws SocketException {
  137. return delegate.getSendBufferSize();
  138. }
  139. @Override public int getSoTimeout() throws SocketException {
  140. return delegate.getSoTimeout();
  141. }
  142. @Override public boolean getTcpNoDelay() throws SocketException {
  143. return delegate.getTcpNoDelay();
  144. }
  145. @Override public void setKeepAlive(boolean keepAlive) throws SocketException {
  146. delegate.setKeepAlive(keepAlive);
  147. }
  148. @Override public void setSendBufferSize(int size) throws SocketException {
  149. delegate.setSendBufferSize(size);
  150. }
  151. @Override public void setReceiveBufferSize(int size) throws SocketException {
  152. delegate.setReceiveBufferSize(size);
  153. }
  154. @Override public void setSoLinger(boolean on, int timeout) throws SocketException {
  155. delegate.setSoLinger(on, timeout);
  156. }
  157. @Override public void setSoTimeout(int timeout) throws SocketException {
  158. delegate.setSoTimeout(timeout);
  159. }
  160. @Override public void setTcpNoDelay(boolean on) throws SocketException {
  161. delegate.setTcpNoDelay(on);
  162. }
  163. @Override public String toString() {
  164. return delegate.toString();
  165. }
  166. @Override public SocketAddress getLocalSocketAddress() {
  167. return delegate.getLocalSocketAddress();
  168. }
  169. @Override public SocketAddress getRemoteSocketAddress() {
  170. return delegate.getRemoteSocketAddress();
  171. }
  172. @Override public boolean isBound() {
  173. return delegate.isBound();
  174. }
  175. @Override public boolean isConnected() {
  176. return delegate.isConnected();
  177. }
  178. @Override public boolean isClosed() {
  179. return delegate.isClosed();
  180. }
  181. @Override public void bind(SocketAddress localAddr) throws IOException {
  182. delegate.bind(localAddr);
  183. }
  184. @Override public void connect(SocketAddress remoteAddr) throws IOException {
  185. delegate.connect(remoteAddr);
  186. }
  187. @Override public void connect(SocketAddress remoteAddr, int timeout) throws IOException {
  188. delegate.connect(remoteAddr, timeout);
  189. }
  190. @Override public boolean isInputShutdown() {
  191. return delegate.isInputShutdown();
  192. }
  193. @Override public boolean isOutputShutdown() {
  194. return delegate.isOutputShutdown();
  195. }
  196. @Override public void setReuseAddress(boolean reuse) throws SocketException {
  197. delegate.setReuseAddress(reuse);
  198. }
  199. @Override public boolean getReuseAddress() throws SocketException {
  200. return delegate.getReuseAddress();
  201. }
  202. @Override public void setOOBInline(boolean oobinline) throws SocketException {
  203. delegate.setOOBInline(oobinline);
  204. }
  205. @Override public boolean getOOBInline() throws SocketException {
  206. return delegate.getOOBInline();
  207. }
  208. @Override public void setTrafficClass(int value) throws SocketException {
  209. delegate.setTrafficClass(value);
  210. }
  211. @Override public int getTrafficClass() throws SocketException {
  212. return delegate.getTrafficClass();
  213. }
  214. @Override public void sendUrgentData(int value) throws IOException {
  215. delegate.sendUrgentData(value);
  216. }
  217. @Override public SocketChannel getChannel() {
  218. return delegate.getChannel();
  219. }
  220. @Override public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
  221. delegate.setPerformancePreferences(connectionTime, latency, bandwidth);
  222. }
  223. // Java 9 methods.
  224. public SSLSession getHandshakeSession() {
  225. try {
  226. return (SSLSession) SSLSocket.class.getMethod("getHandshakeSession").invoke(delegate);
  227. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  228. throw new AssertionError();
  229. }
  230. }
  231. public String getApplicationProtocol() {
  232. try {
  233. return (String) SSLSocket.class.getMethod("getApplicationProtocol").invoke(delegate);
  234. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  235. throw new AssertionError();
  236. }
  237. }
  238. public String getHandshakeApplicationProtocol() {
  239. try {
  240. return (String) SSLSocket.class.getMethod("getHandshakeApplicationProtocol").invoke(delegate);
  241. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  242. throw new AssertionError();
  243. }
  244. }
  245. public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
  246. try {
  247. SSLSocket.class.getMethod("setOption", SocketOption.class, Object.class).invoke(delegate, name, value);
  248. return this;
  249. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  250. throw new AssertionError();
  251. }
  252. }
  253. public <T> T getOption(SocketOption<T> name) throws IOException {
  254. try {
  255. return (T) SSLSocket.class.getMethod("getOption", SocketOption.class).invoke(delegate, name);
  256. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  257. throw new AssertionError();
  258. }
  259. }
  260. public Set<SocketOption<?>> supportedOptions() {
  261. try {
  262. return (Set<SocketOption<?>>) SSLSocket.class.getMethod("supportedOptions").invoke(delegate);
  263. } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  264. throw new AssertionError();
  265. }
  266. }
  267. }