remove the heavy (many MB) unused dependencies
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / httpclient / ProxyRetrofitCall.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 package org.onap.msb.sdk.httpclient;
15
16 import java.io.IOException;
17 import java.lang.reflect.Method;
18
19 import org.onap.msb.sdk.httpclient.handler.RetrofitServiceHandler;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import okhttp3.Request;
24 import retrofit2.Call;
25 import retrofit2.Callback;
26 import retrofit2.Response;
27
28 public class ProxyRetrofitCall<T extends Object> implements Call<T> {
29
30   private static final Logger logger = LoggerFactory.getLogger(ProxyRetrofitCall.class);
31
32   private Call<T> targetCall;
33
34   private RetrofitServiceHandler handler;
35
36   private ServiceHttpEndPointObject endPoint;
37
38   private Object proxy;
39
40   private Method method;
41
42   private Object[] args;
43
44   public ProxyRetrofitCall(Call targetCall, RetrofitServiceHandler retrofitServiceHandler,
45       ServiceHttpEndPointObject endPoint, Object proxy, Method method, Object[] args) {
46     super();
47     this.targetCall = targetCall;
48     this.handler = retrofitServiceHandler;
49     this.endPoint = endPoint;
50     this.proxy = proxy;
51     this.method = method;
52     this.args = args;
53   }
54
55   @Override
56   public Response<T> execute() throws IOException {
57
58
59     try {
60       return targetCall.execute();
61     } catch (Exception e) {
62
63       logger.warn("first invoke httpclient error,endPoint:{},method:{},msg:{}", endPoint, method,
64           e.getMessage());
65
66       // 清理残留的endpoint记录
67       handler.clean();
68       try {
69         return ((Call) handler.reInvoke(proxy, method, args, endPoint)).execute();
70       } catch (IOException e1) {
71         logger.error("sencond invoke httpclient error,endPoint:{},method:{}", endPoint, method, e1);
72         throw e1;
73       } catch (Throwable e2) {
74         logger.error("sencond invoke httpclient error,endPoint:{},method:{}", endPoint, method, e2);
75         throw e;
76       }
77
78
79
80     } finally {
81
82     }
83
84   }
85
86   @Override
87   public void enqueue(Callback<T> callback) {
88     targetCall.enqueue(callback);
89   }
90
91   @Override
92   public boolean isExecuted() {
93     return targetCall.isExecuted();
94   }
95
96   @Override
97   public void cancel() {
98     targetCall.cancel();
99
100   }
101
102   @Override
103   public boolean isCanceled() {
104     // TODO Auto-generated method stub
105     return targetCall.isCanceled();
106   }
107
108   @Override
109   public Call<T> clone() {
110     // TODO Auto-generated method stub
111     return new ProxyRetrofitCall(targetCall.clone(), this.handler, this.endPoint, this.proxy,
112         this.method, this.args);
113   }
114
115   @Override
116   public Request request() {
117     return targetCall.request();
118   }
119
120 }