    001/*
    002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.15/src/java/org/apache/commons/ssl/SSLSocketWrapper.java $
    003 * $Revision: 155 $
    004 * $Date: 2009-09-17 14:00:58 -0700 (Thu, 17 Sep 2009) $
    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 javax.net.ssl.HandshakeCompletedListener;
    035import javax.net.ssl.SSLSession;
    036import javax.net.ssl.SSLSocket;
    037import java.io.IOException;
    038import java.io.InputStream;
    039import java.io.OutputStream;
    040import java.net.InetAddress;
    041import java.net.Socket;
    042import java.net.SocketAddress;
    043import java.net.SocketException;
    044import java.nio.channels.SocketChannel;
    045
    046/**
    047 * @author Credit Union Central of British Columbia
    048 * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
    049 * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
    050 * @since 16-Aug-2006
    051 */
    052public class SSLSocketWrapper extends SSLSocket {
    053    protected Socket s;
    054
    055    public SSLSocketWrapper(Socket s) {
    056        this.s = s;
    057    }
    058
    059    /* javax.net.ssl.SSLSocket */
    060
    061    public void addHandshakeCompletedListener(HandshakeCompletedListener hcl) {
    062        if (s instanceof SSLSocket) {
    063            ((SSLSocket) s).addHandshakeCompletedListener(hcl);
    064        }
    065    }
    066
    067    public void removeHandshakeCompletedListener(HandshakeCompletedListener hcl) {
    068        if (s instanceof SSLSocket) {
    069            ((SSLSocket) s).removeHandshakeCompletedListener(hcl);
    070        }
    071    }
    072
    073    public String[] getSupportedCipherSuites() {
    074        if (s instanceof SSLSocket) {
    075            return ((SSLSocket) s).getSupportedCipherSuites();
    076        } else {
    077            return null;
    078        }
    079    }
    080
    081    public boolean getEnableSessionCreation() {
    082        if (s instanceof SSLSocket) {
    083            return ((SSLSocket) s).getEnableSessionCreation();
    084        } else {
    085            return false;
    086        }
    087    }
    088
    089    public String[] getEnabledCipherSuites() {
    090        if (s instanceof SSLSocket) {
    091            return ((SSLSocket) s).getEnabledCipherSuites();
    092        } else {
    093            return null;
    094        }
    095    }
    096
    097    public String[] getSupportedProtocols() {
    098        if (s instanceof SSLSocket) {
    099            return ((SSLSocket) s).getSupportedProtocols();
    100        } else {
    101            return null;
    102        }
    103    }
    104
    105    public String[] getEnabledProtocols() {
    106        if (s instanceof SSLSocket) {
    107            return ((SSLSocket) s).getEnabledProtocols();
    108        } else {
    109            return null;
    110        }
    111    }
    112
    113    public SSLSession getSession() {
    114        if (s instanceof SSLSocket) {
    115            return ((SSLSocket) s).getSession();
    116        } else {
    117            return null;
    118        }
    119    }
    120
    121    public boolean getUseClientMode() {
    122        if (s instanceof SSLSocket) {
    123            return ((SSLSocket) s).getUseClientMode();
    124        } else {
    125            return false;
    126        }
    127    }
    128
    129    public boolean getNeedClientAuth() {
    130        if (s instanceof SSLSocket) {
    131            return ((SSLSocket) s).getNeedClientAuth();
    132        } else {
    133            return false;
    134        }
    135    }
    136
    137    public boolean getWantClientAuth() {
    138        if (s instanceof SSLSocket) {
    139            return ((SSLSocket) s).getWantClientAuth();
    140        } else {
    141            return false;
    142        }
    143    }
    144
    145    public void setEnabledCipherSuites(String[] cs) {
    146        if (s instanceof SSLSocket) {
    147            ((SSLSocket) s).setEnabledCipherSuites(cs);
    148        }
    149    }
    150
    151    public void setEnabledProtocols(String[] ep) {
    152        if (s instanceof SSLSocket) {
    153            ((SSLSocket) s).setEnabledProtocols(ep);
    154        }
    155    }
    156
    157    public void startHandshake() throws IOException {
    158        if (s instanceof SSLSocket) {
    159            ((SSLSocket) s).startHandshake();
    160        }
    161    }
    162
    163    public void setUseClientMode(boolean b) {
    164        if (s instanceof SSLSocket) {
    165            ((SSLSocket) s).setUseClientMode(b);
    166        }
    167    }
    168
    169    public void setNeedClientAuth(boolean b) {
    170        if (s instanceof SSLSocket) {
    171            ((SSLSocket) s).setNeedClientAuth(b);
    172        }
    173    }
    174
    175    public void setWantClientAuth(boolean b) {
    176        if (s instanceof SSLSocket) {
    177            ((SSLSocket) s).setWantClientAuth(b);
    178        }
    179    }
    180
    181    public void setEnableSessionCreation(boolean b) {
    182        if (s instanceof SSLSocket) {
    183            ((SSLSocket) s).setEnableSessionCreation(b);
    184        }
    185    }
    186
    187    /* java.net.Socket */
    188
    189    public SocketChannel getChannel() {
    190        return s.getChannel();
    191    }
    192
    193    public InetAddress getInetAddress() {
    194        return s.getInetAddress();
    195    }
    196
    197    public boolean getKeepAlive() throws SocketException {
    198        return s.getKeepAlive();
    199    }
    200
    201    public InetAddress getLocalAddress() {
    202        return s.getLocalAddress();
    203    }
    204
    205    public int getLocalPort() {
    206        return s.getLocalPort();
    207    }
    208
    209    public SocketAddress getLocalSocketAddress() {
    210        return s.getLocalSocketAddress();
    211    }
    212
    213    public boolean getOOBInline() throws SocketException {
    214        return s.getOOBInline();
    215    }
    216
    217    public int getPort() {
    218        return s.getPort();
    219    }
    220
    221    public int getReceiveBufferSize() throws SocketException {
    222        return s.getReceiveBufferSize();
    223    }
    224
    225    public SocketAddress getRemoteSocketAddress() {
    226        return s.getRemoteSocketAddress();
    227    }
    228
    229    public boolean getReuseAddress() throws SocketException {
    230        return s.getReuseAddress();
    231    }
    232
    233    public int getSendBufferSize() throws SocketException {
    234        return s.getSendBufferSize();
    235    }
    236
    237    public int getSoLinger() throws SocketException {
    238        return s.getSoLinger();
    239    }
    240
    241    public int getSoTimeout() throws SocketException {
    242        return s.getSoTimeout();
    243    }
    244
    245    public boolean getTcpNoDelay() throws SocketException {
    246        return s.getTcpNoDelay();
    247    }
    248
    249    public int getTrafficClass() throws SocketException {
    250        return s.getTrafficClass();
    251    }
    252
    253    public boolean isBound() {
    254        return s.isBound();
    255    }
    256
    257    public boolean isClosed() {
    258        return s.isClosed();
    259    }
    260
    261    public boolean isConnected() {
    262        return s.isConnected();
    263    }
    264
    265    public boolean isInputShutdown() {
    266        return s.isInputShutdown();
    267    }
    268
    269    public boolean isOutputShutdown() {
    270        return s.isOutputShutdown();
    271    }
    272
    273    public void sendUrgentData(int data) throws IOException {
    274        s.sendUrgentData(data);
    275    }
    276
    277    public void setKeepAlive(boolean on) throws SocketException {
    278        s.setKeepAlive(on);
    279    }
    280
    281    public void setOOBInline(boolean on) throws SocketException {
    282        s.setOOBInline(on);
    283    }
    284
    285    public void setReceiveBufferSize(int size) throws SocketException {
    286        s.setReceiveBufferSize(size);
    287    }
    288
    289    public void setReuseAddress(boolean on) throws SocketException {
    290        s.setReuseAddress(on);
    291    }
    292
    293    public void setSendBufferSize(int size) throws SocketException {
    294        s.setSendBufferSize(size);
    295    }
    296
    297    public void setSoLinger(boolean on, int l) throws SocketException {
    298        s.setSoLinger(on, l);
    299    }
    300
    301    public void setSoTimeout(int timeout) throws SocketException {
    302        s.setSoTimeout(timeout);
    303    }
    304
    305    public void setTcpNoDelay(boolean on) throws SocketException {
    306        s.setTcpNoDelay(on);
    307    }
    308
    309    public void setTrafficClass(int tc) throws SocketException {
    310        s.setTrafficClass(tc);
    311    }
    312
    313    public void shutdownInput() throws IOException {
    314        s.shutdownInput();
    315    }
    316
    317    public void shutdownOutput() throws IOException {
    318        s.shutdownOutput();
    319    }
    320
    321    public String toString() {
    322        return s.toString();
    323    }
    324
    325    /*  Java 1.5
    326     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
    327     {
    328         s.setPerformancePreferences( connectionTime, latency, bandwidth );
    329     }
    330     */
    331
    332    public void bind(SocketAddress bindpoint) throws IOException {
    333        s.bind(bindpoint);
    334    }
    335
    336    public void close() throws IOException {
    337        s.close();
    338    }
    339
    340    public void connect(SocketAddress endpoint) throws IOException {
    341        s.connect(endpoint);
    342    }
    343
    344    public void connect(SocketAddress endpoint, int timeout) throws IOException {
    345        s.connect(endpoint, timeout);
    346    }
    347
    348    public InputStream getInputStream() throws IOException {
    349        return s.getInputStream();
    350    }
    351
    352    public OutputStream getOutputStream() throws IOException {
    353        return s.getOutputStream();
    354    }
    355
    356}