aboutsummaryrefslogtreecommitdiffstats
path: root/apiroute/apiroute-service/src/main/java/org/onap/msb/apiroute/wrapper/consulextend/util/Http.java
blob: ce2dc7c27566ef806e6daaad1bf790d108daa297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright 2016-2017 ZTE, Inc. and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package org.onap.msb.apiroute.wrapper.consulextend.util;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.eclipse.jetty.http.HttpStatus;
import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseCallback;
import org.onap.msb.apiroute.wrapper.consulextend.async.ConsulResponseHeader;
import org.onap.msb.apiroute.wrapper.consulextend.async.OriginalConsulResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.orbitz.consul.ConsulException;
import com.orbitz.consul.model.ConsulResponse;
import com.orbitz.consul.option.CatalogOptions;
import com.orbitz.consul.option.QueryOptions;
import com.orbitz.consul.util.Jackson;

public class Http {
    private static final Logger LOGGER = LoggerFactory.getLogger(Http.class);

    private final static CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom()
                    .setMaxConnTotal(Integer.MAX_VALUE).setMaxConnPerRoute(Integer.MAX_VALUE).build();

    private static Http instance = null;

    private Http() {}

    public static Http getInstance() {
        if (instance == null) {
            instance = new Http();
            httpAsyncClient.start();
        }

        return instance;
    }

    // async get data from consul,and handle response immediately
    public <T> void asyncGet(String requestURI, final TypeReference<T> responseType,
                    final ConsulResponseCallback<T> callback, final Integer... okCodes) {
        // LOGGER.info("Async request:"+requestURI);

        httpAsyncClient.execute(new HttpGet(requestURI), new FutureCallback<HttpResponse>() {

            public void completed(final HttpResponse response) {
                callback.onComplete(consulResponse(responseType, response));
            }

            public void failed(final Exception ex) {
                callback.onFailure(ex);
            }

            public void cancelled() {
                LOGGER.warn("cancelled async request");
            }
        });
    }

    // async get data from consul,and handle response delay
    public <T> void asyncGetDelayHandle(String requestURI, final TypeReference<T> responseType,
                    final ConsulResponseCallback<T> callback, final Integer... okCodes) {

        httpAsyncClient.execute(new HttpGet(requestURI), new FutureCallback<HttpResponse>() {

            public void completed(final HttpResponse response) {
                OriginalConsulResponse<T> originalConsulResponse =
                                new OriginalConsulResponse<T>(response, responseType);

                // handle not 2xx code
                if (!isSuccessful(response)) {

                    LOGGER.warn("response statuscode:" + response.getStatusLine().getStatusCode());

                    callback.onFailure(new ConsulException(
                                    "response statuscode:" + response.getStatusLine().getStatusCode()));
                } else {
                    callback.onDelayComplete(originalConsulResponse);
                }

            }

            public void failed(final Exception ex) {
                callback.onFailure(ex);
            }

            public void cancelled() {
                LOGGER.warn("cancelled async request");
            }
        });
    }

    public static ConsulResponseHeader consulResponseHeader(HttpResponse response) {
        String indexHeaderValue = response.getFirstHeader("X-Consul-Index").getValue();
        String lastContactHeaderValue = response.getFirstHeader("X-Consul-Lastcontact").getValue();
        String knownLeaderHeaderValue = response.getFirstHeader("X-Consul-Knownleader").getValue();

        BigInteger index = indexHeaderValue == null ? new BigInteger("0") : new BigInteger(indexHeaderValue);
        long lastContact = lastContactHeaderValue == null ? 0 : Long.parseLong(lastContactHeaderValue);
        boolean knownLeader = knownLeaderHeaderValue == null ? false : Boolean.parseBoolean(knownLeaderHeaderValue);

        return new ConsulResponseHeader(lastContact, knownLeader, index);
    }

