SSLSocketFactoryImpl.java revision 7329fa972d9c20777444e5e1b13169d700de6567
17329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom/*
27329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  Licensed to the Apache Software Foundation (ASF) under one or more
37329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  contributor license agreements.  See the NOTICE file distributed with
47329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  this work for additional information regarding copyright ownership.
57329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  The ASF licenses this file to You under the Apache License, Version 2.0
67329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  (the "License"); you may not use this file except in compliance with
77329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  the License.  You may obtain a copy of the License at
87329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *
97329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *     http://www.apache.org/licenses/LICENSE-2.0
107329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *
117329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  Unless required by applicable law or agreed to in writing, software
127329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  distributed under the License is distributed on an "AS IS" BASIS,
137329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
147329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  See the License for the specific language governing permissions and
157329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom *  limitations under the License.
167329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */
177329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
187329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrompackage org.apache.harmony.xnet.provider.jsse;
197329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
207329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport org.apache.harmony.xnet.provider.jsse.SSLSocketWrapper;
217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.io.IOException;
237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.InetAddress;
247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.Socket;
257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.UnknownHostException;
267329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.security.KeyManagementException;
277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport javax.net.ssl.SSLSocketFactory;
297329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom/**
317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Implementation of SSLSocketFactory.
327329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */
337329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrompublic class SSLSocketFactoryImpl extends SSLSocketFactory {
347329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    private SSLParameters sslParameters;
367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    private IOException instantiationException;
377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * Constructor.
407329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
417329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public SSLSocketFactoryImpl() {
427329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        super();
437329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        try {
447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            sslParameters = SSLParameters.getDefault();
457329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        } catch (KeyManagementException e) {
467329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            instantiationException =
477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                new IOException("Delayed instantiation exception:");
487329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            instantiationException.initCause(e);
497329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
527329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * Constructor.
547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
557329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    protected SSLSocketFactoryImpl(SSLParameters sslParameters) {
567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        super();
577329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        this.sslParameters = sslParameters;
587329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
617329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
627329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
637329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
647329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public String[] getDefaultCipherSuites() {
657329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
667329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            return new String[0];
677329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
687329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return sslParameters.getEnabledCipherSuites();
697329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
707329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
717329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
727329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
737329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
747329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
757329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public String[] getSupportedCipherSuites() {
767329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
777329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            return new String[0];
787329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
797329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return CipherSuite.getSupportedCipherSuiteNames();
807329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
817329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
827329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
837329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#createSocket(Socket,String,int,boolean)
847329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
857329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
867329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(Socket s, String host, int port,
877329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            boolean autoClose) throws IOException {
887329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
897329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
907329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
917329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketWrapper(s, autoClose, (SSLParameters) sslParameters
927329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                .clone());
937329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
947329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
957329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    // -------------- Methods inherided from SocketFactory --------------
967329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
977329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
987329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket()
997329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1007329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1017329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket() throws IOException {
1027329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1037329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1047329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1057329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl((SSLParameters) sslParameters.clone());
1067329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1077329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1087329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1097329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(String,int)
1107329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1117329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1127329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(String host, int port)
1137329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throws IOException, UnknownHostException {
1147329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1157329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1167329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1177329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port,
1187329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                (SSLParameters) sslParameters.clone());
1197329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1207329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(String,int,InetAddress,int)
1237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(String host, int port,
1267329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            InetAddress localHost, int localPort) throws IOException,
1277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            UnknownHostException {
1287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1297329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port, localHost, localPort,
1327329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                (SSLParameters) sslParameters.clone());
1337329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1347329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(InetAddress,int)
1377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(InetAddress host, int port)
1407329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throws IOException {
1417329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1427329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1437329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port,
1457329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                (SSLParameters) sslParameters.clone());
1467329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1487329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1497329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(InetAddress,int,InetAddress,int)
1507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1527329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(InetAddress address, int port,
1537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            InetAddress localAddress, int localPort) throws IOException {
1547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1557329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1577329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(address, port, localAddress, localPort,
1587329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                (SSLParameters) sslParameters.clone());
1597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1617329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    // ------------------------------------------------------------------
1627329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom}
1637329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
164