SSLSocketFactoryImpl.java revision 6812a2e8bb43d9a875633a9ba255d9882c63e327
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 java.io.IOException;
217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.InetAddress;
227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.Socket;
237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.net.UnknownHostException;
247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport java.security.KeyManagementException;
257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstromimport javax.net.ssl.SSLSocketFactory;
267329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom/**
287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom * Implementation of SSLSocketFactory.
297329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom */
307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrompublic class SSLSocketFactoryImpl extends SSLSocketFactory {
317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
326812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom    private SSLParametersImpl sslParameters;
337329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    private IOException instantiationException;
347329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * Constructor.
377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public SSLSocketFactoryImpl() {
397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        super();
407329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        try {
416812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom            sslParameters = SSLParametersImpl.getDefault();
427329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        } catch (KeyManagementException e) {
437329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            instantiationException =
447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                new IOException("Delayed instantiation exception:");
457329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            instantiationException.initCause(e);
467329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
487329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
497329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * Constructor.
517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
526812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom    protected SSLSocketFactoryImpl(SSLParametersImpl sslParameters) {
537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        super();
547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        this.sslParameters = sslParameters;
557329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
577329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
587329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
617329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public String[] getDefaultCipherSuites() {
627329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
637329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            return new String[0];
647329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
657329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return sslParameters.getEnabledCipherSuites();
667329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
677329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
687329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
697329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
707329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
717329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
727329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public String[] getSupportedCipherSuites() {
737329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
747329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            return new String[0];
757329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
767329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return CipherSuite.getSupportedCipherSuiteNames();
777329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
787329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
797329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
807329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.ssl.SSLSocketFactory#createSocket(Socket,String,int,boolean)
817329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
827329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
837329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(Socket s, String host, int port,
847329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            boolean autoClose) throws IOException {
857329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
867329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
877329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
886812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom        return new SSLSocketWrapper(s, autoClose, (SSLParametersImpl) sslParameters
897329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom                .clone());
907329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
917329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
927329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    // -------------- Methods inherided from SocketFactory --------------
937329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
947329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
957329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket()
967329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
977329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
987329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket() throws IOException {
997329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1007329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1017329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1026812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom        return new SSLSocketImpl((SSLParametersImpl) sslParameters.clone());
1037329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1047329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1057329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1067329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(String,int)
1077329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1087329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1097329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(String host, int port)
1107329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throws IOException, UnknownHostException {
1117329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1127329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1137329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1147329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port,
1156812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom                (SSLParametersImpl) sslParameters.clone());
1167329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1177329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1187329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1197329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(String,int,InetAddress,int)
1207329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1217329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1227329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(String host, int port,
1237329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            InetAddress localHost, int localPort) throws IOException,
1247329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            UnknownHostException {
1257329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1267329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1277329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1287329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port, localHost, localPort,
1296812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom                (SSLParametersImpl) sslParameters.clone());
1307329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1317329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1327329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1337329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(InetAddress,int)
1347329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1357329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1367329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(InetAddress host, int port)
1377329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throws IOException {
1387329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1397329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1407329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1417329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(host, port,
1426812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom                (SSLParametersImpl) sslParameters.clone());
1437329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1447329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1457329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    /**
1467329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     * @see javax.net.SocketFactory#createSocket(InetAddress,int,InetAddress,int)
1477329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom     */
1487329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    @Override
1497329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    public Socket createSocket(InetAddress address, int port,
1507329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            InetAddress localAddress, int localPort) throws IOException {
1517329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        if (instantiationException != null) {
1527329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom            throw instantiationException;
1537329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        }
1547329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom        return new SSLSocketImpl(address, port, localAddress, localPort,
1556812a2e8bb43d9a875633a9ba255d9882c63e327Brian Carlstrom                (SSLParametersImpl) sslParameters.clone());
1567329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    }
1577329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
1587329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom    // ------------------------------------------------------------------
1597329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom}
1607329fa972d9c20777444e5e1b13169d700de6567Brian Carlstrom
161