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 ******************************************************************************/
17 package org.onap.msb.sdk.httpclient;
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Proxy;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.onap.msb.sdk.httpclient.conf.HttpClientConf;
25 import org.onap.msb.sdk.httpclient.handler.RetrofitServiceHandlerContext;
26 import org.onap.msb.sdk.httpclient.handler.RetrofitServiceHandlerFactory;
27 import org.onap.msb.sdk.httpclient.msb.MSBServiceWrapperClient;
33 public class RetrofitServiceCreater {
36 private RetrofitServiceHandlerFactory factory = new RetrofitServiceHandlerFactory();
38 private MSBServiceWrapperClient msbClient;
41 public RetrofitServiceCreater(HttpClientConf globalHttpClientConf,
42 MSBServiceWrapperClient msbClient) {
44 RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
45 this.msbClient = msbClient;
52 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
53 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject) {
55 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
56 serviceHttpEndPointBeanObject, null, msbClient);
58 List<Class<?>> clazzList = new ArrayList<>();
59 clazzList.add(retrofitSrvInterfaceClazz);
61 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
62 clazzList.toArray(new Class[] {}), handler);
64 return (T) targetInterface;
68 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
69 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject, HttpClientConf httpClientConf) {
72 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
73 serviceHttpEndPointBeanObject, httpClientConf, msbClient);
75 List<Class<?>> clazzList = new ArrayList<>();
76 clazzList.add(retrofitSrvInterfaceClazz);
78 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
79 clazzList.toArray(new Class[] {}), handler);
81 return (T) targetInterface;
84 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
85 HttpClientConf httpClientConf) {
88 InvocationHandler handler =
89 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, httpClientConf, msbClient);
91 List<Class<?>> clazzList = new ArrayList<>();
92 clazzList.add(retrofitSrvInterfaceClazz);
94 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
95 clazzList.toArray(new Class[] {}), handler);
97 return (T) targetInterface;
101 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz) {
104 InvocationHandler handler =
105 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, null, msbClient);
107 List<Class<?>> clazzList = new ArrayList<>();
108 clazzList.add(retrofitSrvInterfaceClazz);
110 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
111 clazzList.toArray(new Class[] {}), handler);
113 return (T) targetInterface;