001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.activemq.transport.https;
019
020import java.io.IOException;
021import java.net.URI;
022
023import org.apache.activemq.broker.SslContext;
024import org.apache.activemq.transport.http.HttpClientTransport;
025import org.apache.activemq.transport.util.TextWireFormat;
026import org.apache.activemq.util.IOExceptionSupport;
027import org.apache.http.config.Registry;
028import org.apache.http.config.RegistryBuilder;
029import org.apache.http.conn.HttpClientConnectionManager;
030import org.apache.http.conn.socket.ConnectionSocketFactory;
031import org.apache.http.conn.ssl.DefaultHostnameVerifier;
032import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
033import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
034
035public class HttpsClientTransport extends HttpClientTransport {
036
037    public HttpsClientTransport(TextWireFormat wireFormat, URI remoteUrl) {
038        super(wireFormat, remoteUrl);
039    }
040
041    @Override
042    protected HttpClientConnectionManager createClientConnectionManager() {
043        return new PoolingHttpClientConnectionManager(createRegistry());
044    }
045
046    private Registry<ConnectionSocketFactory> createRegistry() {
047
048        RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create();
049        try {
050            SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(createSocketFactory(), new DefaultHostnameVerifier());
051            registryBuilder.register("https", sslConnectionFactory);
052            return registryBuilder.build();
053        } catch (Exception e) {
054            throw new IllegalStateException("Failure trying to create scheme registry", e);
055        }
056    }
057
058    /**
059     * Creates a new SSL SocketFactory. The given factory will use user-provided
060     * key and trust managers (if the user provided them).
061     *
062     * @return Newly created (Ssl)SocketFactory.
063     * @throws IOException
064     */
065    protected javax.net.ssl.SSLSocketFactory createSocketFactory() throws IOException {
066        if (SslContext.getCurrentSslContext() != null) {
067            SslContext ctx = SslContext.getCurrentSslContext();
068            try {
069                return ctx.getSSLContext().getSocketFactory();
070            } catch (Exception e) {
071                throw IOExceptionSupport.create(e);
072            }
073        } else {
074            return (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
075        }
076
077    }
078
079    @Override
080    protected String getSystemPropertyPrefix() {
081        return "https.";
082    }
083
084}