7f6a00069f82e962a90ee2069976d12698630d0e
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / http / client / HttpClientUtils.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client;
18
19 import java.security.cert.CertificateException;
20 import java.security.cert.X509Certificate;
21
22 import javax.net.ssl.HostnameVerifier;
23 import javax.net.ssl.SSLContext;
24
25 import org.apache.http.client.methods.HttpDelete;
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.client.methods.HttpRequestBase;
29 import org.apache.http.config.Registry;
30 import org.apache.http.config.RegistryBuilder;
31 import org.apache.http.conn.socket.ConnectionSocketFactory;
32 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
33 import org.apache.http.conn.ssl.NoopHostnameVerifier;
34 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
35 import org.apache.http.impl.client.HttpClientBuilder;
36 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
37 import org.apache.http.ssl.SSLContextBuilder;
38 import org.apache.http.ssl.TrustStrategy;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
42 import org.springframework.context.annotation.Bean;
43 import org.springframework.stereotype.Service;
44 import org.springframework.web.bind.annotation.RequestMethod;
45
46 @EnableAutoConfiguration
47 @Service
48 public class HttpClientUtils {
49         private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
50         
51         @Bean
52         public static HttpClientBuilder createHttpClientBuilder() {
53                 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
54
55         SSLContext sslContext = null;
56                 try {
57                         sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
58                             public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
59                                 return true;
60                             }
61                         }).build();
62                 } catch (Exception e) {
63                         logger.error("Error to createHttpClientBuilder", e);
64                 }
65                 httpClientBuilder.setSSLContext(sslContext);
66                 
67                 HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
68                 
69                 SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
70                 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
71                                 .register("http", PlainConnectionSocketFactory.getSocketFactory())
72                                 .register("https", sslSocketFactory)
73                                 .build();
74                 
75                 PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
76                 connMgr.setMaxTotal(200);
77                 connMgr.setDefaultMaxPerRoute(50);
78                 httpClientBuilder.setConnectionManager(connMgr);
79                 return httpClientBuilder;
80         }
81         
82         public static HttpRequestBase getHttpRequest(RequestMethod requestMethod) {
83                 HttpRequestBase base = null;
84                 switch(requestMethod) {
85                         case GET:
86                                 base = new HttpGet();
87                                 break;
88                         case DELETE:
89                                 base = new HttpDelete();
90                                 break;
91                         default:
92                                 base = new HttpPost();
93                                 break;
94                 }
95                 return base;
96         }
97 }