1 /*******************************************************************************
2 * Copyright 2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
13 ******************************************************************************/
14 package org.onap.msb.sdk.httpclient;
16 import java.security.SecureRandom;
17 import java.security.cert.CertificateException;
18 import java.security.cert.X509Certificate;
19 import java.util.HashMap;
21 import java.util.concurrent.TimeUnit;
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;
30 import org.onap.msb.sdk.httpclient.conf.HttpClientConf;
32 import okhttp3.OkHttpClient;
38 public class RetrofitServiceUtils {
42 private static Map<HttpClientConf, OkHttpClient> httpMap = new HashMap<>();
44 private static Map<HttpClientConf, OkHttpClient> httpsMap = new HashMap<>();
46 public synchronized static OkHttpClient buildDefaultOkHttpsClient(HttpClientConf httpClientConf)
49 if (!httpsMap.containsKey(httpClientConf)) {
50 OkHttpClient httpsOkHttpClient = buildOkHttpsClient(httpClientConf);
51 httpsMap.put(httpClientConf, httpsOkHttpClient);
53 return httpsMap.get(httpClientConf);
57 public synchronized static OkHttpClient buildDefaultOkHttpClient(HttpClientConf httpClientConf)
60 if (!httpMap.containsKey(httpClientConf)) {
61 OkHttpClient httpOkHttpClient = buildOkHttpClient(httpClientConf);
62 httpMap.put(httpClientConf, httpOkHttpClient);
65 return httpMap.get(httpClientConf);
70 private static OkHttpClient buildOkHttpsClient(HttpClientConf httpClientConf) throws Exception {
72 if (httpClientConf == null) {
73 httpClientConf = new HttpClientConf();
76 TrustManager[] trustManager = new TrustManager[] {new X509TrustManager() {
78 public void checkClientTrusted(X509Certificate[] chain, String authType)
79 throws CertificateException {}
82 public void checkServerTrusted(X509Certificate[] chain, String authType)
83 throws CertificateException {}
86 public X509Certificate[] getAcceptedIssuers() {
87 return new X509Certificate[0];
91 SSLContext sslContext = SSLContext.getInstance("SSL");
92 sslContext.init(null, trustManager, new SecureRandom());
93 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
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() {
103 public boolean verify(String hostname, SSLSession session) {
110 private static OkHttpClient buildOkHttpClient(HttpClientConf httpClientConf) throws Exception {
112 if (httpClientConf == null) {
113 httpClientConf = new HttpClientConf();
116 return new OkHttpClient.Builder()
117 .connectTimeout(httpClientConf.getConnectTimeout(), TimeUnit.MILLISECONDS)
118 .readTimeout(httpClientConf.getReadTimeout(), TimeUnit.MILLISECONDS)
119 .writeTimeout(httpClientConf.getWriteTimeout(), TimeUnit.MILLISECONDS).build();