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