msb http client
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / RetrofitServiceCreater.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
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
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
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
12  * the License.
13  ******************************************************************************/
14 /**
15  * 
16  */
17 package org.onap.msb.sdk.httpclient;
18
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Proxy;
21 import java.util.ArrayList;
22 import java.util.List;
23
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;
28
29 /**
30  * @author 10071214
31  *
32  */
33 public class RetrofitServiceCreater {
34
35
36   private RetrofitServiceHandlerFactory factory = new RetrofitServiceHandlerFactory();
37
38   private MSBServiceWrapperClient msbClient;
39
40
41   public RetrofitServiceCreater(HttpClientConf globalHttpClientConf,
42       MSBServiceWrapperClient msbClient) {
43
44     RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
45     this.msbClient = msbClient;
46
47     factory.init();
48
49
50   }
51
52   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
53       ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject) {
54
55     InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
56         serviceHttpEndPointBeanObject, null, msbClient);
57
58     List<Class<?>> clazzList = new ArrayList<>();
59     clazzList.add(retrofitSrvInterfaceClazz);
60
61     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
62         clazzList.toArray(new Class[] {}), handler);
63
64     return (T) targetInterface;
65   }
66
67
68   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
69       ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject, HttpClientConf httpClientConf) {
70
71
72     InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
73         serviceHttpEndPointBeanObject, httpClientConf, msbClient);
74
75     List<Class<?>> clazzList = new ArrayList<>();
76     clazzList.add(retrofitSrvInterfaceClazz);
77
78     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
79         clazzList.toArray(new Class[] {}), handler);
80
81     return (T) targetInterface;
82   }
83
84   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
85       HttpClientConf httpClientConf) {
86
87
88     InvocationHandler handler =
89         factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, httpClientConf, msbClient);
90
91     List<Class<?>> clazzList = new ArrayList<>();
92     clazzList.add(retrofitSrvInterfaceClazz);
93
94     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
95         clazzList.toArray(new Class[] {}), handler);
96
97     return (T) targetInterface;
98   }
99
100
101   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz) {
102
103
104     InvocationHandler handler =
105         factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, null, msbClient);
106
107     List<Class<?>> clazzList = new ArrayList<>();
108     clazzList.add(retrofitSrvInterfaceClazz);
109
110     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
111         clazzList.toArray(new Class[] {}), handler);
112
113     return (T) targetInterface;
114   }
115
116
117 }