Modify dmaap adapter license headers.
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / onap / appc / adapter / messaging / dmaap / http / CommonHttpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.adapter.messaging.dmaap.http;
25
26 import java.net.URI;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.http.client.config.RequestConfig;
30 import org.apache.http.client.config.RequestConfig.Builder;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClientBuilder;
35
36 abstract class CommonHttpClient {
37
38     private static final int HTTP_PORT = 3904;
39     private static final int HTTPS_PORT = 3905;
40     private static final int TIMEOUT_OFFSET = 5000;
41
42     private String authStr;
43
44     protected void setBasicAuth(String username, String password) {
45         if (username != null && password != null) {
46             String plain = String.format("%s:%s", username, password);
47             authStr = Base64.encodeBase64String(plain.getBytes());
48         } else {
49             authStr = null;
50         }
51     }
52
53     protected HttpGet getReq(URI uri, int timeoutMs) throws AuthenticationException {
54
55         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
56         if (authStr != null) {
57             out.setHeader("Authorization", String.format("Basic %s", authStr));
58         }
59       
60         out.setConfig(getConfig(timeoutMs));
61         return out;
62     }
63
64     protected HttpPost postReq(String url) throws AuthenticationException {
65
66         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
67         if (authStr != null) {
68             out.setHeader("Authorization", String.format("Basic %s", authStr));
69         }
70         out.setConfig(getConfig(0));
71         return out;
72     }
73
74     private RequestConfig getConfig(int timeoutMs) {
75         Builder builder = RequestConfig.custom();
76         builder.setSocketTimeout(timeoutMs + TIMEOUT_OFFSET);
77         return builder.build();
78     }
79
80     protected CloseableHttpClient getClient() {
81         return HttpClientBuilder.create().build();
82     }
83
84     protected String formatHostString(String host) {
85         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
86     }
87
88     private String formatHostString(String host, boolean useHttps) {
89         // Trim trailing slash
90         String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
91
92         boolean hasProtocol = out.startsWith("http");
93         boolean hasPort = out.contains(":");
94
95         // Add protocol
96         if (!hasProtocol) {
97             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
98         }
99         // Add port
100         if (!hasPort) {
101             out = String.format("%s:%d", out, (useHttps) ? HTTPS_PORT : HTTP_PORT);
102         }
103         return out;
104     }
105 }