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.MSBServiceClient;
29 public class RetrofitServiceCreater {
32 private RetrofitServiceHandlerFactory factory = new RetrofitServiceHandlerFactory();
34 private MSBServiceClient msbClient;
37 public RetrofitServiceCreater(HttpClientConf globalHttpClientConf,
38 MSBServiceClient msbClient) {
40 RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
41 this.msbClient = msbClient;
48 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
49 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject) {
51 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
52 serviceHttpEndPointBeanObject, null, msbClient);
54 List<Class<?>> clazzList = new ArrayList<>();
55 clazzList.add(retrofitSrvInterfaceClazz);
57 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
58 clazzList.toArray(new Class[] {}), handler);
60 return (T) targetInterface;
64 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
65 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject, HttpClientConf httpClientConf) {
68 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
69 serviceHttpEndPointBeanObject, httpClientConf, msbClient);
71 List<Class<?>> clazzList = new ArrayList<>();
72 clazzList.add(retrofitSrvInterfaceClazz);
74 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
75 clazzList.toArray(new Class[] {}), handler);
77 return (T) targetInterface;
80 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
81 HttpClientConf httpClientConf) {
84 InvocationHandler handler =
85 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, httpClientConf, msbClient);
87 List<Class<?>> clazzList = new ArrayList<>();
88 clazzList.add(retrofitSrvInterfaceClazz);
90 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
91 clazzList.toArray(new Class[] {}), handler);
93 return (T) targetInterface;
97 public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz) {
100 InvocationHandler handler =
101 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, null, msbClient);
103 List<Class<?>> clazzList = new ArrayList<>();
104 clazzList.add(retrofitSrvInterfaceClazz);
106 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
107 clazzList.toArray(new Class[] {}), handler);
109 return (T) targetInterface;