aboutsummaryrefslogtreecommitdiffstats
path: root/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java
blob: 0ab09c99125e11c251d915892734d0ef5b0e752d (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * 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.cli.fw.http.connect;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import javax.net.ssl.HostnameVerifier;
/**
 * Helps to make http connection.<br>
 */
public class OnapHttpConnection {

    private static Logger log = LoggerFactory.getLogger(OnapHttpConnection.class); //NOSONAR

    private CloseableHttpClient httpClient = null;

    Map<String, String> mapCommonHeaders = new HashMap<> ();

    public static class TrustAllX509TrustManager implements X509TrustManager {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { //NOSONAR
            // No need to implement.
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { //NOSONAR
            // No need to implement.
        }
    }

    private void initHttpClient(boolean isSecured) throws OnapCommandHttpFailure {
        if (this.httpClient == null) {
            try {
                if (isSecured) {
                    SSLContext sslContext = SSLContext.getInstance(OnapCommandHttpConstants.SSLCONTEST_TLS);
                    sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager() },
                            new java.security.SecureRandom());
                    HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
                    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                            .<ConnectionSocketFactory>create()
                            .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build();
                    HttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);  // NOSONAR

                    this.httpClient = HttpClients.custom().setConnectionManager(connManager)
                            .setRedirectStrategy(new LaxRedirectStrategy()).build();
                } else {
                    this.httpClient = HttpClients.createDefault();  // NOSONAR
                }
            } catch (Exception e) {
                throw new OnapCommandHttpFailure(e);
            }
        }
    }

    private Map<String, String> getHttpHeaders(HttpResponse resp) {
        Map<String, String> result = new HashMap<>();

        Header[] hs = resp.getAllHeaders();
        for (int i = 0; i < hs.length; i++) {
            result.put(hs[i].getName(), hs[i].getValue());
        }

        return result;
    }

    private String getResponseBody(HttpResponse resp) throws OnapCommandHttpFailure {
        if (resp.getEntity() == null) {
            return null;
        }
        try {
            String body = EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8);
            EntityUtils.consume(resp.getEntity());
            return body;
        } catch (IOException e) {
            throw new OnapCommandHttpFailure(e);
        }
    }

    private StringEntity getStringEntity(HttpInput input) {
        return new StringEntity(input.getBody(), StandardCharsets.UTF_8);
    }

    /**
     * Post. <br>
     *
     * @param input
     *            HttpInput Obj
     * @return HttpResult
     * @throws OnapCommandHttpFailure
     *             http failure
     */
    public HttpResult post(final HttpInput input) throws OnapCommandHttpFailure {
        input.setMethod("post");
        return this.request(input);
    }

    /**
     * Get. <br>
     *
     * @param input
     *            input request
     * @return HttpResult
     * @throws OnapCommandHttpFailure
     *             excpetion
     */
    public HttpResult get(final HttpInput input) throws OnapCommandHttpFailure {
        input.setMethod("get");
        return this.request(input);
    }

    /**
     * Put. <br>
     *
     * @param input
     *            input request
     * @return HttpResult
     * @throws OnapCommandHttpFailure
     *             Exception
     */
    public HttpResult put(final HttpInput input) throws OnapCommandHttpFailure {
        input.setMethod("put");
        return this.request(input);
    }

    /**
     * Delete. <br>
     *
     * @param input
     *            input request
     * @return HttpResult
     * @throws OnapCommandHttpFailure
     *             exception
     */
    public HttpResult delete(final HttpInput input) throws OnapCommandHttpFailure {
        input.setMethod("delete");
        return this.request(input);
    }

    public void setCommonHeaders(Map<String, String> headers) {
        this.mapCommonHeaders = headers;
    }

    private void addCommonHeaders(HttpInput input) {
        if (!input.isBinaryData() && !input.getReqHeaders().containsKey("Content-Type")) {
            input.getReqHeaders().put("Content-Type", OnapCommandHttpConstants.APPLICATION_JSON);
        }

        if (!input.getReqHeaders().containsKey("Accept")) {
            input.getReqHeaders().put("Accept", OnapCommandHttpConstants.APPLICATION_JSON);
        }

        for (Entry<String, String> header : this.mapCommonHeaders.entrySet()) {
            if (!input.getReqHeaders().containsKey(header.getKey()))
                input.getReqHeaders().put(header.getKey(), header.getValue());
        }
    }

    private void addCommonCookies(HttpInput input, CookieStore cookieStore) {
         for (Entry<String, String> header : this.mapCommonHeaders.entrySet()) {
             //take care of overriden headers in OCS YAML
                String value = input.getReqHeaders().getOrDefault(header.getKey(),
                         header.getValue());
             Cookie cookie = new BasicClientCookie(header.getKey(), value);
             cookieStore.addCookie(cookie);
         }
    }

    private void updateResultFromCookies(HttpResult result, List<Cookie> cookies) {
        for (Cookie cookie : cookies) {
            result.getRespCookies().put(cookie.getName(), cookie.getValue());
        }
    }

    private String getDomain(String url) {
        try {
            return new URL(url).getHost();
        } catch (MalformedURLException e) {
            // url is always proper !!
            return url;
        }
    }

    private void updateInputFromCookies(HttpInput input, CookieStore cookieStore) {
        addCommonCookies(input, cookieStore);
        for (String cookieName : input.getReqCookies().keySet()) {
            BasicClientCookie cookie = new BasicClientCookie(cookieName, input.getReqCookies().get(cookieName));
            cookie.setDomain(this.getDomain(input.getUri()));
            cookieStore.addCookie(cookie);
        }

    }

    /**
     * Handles http method requests.
     *
     * @param input
     *            HttpInput
     * @return HttpResult
     * @throws OnapCommandHttpFailure
     *             exception
     */
    public HttpResult request(HttpInput input) throws OnapCommandHttpFailure {
        this.addCommonHeaders(input);

        HttpRequestBase requestBase = null;
        if ("post".equals(input.getMethod())) {
            HttpPost httpPost = new HttpPost();
            if (input.isBinaryData() || !(input.getMultiparts().isEmpty())) {
                httpPost.setEntity(getMultipartEntity(input));
            } else {
                httpPost.setEntity(this.getStringEntity(input));
            }
            requestBase = httpPost;
        } else if ("put".equals(input.getMethod())) {
            HttpPut httpPut = new HttpPut();
            httpPut.setEntity(this.getStringEntity(input));
            requestBase = httpPut;
        } else if ("patch".equals(input.getMethod())) {
            HttpPatch httpPatch = new HttpPatch();
            httpPatch.setEntity(this.getStringEntity(input));
            requestBase = httpPatch;
        } else if ("get".equals(input.getMethod())) {
            requestBase = new HttpGet();
        } else if ("delete".equals(input.getMethod())) {
            if (!input.getBody().isEmpty()) {
                HttpDeleteWithBody httpDelete = new HttpDeleteWithBody();
                httpDelete.setEntity(new StringEntity(input.getBody(), ContentType.APPLICATION_JSON));
                requestBase = httpDelete;
            } else {
                requestBase = new HttpDelete();
            }
        } else {
            throw new IllegalArgumentException("Invalid HTTP method");
        }

        requestBase.setURI(URI.create(input.getUri()));
        requestBase.setConfig(RequestConfig.custom()
                .setSocketTimeout(600000).setConnectTimeout(600000).build());

        for (Entry<String, String> h : input.getReqHeaders().entrySet()) {
            requestBase.addHeader(h.getKey(), h.getValue());
        }

        HttpResult result = new HttpResult();

        try {
            CookieStore cookieStore = new BasicCookieStore();
            updateInputFromCookies(input, cookieStore);
            HttpContext localContext = new BasicHttpContext();
            localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

            this.initHttpClient(input.getUri().startsWith("https"));

            HttpResponse resp = this.httpClient.execute(requestBase, localContext);
            String respContent = this.getResponseBody(resp);
            result.setBody(respContent);
            result.setStatus(resp.getStatusLine().getStatusCode());
            result.setRespHeaders(this.getHttpHeaders(resp));
            this.updateResultFromCookies(result, cookieStore.getCookies());
        } catch (Exception e) {  // NOSONAR
            throw new OnapCommandHttpFailure(e);
        }

        return result;
    }

    public void close() throws IOException {
        this.mapCommonHeaders.clear();
        if (this.httpClient != null) {
            this.httpClient.close();
        }
    }

    private HttpEntity getMultipartEntity(HttpInput input) {
        if (!input.getMultiparts().isEmpty()) {
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            for (HttpInput.Part part: input.getMultiparts()) {
                if (part.isBinary()) {
                     File file = new File(part.getContent());
                     entityBuilder.addBinaryBody(
                            part.getName(),
                            file,
                            ContentType.APPLICATION_OCTET_STREAM,
                            file.getName());
                } else {
                    entityBuilder.addTextBody(part.getName(), part.getContent(), ContentType.APPLICATION_JSON);
                }
            }

            return entityBuilder.build();
        } else {
            String fileTag = (!input.getMultipartEntityName().isEmpty()) ? input.getMultipartEntityName() : "file";
            File file = new File(input.getBody().trim());
            return MultipartEntityBuilder
                    .create()
                    .addBinaryBody(fileTag, file, ContentType.create("application/octet-stream"), file.getName())
                    .build();
        }
    }

     @Contract(threading = ThreadingBehavior.UNSAFE)
    static class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {

        public HttpDeleteWithBody() {
            super();
        }

        public String getMethod() {
            return OnapCommandHttpConstants.DELETE.toUpperCase();
        }

    }
}