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