/* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egovernment.moa.id.commons.utils; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSocketFactory; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.MiscUtil; /** * @author tlenz * */ public class HttpClientWithProxySupport { public static CloseableHttpClient getHttpClient(SSLSocketFactory sSLSocketFactory, boolean validateHostname) { HttpClientBuilder clientBuilder = HttpClients.custom(); //set proxy functionality String host = System.getProperty("http.proxyHost"); //$NON-NLS-1$ String port = System.getProperty("http.proxyPort"); //$NON-NLS-1$ int p = -1; if (MiscUtil.isNotEmpty(host) && MiscUtil.isNotEmpty(port)) { p = Integer.parseInt(port); HttpHost proxy = null; if (host.startsWith("https")) proxy = new HttpHost(host, p, "https"); else proxy = new HttpHost(host, p, "http"); clientBuilder.setProxy(proxy); Logger.info("Initial HTTPClient with proxy usage. " + "ProxyHost=" + host + " ProxyPort=" + port); String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$ String pass = System.getProperty("http.proxyPassword"); //$NON-NLS-1$ if (MiscUtil.isNotEmpty(user) && pass != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, p), new UsernamePasswordCredentials(user, pass)); } } //set SSL context if (sSLSocketFactory != null) { HostnameVerifier hostnameVerifier = null; //set hostName validation filter if (validateHostname) hostnameVerifier = new DefaultHostnameVerifier(); else hostnameVerifier = new NoopHostnameVerifier(); clientBuilder.setSSLSocketFactory( new SSLConnectionSocketFactory(sSLSocketFactory, hostnameVerifier)); } return clientBuilder.build(); } }