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 RestServiceCreater {
32 private RetrofitServiceHandlerFactory factory = new RetrofitServiceHandlerFactory();
34 private MSBServiceClient msbClient;
36 public RestServiceCreater(MSBServiceClient msbClient) {
37 this(new HttpClientConf(), msbClient);
40 public RestServiceCreater(HttpClientConf globalHttpClientConf,
41 MSBServiceClient msbClient) {
43 RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
44 this.msbClient = msbClient;
49 public <T> T createService(Class<T> retrofitSrvInterfaceClazz,
50 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject) {
52 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
53 serviceHttpEndPointBeanObject, null, msbClient);
55 List<Class<?>> clazzList = new ArrayList<>();
56 clazzList.add(retrofitSrvInterfaceClazz);
58 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
59 clazzList.toArray(new Class[] {}), handler);
61 return (T) targetInterface;
65 public <T> T createService(Class<T> retrofitSrvInterfaceClazz,
66 ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject, HttpClientConf httpClientConf) {
69 InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
70 serviceHttpEndPointBeanObject, httpClientConf, msbClient);
72 List<Class<?>> clazzList = new ArrayList<>();
73 clazzList.add(retrofitSrvInterfaceClazz);
75 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
76 clazzList.toArray(new Class[] {}), handler);
78 return (T) targetInterface;
81 public <T> T createService(Class<T> retrofitSrvInterfaceClazz,
82 HttpClientConf httpClientConf) {
85 InvocationHandler handler =
86 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, httpClientConf, msbClient);
88 List<Class<?>> clazzList = new ArrayList<>();
89 clazzList.add(retrofitSrvInterfaceClazz);
91 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
92 clazzList.toArray(new Class[] {}), handler);
94 return (T) targetInterface;
98 public <T> T createService(Class<T> retrofitSrvInterfaceClazz) {
101 InvocationHandler handler =
102 factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, null, msbClient);
104 List<Class<?>> clazzList = new ArrayList<>();
105 clazzList.add(retrofitSrvInterfaceClazz);
107 Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
108 clazzList.toArray(new Class[] {}), handler);
110 return (T) targetInterface;