001    /*
002     * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.11/src/java/org/apache/commons/ssl/SSLWrapperFactory.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    
032    package org.apache.commons.ssl;
033    
034    import javax.net.ssl.SSLServerSocket;
035    import javax.net.ssl.SSLSocket;
036    import java.io.IOException;
037    import java.net.Socket;
038    
039    /**
040     * @author Credit Union Central of British Columbia
041     * @author <a href="http://www.cucbc.com/">www.cucbc.com</a>
042     * @author <a href="mailto:juliusdavies@cucbc.com">juliusdavies@cucbc.com</a>
043     * @since 19-Sep-2006
044     */
045    public interface SSLWrapperFactory {
046    
047        /**
048         * Wraps an SSLSSocket.
049         *
050         * @param s SSLSocket to wrap.
051         * @return The new wrapped SSLSocket.
052         * @throws IOException if wrapping failed
053         */
054        public Socket wrap(Socket s) throws IOException;
055    
056        /**
057         * Wraps an SSLServerSocket.
058         *
059         * @param s   The SSLServerSocket to wrap.
060         * @param ssl The SSL object that created the SSLServerSocket.
061         *            This way some important commons-ssl config can be applied
062         *            to the returned socket.
063         * @return The new wrapped SSLServerSocket.
064         * @throws IOException if wrapping failed
065         */
066        public SSLServerSocket wrap(SSLServerSocket s, SSL ssl)
067            throws IOException;
068    
069    
070        /**
071         * NO_WRAP doesn't wrap the SSLSocket.  It does wrap the SSLServerSocket
072         * so that we can do the usual housekeeping after accept() that we like to
073         * do on every socket.  E.g. setSoTimeout, setEnabledProtocols,
074         * setEnabledCiphers, setUseClientMode, and the hostname verifier (which
075         * should be very rare on SSLServerSockets!).
076         */
077        public final static SSLWrapperFactory NO_WRAP = new SSLWrapperFactory() {
078            // Notice!  No wrapping!
079            public Socket wrap(Socket s) { return s; }
080    
081            // We still wrap the ServerSocket, but we don't wrap the result of the
082            // the accept() call.
083            public SSLServerSocket wrap(SSLServerSocket s, SSL ssl)
084                throws IOException {
085                // Can't wrap with Java 1.3 because SSLServerSocket's constructor has
086                // default access instead of protected access in Java 1.3.
087                boolean java13 = JavaImpl.isJava13();
088                return java13 ? s : new SSLServerSocketWrapper(s, ssl, this);
089            }
090        };
091    
092        /**
093         * DUMB_WRAP is useful to make sure that wrapping the sockets doesn't break
094         * anything.  It doesn't actually do anything interesting in its wrapped
095         * implementations.
096         */
097        public final static SSLWrapperFactory DUMB_WRAP = new SSLWrapperFactory() {
098            public Socket wrap(Socket s) { return new SSLSocketWrapper(s); }
099    
100            public SSLServerSocket wrap(SSLServerSocket s, SSL ssl)
101                throws IOException {
102                // Can't wrap with Java 1.3 because SSLServerSocket's constructor has
103                // default access instead of protected access in Java 1.3.
104                boolean java13 = JavaImpl.isJava13();
105                return java13 ? s : new SSLServerSocketWrapper(s, ssl, this);
106            }
107        };
108    
109    
110    }