Java tutorial
/* * JBoss, Home of Professional Open Source * Copyright 2016 Red Hat Inc. and/or its affiliates and other * contributors as indicated by the @author tags. All rights reserved. * See the copyright.txt in the distribution for a full listing of * individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.arquillian.ce.httpclient; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; /** * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a> */ public class HttpClientBuilder { private org.apache.http.impl.client.HttpClientBuilder builder; private HttpClientBuilder() { builder = org.apache.http.impl.client.HttpClientBuilder.create(); } public static HttpRequest doPOST(String url) { return new HttpRequestImpl(new HttpPost(url)); } public static HttpRequest doGET(String url) { return new HttpRequestImpl(new HttpGet(url)); } /* * Allow httpClient to use untrusted connections * The code below was tested with httpclient 4.3.6-redhat-1 * code example from; http://literatejava.com/networks/ignore-ssl-certificate-errors-apache-httpclient-4-4/ */ public static HttpClient untrustedConnectionClient() throws Exception { return create().untrustedConnectionClientBuilder().build(); } public static HttpClientBuilder create() { return new HttpClientBuilder(); } public HttpClient build() { return new HttpClientImpl(builder.build()); } public HttpClientBuilder setCookieStore(Object cookieStore) { if (cookieStore != null && (cookieStore instanceof CookieStore)) { builder.setDefaultCookieStore((CookieStore) cookieStore); } return this; } public HttpClientBuilder untrustedConnectionClientBuilder() throws Exception { // setup a Trust Strategy that allows all certificates. // SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException { return true; } }).build(); builder.setSslcontext(sslContext); // don't check Hostnames, either. // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; // here's the special part: // -- need to create an SSL Socket Factory, to use our weakened "trust strategy"; // -- and create a Registry, to register it. // SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build(); // now, we create connection-manager using our Registry. // -- allows multi-threaded use PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry); builder.setConnectionManager(connMgr); // finally, build the HttpClient; // -- done! return this; } }