    public static <T> ConsulResponse<T> consulResponse(TypeReference<T> responseType, HttpResponse response) {

        String indexHeaderValue = response.getFirstHeader("X-Consul-Index").getValue();
        String lastContactHeaderValue = response.getFirstHeader("X-Consul-Lastcontact").getValue();
        String knownLeaderHeaderValue = response.getFirstHeader("X-Consul-Knownleader").getValue();

        BigInteger index = indexHeaderValue == null ? new BigInteger("0") : new BigInteger(indexHeaderValue);
        long lastContact = lastContactHeaderValue == null ? 0 : Long.parseLong(lastContactHeaderValue);
        boolean knownLeader = knownLeaderHeaderValue == null ? false : Boolean.parseBoolean(knownLeaderHeaderValue);

        ConsulResponse<T> consulResponse =
                        new ConsulResponse<T>(readResponse(response, responseType), lastContact, knownLeader, index);
        return consulResponse;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static <T> T readResponse(HttpResponse response, TypeReference<T> responseType) {

        // read streamed entity
        T object;

        // HttpEntity,read original data.
        Type _type = responseType.getType();
        if (_type instanceof Class && (((Class) _type).isAssignableFrom(HttpEntity.class))) {
            object = (T) response.getEntity();
            return object;
        }

        // String,read original data.
        if (_type instanceof Class && (((Class) _type).isAssignableFrom(String.class))) {

            try {

                object = (T) IOUtils.toString(response.getEntity().getContent());
                response.getEntity().getContent().close();

            } catch (UnsupportedOperationException e) {
                object = (T) "";
                LOGGER.warn("covert streamed entity to String exception:", e);
            } catch (IOException e) {
                object = (T) "";
                LOGGER.warn("covert streamed entity to String exception:", e);
            }

            return object;
        }

        // change data type
        try {
            object = Jackson.MAPPER.readValue(response.getEntity().getContent(), responseType);
        } catch (IOException e) {
            LOGGER.warn("covert streamed entity to object exception:", e);
            object = readDefaultResponse(responseType);
        }

        return object;
    }

    @SuppressWarnings("unchecked")
    public static <T> T readDefaultResponse(TypeReference<T> responseType) {
        Type _type = responseType.getType();
        if (_type instanceof ParameterizedType && ((ParameterizedType) _type).getRawType() == List.class) {
            return (T) ImmutableList.of();
        } else if (_type instanceof ParameterizedType && ((ParameterizedType) _type).getRawType() == Map.class) {
            return (T) ImmutableMap.of();
        } else {
            // Not sure if this case will be reached, but if it is it'll be nice
            // to know
            throw new IllegalStateException("Cannot determine empty representation for " + _type);
        }
    }

    public static boolean isSuccessful(HttpResponse response, Integer... okCodes) {
        return HttpStatus.isSuccess(response.getStatusLine().getStatusCode())
                        || Sets.newHashSet(okCodes).contains(response.getStatusLine().getStatusCode());
    }

    public static String optionsFrom(CatalogOptions catalogOptions, QueryOptions queryOptions) {
        String params = "";

        if (catalogOptions != null) {
            Map<String, Object> options = catalogOptions.toQuery();

            if (options.containsKey("dc")) {
                params += "dc=" + options.get("dc");
            }
            if (options.containsKey("tag")) {
                params += params.isEmpty() ? "" : "&";
                params += "tag=" + options.get("tag");
            }
        }

        if (queryOptions != null) {
            Map<String, Object> options = queryOptions.toQuery();

            if (options.containsKey("consistent")) {
                params += params.isEmpty() ? "" : "&";
                params += "consistent=" + options.get("consistent");
            }
            if (options.containsKey("stale")) {
                params += params.isEmpty() ? "" : "&";
                params += "stale=" + options.get("stale");
            }
            if (options.containsKey("wait")) {
                params += params.isEmpty() ? "" : "&";
                params += "wait=" + options.get("wait");
            }

            if (options.containsKey("index")) {
                params += params.isEmpty() ? "" : "&";
                params += "index=" + options.get("index");
            }
            if (options.containsKey("token")) {
                params += params.isEmpty() ? "" : "&";
                params += "token=" + options.get("token");
            }
            if (options.containsKey("near")) {
                params += params.isEmpty() ? "" : "&";
                params += "near=" + options.get("near");
            }
            if (options.containsKey("dc")) {
                params += params.isEmpty() ? "" : "&";
                params += "dc=" + options.get("dc");
            }
        }
        return params;
    }
}