msb http client
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / RetrofitServiceUtils.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.httpclient;
15
16 import java.security.SecureRandom;
17 import java.security.cert.CertificateException;
18 import java.security.cert.X509Certificate;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.net.ssl.HostnameVerifier;
24 import javax.net.ssl.SSLContext;
25 import javax.net.ssl.SSLSession;
26 import javax.net.ssl.SSLSocketFactory;
27 import javax.net.ssl.TrustManager;
28 import javax.net.ssl.X509TrustManager;
29
30 import org.onap.msb.sdk.httpclient.conf.HttpClientConf;
31
32 import okhttp3.OkHttpClient;
33
34 /**
35  * @author 10071214
36  *
37  */
38 public class RetrofitServiceUtils {
39
40
41
42   private static Map<HttpClientConf, OkHttpClient> httpMap = new HashMap<>();
43
44   private static Map<HttpClientConf, OkHttpClient> httpsMap = new HashMap<>();
45
46   public synchronized static OkHttpClient buildDefaultOkHttpsClient(HttpClientConf httpClientConf)
47       throws Exception {
48
49     if (!httpsMap.containsKey(httpClientConf)) {
50       OkHttpClient httpsOkHttpClient = buildOkHttpsClient(httpClientConf);
51       httpsMap.put(httpClientConf, httpsOkHttpClient);
52     }
53     return httpsMap.get(httpClientConf);
54   }
55
56
57   public synchronized static OkHttpClient buildDefaultOkHttpClient(HttpClientConf httpClientConf)
58       throws Exception {
59
60     if (!httpMap.containsKey(httpClientConf)) {
61       OkHttpClient httpOkHttpClient = buildOkHttpClient(httpClientConf);
62       httpMap.put(httpClientConf, httpOkHttpClient);
63     }
64
65     return httpMap.get(httpClientConf);
66   }
67
68
69
70   private static OkHttpClient buildOkHttpsClient(HttpClientConf httpClientConf) throws Exception {
71
72     if (httpClientConf == null) {
73       httpClientConf = new HttpClientConf();
74     }
75
76     TrustManager[] trustManager = new TrustManager[] {new X509TrustManager() {
77       @Override
78       public void checkClientTrusted(X509Certificate[] chain, String authType)
79           throws CertificateException {}
80
81       @Override
82       public void checkServerTrusted(X509Certificate[] chain, String authType)
83           throws CertificateException {}
84
85       @Override
86       public X509Certificate[] getAcceptedIssuers() {
87         return new X509Certificate[0];
88       }
89     }};
90
91     SSLContext sslContext = SSLContext.getInstance("SSL");
92     sslContext.init(null, trustManager, new SecureRandom());
93     SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
94
95
96
97     return new OkHttpClient.Builder()
98         .connectTimeout(httpClientConf.getConnectTimeout(), TimeUnit.MILLISECONDS)
99         .readTimeout(httpClientConf.getReadTimeout(), TimeUnit.MILLISECONDS)
100         .writeTimeout(httpClientConf.getWriteTimeout(), TimeUnit.MILLISECONDS)
101         .sslSocketFactory(sslSocketFactory).hostnameVerifier(new HostnameVerifier() {
102           @Override
103           public boolean verify(String hostname, SSLSession session) {
104             return true;
105           }
106         }).build();
107   }
108
109
110   private static OkHttpClient buildOkHttpClient(HttpClientConf httpClientConf) throws Exception {
111
112     if (httpClientConf == null) {
113       httpClientConf = new HttpClientConf();
114     }
115
116     return new OkHttpClient.Builder()
117         .connectTimeout(httpClientConf.getConnectTimeout(), TimeUnit.MILLISECONDS)
118         .readTimeout(httpClientConf.getReadTimeout(), TimeUnit.MILLISECONDS)
119         .writeTimeout(httpClientConf.getWriteTimeout(), TimeUnit.MILLISECONDS).build();
120   }
121
122 }