Change comments to English
[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.MSBServiceClient;
28
29 public class RetrofitServiceCreater {
30
31
32   private RetrofitServiceHandlerFactory factory = new RetrofitServiceHandlerFactory();
33
34   private MSBServiceClient msbClient;
35
36
37   public RetrofitServiceCreater(HttpClientConf globalHttpClientConf,
38       MSBServiceClient msbClient) {
39
40     RetrofitServiceHandlerContext.setGlobalHttpClientConf(globalHttpClientConf);
41     this.msbClient = msbClient;
42
43     factory.init();
44
45
46   }
47
48   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
49       ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject) {
50
51     InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
52         serviceHttpEndPointBeanObject, null, msbClient);
53
54     List<Class<?>> clazzList = new ArrayList<>();
55     clazzList.add(retrofitSrvInterfaceClazz);
56
57     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
58         clazzList.toArray(new Class[] {}), handler);
59
60     return (T) targetInterface;
61   }
62
63
64   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
65       ServiceHttpEndPointBeanObject serviceHttpEndPointBeanObject, HttpClientConf httpClientConf) {
66
67
68     InvocationHandler handler = factory.buildInvocationHandler(retrofitSrvInterfaceClazz,
69         serviceHttpEndPointBeanObject, httpClientConf, msbClient);
70
71     List<Class<?>> clazzList = new ArrayList<>();
72     clazzList.add(retrofitSrvInterfaceClazz);
73
74     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
75         clazzList.toArray(new Class[] {}), handler);
76
77     return (T) targetInterface;
78   }
79
80   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz,
81       HttpClientConf httpClientConf) {
82
83
84     InvocationHandler handler =
85         factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, httpClientConf, msbClient);
86
87     List<Class<?>> clazzList = new ArrayList<>();
88     clazzList.add(retrofitSrvInterfaceClazz);
89
90     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
91         clazzList.toArray(new Class[] {}), handler);
92
93     return (T) targetInterface;
94   }
95
96
97   public <T> T createRetrofitService(Class<T> retrofitSrvInterfaceClazz) {
98
99
100     InvocationHandler handler =
101         factory.buildInvocationHandler(retrofitSrvInterfaceClazz, null, null, msbClient);
102
103     List<Class<?>> clazzList = new ArrayList<>();
104     clazzList.add(retrofitSrvInterfaceClazz);
105
106     Object targetInterface = Proxy.newProxyInstance(retrofitSrvInterfaceClazz.getClassLoader(),
107         clazzList.toArray(new Class[] {}), handler);
108
109     return (T) targetInterface;
110   }
111
112
113 }