001/*
002 * Copyright 2014 Groupon, Inc
003 * Copyright 2014 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.ning.billing.recurly.util.http;
019
020
021import java.security.KeyManagementException;
022import java.security.NoSuchAlgorithmException;
023import javax.net.ssl.SSLContext;
024
025public class SslUtils {
026
027    private static final String TLS_PROTOCOL_KEY = "killbill.payment.recurly.tlsProtocol";
028    private static final String TLS_PROTOCOL_DEFAULT = "TLSv1.2";
029    private SSLContext context;
030
031    private static class SingletonHolder {
032        public static final SslUtils instance = new SslUtils();
033    }
034
035    public static SslUtils getInstance() {
036        return SingletonHolder.instance;
037    }
038
039    public SSLContext getSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
040        if (context != null) return this.context;
041
042        final String protocol = System.getProperty(TLS_PROTOCOL_KEY, TLS_PROTOCOL_DEFAULT);
043        context = SSLContext.getInstance(protocol);
044        context.init(null, null, null);
045
046        return context;
047    }
048}