From 96d16d95ec27c62722a601f5bf8e735965692b8f Mon Sep 17 00:00:00 2001 From: hekeguang Date: Mon, 8 Aug 2022 15:50:01 +0800 Subject: Add code to support interaction with other ONAP components. Issue-ID: USECASEUI-696 Change-Id: Ic403827c7dc443bb20d0e0971155ce3a36730be3 Signed-off-by: hekeguang --- .../intentanalysis/util/CustomTrustManager.java | 38 +++++++ .../intentanalysis/util/RestfulServices.java | 118 +++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/CustomTrustManager.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java (limited to 'intentanalysis/src/main') diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/CustomTrustManager.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/CustomTrustManager.java new file mode 100644 index 0000000..daed733 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/CustomTrustManager.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.utils; + +import javax.net.ssl.X509TrustManager; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +public class CustomTrustManager implements X509TrustManager { + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java new file mode 100644 index 0000000..f37021f --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.utils; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.RequestBody; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; + +import javax.net.ssl.*; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.security.SecureRandom; +import java.util.concurrent.TimeUnit; + +public class RestfulServices { + + private static final Logger logger = LoggerFactory.getLogger(RestfulServices.class); + + public static T create(String baseUrl, Class clazz) { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(baseUrl) + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + return retrofit.create(clazz); + } + + public static T create(Class clazz) { + //Set the interface response time + + OkHttpClient okHttpClient = new OkHttpClient.Builder() + .connectTimeout(300, TimeUnit.SECONDS) + .readTimeout(300, TimeUnit.SECONDS) + .sslSocketFactory(getSSLSocketFactory(), new CustomTrustManager()) + .hostnameVerifier(getHostnameVerifier()) + .build(); + + String msbUrl = getMsbAddress(); + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://" + msbUrl + "/") + .client(okHttpClient) + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + + return retrofit.create(clazz); + } + + public static String getMsbAddress() { + String msbAddress = System.getenv("MSB_ADDR"); + if (msbAddress == null) { + return ""; + } + return msbAddress; + } + + public static RequestBody extractBody(HttpServletRequest request) throws IOException { + BufferedReader br = null; + StringBuilder sb = new StringBuilder(""); + try { + br = request.getReader(); + String str; + while ((str = br.readLine()) != null) { + sb.append(str); + } + br.close(); + logger.info("The request body content is: " + sb.toString()); + return RequestBody.create(MediaType.parse("application/json"), sb.toString()); + } catch (Exception e) { + logger.info("RestfulServices occur exection,this content is: " + e.getMessage()); + return RequestBody.create(MediaType.parse("application/json"), sb.toString()); + } finally { + if (null != br) { + br.close(); + } + } + } + + public static SSLSocketFactory getSSLSocketFactory() { + SSLSocketFactory ssfFactory = null; + + try { + SSLContext sc = SSLContext.getInstance("TLS"); + sc.init(null, new TrustManager[]{new CustomTrustManager()}, new SecureRandom()); + + ssfFactory = sc.getSocketFactory(); + } catch (Exception e) { + } + + return ssfFactory; + } + + public static HostnameVerifier getHostnameVerifier() { + HostnameVerifier hostnameVerifier= new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + return hostnameVerifier; + } +} + -- cgit 1.2.3-korg