0749eb18553e8c53ff75877418ae3b842aa2738a
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.messaging.dmaap.http;
26
27 import java.net.URI;
28
29 import org.apache.commons.codec.binary.Base64;
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         if (authStr == null) {
56             throw new AuthenticationException("All DMaaP requests require authentication and none was provided.");
57         }
58
59         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
60         out.setHeader("Authorization", String.format("Basic %s", authStr));
61         out.setConfig(getConfig(timeoutMs));
62         return out;
63     }
64
65     protected HttpPost postReq(String url) throws AuthenticationException {
66         if (authStr == null) {
67             throw new AuthenticationException("All DMaaP requests require authentication and none was provided.");
68         }
69
70         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
71         out.setHeader("Authorization", String.format("Basic %s", authStr));
72         out.setConfig(getConfig(0));
73         return out;
74     }
75
76     private RequestConfig getConfig(int timeoutMs) {
77         Builder builder = RequestConfig.custom();
78         builder.setSocketTimeout(timeoutMs + TIMEOUT_OFFSET);
79         return builder.build();
80     }
81
82     protected CloseableHttpClient getClient() {
83         return HttpClientBuilder.create().build();
84     }
85
86     protected String formatHostString(String host) {
87         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
88     }
89
90     private String formatHostString(String host, boolean useHttps) {
91         // Trim trailing slash
92         String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
93
94         boolean hasProtocol = out.startsWith("http");
95         boolean hasPort = out.contains(":");
96
97         // Add protocol
98         if (!hasProtocol) {
99             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
100         }
101         // Add port
102         if (!hasPort) {
103             out = String.format("%s:%d", out, (useHttps) ? HTTPS_PORT : HTTP_PORT);
104         }
105         return out;
106     }
107 }