001/*
002 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.16/src/java/org/apache/commons/ssl/SSLServerSocketWrapper.java $
003 * $Revision: 121 $
004 * $Date: 2007-11-13 21:26:57 -0800 (Tue, 13 Nov 2007) $
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.SSLServerSocket;
035import javax.net.ssl.SSLSocket;
036import java.io.IOException;
037import java.net.InetAddress;
038import java.net.Socket;
039import java.net.SocketAddress;
040import java.net.SocketException;
041import java.nio.channels.ServerSocketChannel;
042
043/**
044 * Wraps an SSLServerSocket - NOTE that the accept() method applies a number of
045 * important common-ssl settings before returning the SSLSocket!
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 20-Nov-2006
051 */
052public class SSLServerSocketWrapper extends SSLServerSocket {
053    protected SSLServerSocket s;
054    protected SSL ssl;
055    protected SSLWrapperFactory wf;
056
057    public SSLServerSocketWrapper(SSLServerSocket s, SSL ssl,
058                                  SSLWrapperFactory wf)
059        throws IOException {
060        super();
061        this.s = s;
062        this.ssl = ssl;
063        this.wf = wf;
064    }
065
066    /* javax.net.ssl.SSLServerSocket */
067
068    public Socket accept() throws IOException {
069        SSLSocket secureSocket = (SSLSocket) s.accept();
070
071        // Do the commons-ssl usual housekeeping for every socket:
072        ssl.doPreConnectSocketStuff(secureSocket);
073        InetAddress addr = secureSocket.getInetAddress();
074        String hostName = addr.getHostName();
075        ssl.doPostConnectSocketStuff(secureSocket, hostName);
076
077        return wf.wrap(secureSocket);
078    }
079
080    public String[] getEnabledCipherSuites() {
081        return s.getEnabledCipherSuites();
082    }
083
084    public String[] getEnabledProtocols() { return s.getEnabledProtocols(); }
085
086    public boolean getEnableSessionCreation() {
087        return s.getEnableSessionCreation();
088    }
089
090    public boolean getNeedClientAuth() { return s.getNeedClientAuth(); }
091
092    public String[] getSupportedCipherSuites() {
093        return s.getSupportedCipherSuites();
094    }
095
096    public String[] getSupportedProtocols() { return s.getSupportedProtocols(); }
097
098    public boolean getUseClientMode() { return s.getUseClientMode(); }
099
100    public boolean getWantClientAuth() { return s.getWantClientAuth(); }
101
102    public void setEnabledCipherSuites(String[] suites) {
103        s.setEnabledCipherSuites(suites);
104    }
105
106    public void setEnabledProtocols(String[] protocols) {
107        s.setEnabledProtocols(protocols);
108    }
109
110    public void setEnableSessionCreation(boolean flag) {
111        s.setEnableSessionCreation(flag);
112    }
113
114    public void setNeedClientAuth(boolean need) {
115        s.setNeedClientAuth(need);
116    }
117
118    public void setUseClientMode(boolean use) { s.setUseClientMode(use); }
119
120    public void setWantClientAuth(boolean want) {
121        s.setWantClientAuth(want);
122    }
123
124    /* java.net.Socket */
125
126    public void bind(SocketAddress endpoint) throws IOException {
127        s.bind(endpoint);
128    }
129
130    public void bind(SocketAddress ep, int bl) throws IOException {
131        s.bind(ep, bl);
132    }
133
134    public void close() throws IOException { s.close(); }
135
136    public ServerSocketChannel getChannel() { return s.getChannel(); }
137
138    public InetAddress getInetAddress() { return s.getInetAddress(); }
139
140    public int getLocalPort() { return s.getLocalPort(); }
141
142    public SocketAddress getLocalSocketAddress() {
143        return s.getLocalSocketAddress();
144    }
145
146    public int getReceiveBufferSize() throws SocketException {
147        return s.getReceiveBufferSize();
148    }
149
150    public boolean getReuseAddress() throws SocketException {
151        return s.getReuseAddress();
152    }
153
154    public int getSoTimeout() throws IOException { return s.getSoTimeout(); }
155
156    public boolean isBound() { return s.isBound(); }
157
158    public boolean isClosed() { return s.isClosed(); }
159
160    public void setReceiveBufferSize(int size) throws SocketException {
161        s.setReceiveBufferSize(size);
162    }
163
164    public void setReuseAddress(boolean on) throws SocketException {
165        s.setReuseAddress(on);
166    }
167
168    public void setSoTimeout(int timeout) throws SocketException {
169        s.setSoTimeout(timeout);
170    }
171
172    public String toString() { return s.toString(); }
173
174    /*  Java 1.5
175     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
176     {
177         s.setPerformancePreferences( connectionTime, latency, bandwidth );
178     }
179     */
180
181
182}