Feature for micro service communication
[appc.git] / appc-service-communicator / appc-service-communicator-bundle / src / main / java / org / onap / appc / srvcomm / messaging / CommonHttpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.srvcomm.messaging;
25
26 import java.net.URI;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.http.auth.AuthenticationException;
30 import org.apache.http.client.config.RequestConfig;
31 import org.apache.http.client.config.RequestConfig.Builder;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.impl.client.HttpClientBuilder;
36
37 abstract class CommonHttpClient {
38
39     private static final int HTTP_PORT = 3904;
40     private static final int HTTPS_PORT = 3905;
41     private static final int TIMEOUT_OFFSET = 5000;
42
43     private String authStr;
44
45     protected void setBasicAuth(String username, String password) {
46         if (username != null && password != null) {
47             String plain = String.format("%s:%s", username, password);
48             authStr = Base64.encodeBase64String(plain.getBytes());
49         } else {
50             authStr = null;
51         }
52     }
53
54     protected HttpGet getReq(URI uri, int timeoutMs) throws AuthenticationException {
55
56         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
57         if (authStr != null) {
58             out.setHeader("Authorization", String.format("Basic %s", authStr));
59         }
60       
61         out.setConfig(getConfig(timeoutMs));
62         return out;
63     }
64
65     protected HttpPost postReq(String url) throws AuthenticationException {
66
67         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
68         if (authStr != null) {
69             out.setHeader("Authorization", String.format("Basic %s", authStr));
70         }
71         out.setConfig(getConfig(0));
72         return out;
73     }
74
75     private RequestConfig getConfig(int timeoutMs) {
76         Builder builder = RequestConfig.custom();
77         builder.setSocketTimeout(timeoutMs + TIMEOUT_OFFSET);
78         return builder.build();
79     }
80
81     protected CloseableHttpClient getClient() {
82         return HttpClientBuilder.create().build();
83     }
84
85     protected String formatHostString(String host) {
86         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
87     }
88
89     private String formatHostString(String host, boolean useHttps) {
90         // Trim trailing slash
91         String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
92
93         boolean hasProtocol = out.startsWith("http");
94         boolean hasPort = out.contains(":");
95
96         // Add protocol
97         if (!hasProtocol) {
98             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
99         }
100         // Add port
101         if (!hasPort) {
102             out = String.format("%s:%d", out, (useHttps) ? HTTPS_PORT : HTTP_PORT);
103         }
104         return out;
105     }
106 }