2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client;
19 import java.security.KeyManagementException;
20 import java.security.NoSuchAlgorithmException;
21 import java.security.cert.CertificateException;
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.TrustManager;
25 import javax.net.ssl.X509TrustManager;
27 import org.apache.http.client.config.RequestConfig;
28 import org.apache.http.client.methods.HttpDelete;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.client.methods.HttpPatch;
31 import org.apache.http.client.methods.HttpPost;
32 import org.apache.http.client.methods.HttpRequestBase;
33 import org.apache.http.config.Registry;
34 import org.apache.http.config.RegistryBuilder;
35 import org.apache.http.conn.socket.ConnectionSocketFactory;
36 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
37 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
38 import org.apache.http.impl.client.HttpClientBuilder;
39 import org.apache.http.impl.client.HttpClients;
40 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
44 import org.springframework.context.annotation.Bean;
45 import org.springframework.stereotype.Service;
46 import org.springframework.web.bind.annotation.RequestMethod;
48 @EnableAutoConfiguration
50 public class HttpClientUtils {
51 private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
54 public static HttpClientBuilder createHttpClientBuilder() {
55 // org.apache.commons.httpclient.protocol.Protocol.unregisterProtocol("https");
56 SSLContext sslcontext = null;
58 sslcontext = createIgnoreVerifySSL();
59 } catch (KeyManagementException | NoSuchAlgorithmException e) {
60 logger.error("HttpRequestProcessor -> generateHttpClient exception. ", e);
63 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
64 .register("http", PlainConnectionSocketFactory.INSTANCE)
65 .register("https", new SSLConnectionSocketFactory(sslcontext))
68 PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
69 connManager.setMaxTotal(200);
70 connManager.setDefaultMaxPerRoute(50);
72 HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connManager);
74 return httpClientBuilder;
77 private static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
78 SSLContext sc = SSLContext.getInstance("TLSv1.2");
80 X509TrustManager trustManager = new X509TrustManager() {
82 public void checkClientTrusted(
83 java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
84 String paramString) throws CertificateException {
88 public void checkServerTrusted(
89 java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
90 String paramString) throws CertificateException {
94 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
99 sc.init(null, new TrustManager[] { trustManager }, null);
103 public static HttpRequestBase getHttpRequest(RequestMethod requestMethod) {
104 HttpRequestBase base = null;
105 switch(requestMethod) {
107 base = new HttpGet();
110 base = new HttpDelete();
113 base = new HttpPatch();
116 base = new HttpPost();
119 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).setConnectionRequestTimeout(20000).build();
120 base.setConfig(requestConfig);