/*******************************************************************************
 * Copyright (C) 2016 Push Technology Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

package com.pushtechnology.adapters.rest.polling;

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.client.HttpAsyncClients;

import com.pushtechnology.adapters.rest.model.latest.Model;

/**
 * Factory for {@link HttpComponent}.
 * @author Push Technology Limited
 */
public final class HttpComponentFactory {
    /**
     * @return a new {@link HttpComponent}
     */
    public HttpComponent create(Model model) {
        final HttpAsyncClientBuilder builder = HttpAsyncClients
            .custom()
            .disableCookieManagement()
            .disableAuthCaching();

        final CloseableHttpAsyncClient client = builder.build();
        client.start();
        return new HttpComponentImpl(client);
    }
}

@@ -802,16 +802,600 @@
 lling;%0A%0A
+import static java.security.KeyStore.getDefaultType;%0Aimport static javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm;%0Aimport static javax.net.ssl.TrustManagerFactory.getInstance;%0A%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport java.security.KeyManagementException;%0Aimport java.security.KeyStore;%0Aimport java.security.KeyStoreException;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.security.cert.CertificateException;%0A%0Aimport javax.net.ssl.SSLContext;%0Aimport javax.net.ssl.TrustManagerFactory;%0A%0A
 import o
@@ -1870,22 +1870,16 @@
         
-final 
 HttpAsyn
@@ -2029,133 +2029,1655 @@
     
-final CloseableHttpAsyncClient client = builder.build();%0A        client.start();%0A        return new HttpComponentImpl(client)
+if (model.getTruststore() != null) %7B%0A            builder = builder.setSSLContext(createTruststore(model));%0A        %7D%0A%0A        final CloseableHttpAsyncClient client = builder.build();%0A        client.start();%0A        return new HttpComponentImpl(client);%0A    %7D%0A%0A    private static SSLContext createTruststore(Model model) %7B%0A        final String truststoreLocation = model.getTruststore();%0A        try (InputStream stream = resolveTruststore(truststoreLocation)) %7B%0A            final KeyStore keyStore = KeyStore.getInstance(getDefaultType());%0A            keyStore.load(stream, null);%0A            final TrustManagerFactory trustManagerFactory = getInstance(getDefaultAlgorithm());%0A            trustManagerFactory.init(keyStore);%0A            final SSLContext sslContext = SSLContext.getInstance(%22SSL%22);%0A            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);%0A            return sslContext;%0A        %7D%0A        catch (KeyStoreException %7C%0A            CertificateException %7C%0A            NoSuchAlgorithmException %7C%0A            IOException %7C%0A            KeyManagementException e) %7B%0A%0A            throw new IllegalArgumentException(%22An SSLContext could not be created as requested in the%22 +%0A                %22 configuration%22);%0A        %7D%0A    %7D%0A%0A    private static InputStream resolveTruststore(String truststoreLocation) throws IOException %7B%0A        final InputStream stream = Thread%0A            .currentThread()%0A            .getContextClassLoader()%0A            .getResourceAsStream(truststoreLocation);%0A%0A        if (stream == null) %7B%0A            return Files.newInputStream(Paths.get(truststoreLocation));%0A        %7D%0A        return stream
 ;%0A  

Create SSLContext for HTTP clients.