001/* 002 * $HeadURL: file:///opt/dev/not-yet-commons-ssl-SVN-repo/tags/commons-ssl-0.3.17/src/java/org/apache/commons/ssl/SSLSocketWrapper.java $ 003 * $Revision: 187 $ 004 * $Date: 2015-03-16 14:27:25 -0700 (Mon, 16 Mar 2015) $ 005 * 006 * ==================================================================== 007 * Licensed to the Apache Software Foundation (ASF) under one 008 * or more contributor license agreements. See the NOTICE file 009 * distributed with this work for additional information 010 * regarding copyright ownership. The ASF licenses this file 011 * to you under the Apache License, Version 2.0 (the 012 * "License"); you may not use this file except in compliance 013 * with the License. You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, 018 * software distributed under the License is distributed on an 019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 020 * KIND, either express or implied. See the License for the 021 * specific language governing permissions and limitations 022 * under the License. 023 * ==================================================================== 024 * 025 * This software consists of voluntary contributions made by many 026 * individuals on behalf of the Apache Software Foundation. For more 027 * information on the Apache Software Foundation, please see 028 * <http://www.apache.org/>. 029 * 030 */ 031 032package org.apache.commons.ssl; 033 034import java.io.IOException; 035import java.io.InputStream; 036import java.io.OutputStream; 037import java.net.InetAddress; 038import java.net.Socket; 039import java.net.SocketAddress; 040import java.net.SocketException; 041import java.nio.channels.SocketChannel; 042 043import javax.net.ssl.HandshakeCompletedListener; 044import javax.net.ssl.SSLSession; 045import javax.net.ssl.SSLSocket; 046 047/** 048 * @author Credit Union Central of British Columbia 049 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a> 050 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a> 051 * @since 16-Aug-2006 052 */ 053public class SSLSocketWrapper extends SSLSocket { 054 protected Socket s; 055 056 public SSLSocketWrapper(Socket s) { 057 this.s = s; 058 } 059 060 /* javax.net.ssl.SSLSocket */ 061 062 public void addHandshakeCompletedListener(HandshakeCompletedListener hcl) { 063 if (s instanceof SSLSocket) { 064 ((SSLSocket) s).addHandshakeCompletedListener(hcl); 065 } 066 } 067 068 public void removeHandshakeCompletedListener(HandshakeCompletedListener hcl) { 069 if (s instanceof SSLSocket) { 070 ((SSLSocket) s).removeHandshakeCompletedListener(hcl); 071 } 072 } 073 074 public String[] getSupportedCipherSuites() { 075 if (s instanceof SSLSocket) { 076 return ((SSLSocket) s).getSupportedCipherSuites(); 077 } else { 078 return null; 079 } 080 } 081 082 public boolean getEnableSessionCreation() { 083 return s instanceof SSLSocket && ((SSLSocket) s).getEnableSessionCreation(); 084 } 085 086 public String[] getEnabledCipherSuites() { 087 if (s instanceof SSLSocket) { 088 return ((SSLSocket) s).getEnabledCipherSuites(); 089 } else { 090 return null; 091 } 092 } 093 094 public String[] getSupportedProtocols() { 095 if (s instanceof SSLSocket) { 096 return ((SSLSocket) s).getSupportedProtocols(); 097 } else { 098 return null; 099 } 100 } 101 102 public String[] getEnabledProtocols() { 103 if (s instanceof SSLSocket) { 104 return ((SSLSocket) s).getEnabledProtocols(); 105 } else { 106 return null; 107 } 108 } 109 110 public SSLSession getSession() { 111 if (s instanceof SSLSocket) { 112 return ((SSLSocket) s).getSession(); 113 } else { 114 return null; 115 } 116 } 117 118 public boolean getUseClientMode() { 119 return s instanceof SSLSocket && ((SSLSocket) s).getUseClientMode(); 120 } 121 122 public boolean getNeedClientAuth() { 123 return s instanceof SSLSocket && ((SSLSocket) s).getNeedClientAuth(); 124 } 125 126 public boolean getWantClientAuth() { 127 return s instanceof SSLSocket && ((SSLSocket) s).getWantClientAuth(); 128 } 129 130 public void setEnabledCipherSuites(String[] cs) { 131 if (s instanceof SSLSocket) { 132 ((SSLSocket) s).setEnabledCipherSuites(cs); 133 } 134 } 135 136 public void setEnabledProtocols(String[] ep) { 137 if (s instanceof SSLSocket) { 138 ((SSLSocket) s).setEnabledProtocols(ep); 139 } 140 } 141 142 public void startHandshake() throws IOException { 143 if (s instanceof SSLSocket) { 144 ((SSLSocket) s).startHandshake(); 145 } 146 } 147 148 public void setUseClientMode(boolean b) { 149 if (s instanceof SSLSocket) { 150 ((SSLSocket) s).setUseClientMode(b); 151 } 152 } 153 154 public void setNeedClientAuth(boolean b) { 155 if (s instanceof SSLSocket) { 156 ((SSLSocket) s).setNeedClientAuth(b); 157 } 158 } 159 160 public void setWantClientAuth(boolean b) { 161 if (s instanceof SSLSocket) { 162 ((SSLSocket) s).setWantClientAuth(b); 163 } 164 } 165 166 public void setEnableSessionCreation(boolean b) { 167 if (s instanceof SSLSocket) { 168 ((SSLSocket) s).setEnableSessionCreation(b); 169 } 170 } 171 172 /* java.net.Socket */ 173 174 public SocketChannel getChannel() { 175 return s.getChannel(); 176 } 177 178 public InetAddress getInetAddress() { 179 return s.getInetAddress(); 180 } 181 182 public boolean getKeepAlive() throws SocketException { 183 return s.getKeepAlive(); 184 } 185 186 public InetAddress getLocalAddress() { 187 return s.getLocalAddress(); 188 } 189 190 public int getLocalPort() { 191 return s.getLocalPort(); 192 } 193 194 public SocketAddress getLocalSocketAddress() { 195 return s.getLocalSocketAddress(); 196 } 197 198 public boolean getOOBInline() throws SocketException { 199 return s.getOOBInline(); 200 } 201 202 public int getPort() { 203 return s.getPort(); 204 } 205 206 public int getReceiveBufferSize() throws SocketException { 207 return s.getReceiveBufferSize(); 208 } 209 210 public SocketAddress getRemoteSocketAddress() { 211 return s.getRemoteSocketAddress(); 212 } 213 214 public boolean getReuseAddress() throws SocketException { 215 return s.getReuseAddress(); 216 } 217 218 public int getSendBufferSize() throws SocketException { 219 return s.getSendBufferSize(); 220 } 221 222 public int getSoLinger() throws SocketException { 223 return s.getSoLinger(); 224 } 225 226 public int getSoTimeout() throws SocketException { 227 return s.getSoTimeout(); 228 } 229 230 public boolean getTcpNoDelay() throws SocketException { 231 return s.getTcpNoDelay(); 232 } 233 234 public int getTrafficClass() throws SocketException { 235 return s.getTrafficClass(); 236 } 237 238 public boolean isBound() { 239 return s.isBound(); 240 } 241 242 public boolean isClosed() { 243 return s.isClosed(); 244 } 245 246 public boolean isConnected() { 247 return s.isConnected(); 248 } 249 250 public boolean isInputShutdown() { 251 return s.isInputShutdown(); 252 } 253 254 public boolean isOutputShutdown() { 255 return s.isOutputShutdown(); 256 } 257 258 public void sendUrgentData(int data) throws IOException { 259 s.sendUrgentData(data); 260 } 261 262 public void setKeepAlive(boolean on) throws SocketException { 263 s.setKeepAlive(on); 264 } 265 266 public void setOOBInline(boolean on) throws SocketException { 267 s.setOOBInline(on); 268 } 269 270 public void setReceiveBufferSize(int size) throws SocketException { 271 s.setReceiveBufferSize(size); 272 } 273 274 public void setReuseAddress(boolean on) throws SocketException { 275 s.setReuseAddress(on); 276 } 277 278 public void setSendBufferSize(int size) throws SocketException { 279 s.setSendBufferSize(size); 280 } 281 282 public void setSoLinger(boolean on, int l) throws SocketException { 283 s.setSoLinger(on, l); 284 } 285 286 public void setSoTimeout(int timeout) throws SocketException { 287 s.setSoTimeout(timeout); 288 } 289 290 public void setTcpNoDelay(boolean on) throws SocketException { 291 s.setTcpNoDelay(on); 292 } 293 294 public void setTrafficClass(int tc) throws SocketException { 295 s.setTrafficClass(tc); 296 } 297 298 public void shutdownInput() throws IOException { 299 s.shutdownInput(); 300 } 301 302 public void shutdownOutput() throws IOException { 303 s.shutdownOutput(); 304 } 305 306 public String toString() { 307 return s.toString(); 308 } 309 310 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { 311 s.setPerformancePreferences( connectionTime, latency, bandwidth ); 312 } 313 314 public void bind(SocketAddress bindpoint) throws IOException { 315 s.bind(bindpoint); 316 } 317 318 public void close() throws IOException { 319 s.close(); 320 } 321 322 public void connect(SocketAddress endpoint) throws IOException { 323 s.connect(endpoint); 324 } 325 326 public void connect(SocketAddress endpoint, int timeout) throws IOException { 327 s.connect(endpoint, timeout); 328 } 329 330 public InputStream getInputStream() throws IOException { 331 return s.getInputStream(); 332 } 333 334 public OutputStream getOutputStream() throws IOException { 335 return s.getOutputStream(); 336 } 337 338 public void setHost(String host) throws IOException { 339 if (s instanceof SSLSocket) { 340 Java14.setHostForSNI((SSLSocket)s, host); 341 } 342 } 343 344}