76b050d8e274bd7cac196e8a03e08b601d62757d
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / 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 public class CommonHttpClient {
38
39     public static final int HTTPS_PORT = 3905;
40
41     private String AUTH_STR;
42
43     protected void setBasicAuth(String username, String password) {
44         if (username != null && password != null) {
45             String plain = String.format("%s:%s", username, password);
46             AUTH_STR = Base64.encodeBase64String(plain.getBytes());
47         } else {
48             AUTH_STR = null;
49         }
50     }
51
52     public HttpGet getReq(URI uri, int timeoutMs) throws Exception {
53         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
54         if (AUTH_STR != null) {
55             out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
56         }
57         out.setConfig(getConfig(timeoutMs));
58         return out;
59     }
60
61     public HttpPost postReq(String url) throws Exception {
62         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
63         if (AUTH_STR != null) {
64             out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
65         }
66         out.setConfig(getConfig(0));
67         return out;
68     }
69
70     private RequestConfig getConfig(int timeoutMs) {
71         Builder builder = RequestConfig.custom();
72         builder.setSocketTimeout(timeoutMs + 5000);
73         return builder.build();
74     }
75
76     public CloseableHttpClient getClient() {
77         return getClient(false);
78     }
79
80     public CloseableHttpClient getClient(boolean useHttps) {
81         return HttpClientBuilder.create().build();
82     }
83
84     public String formatHostString(String host) {
85         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
86     }
87
88     public 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 hasProto = out.startsWith("http");
93         boolean hasPort = out.contains(":");
94
95         // Add protocol
96         if (!hasProto) {
97             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
98         }
99
100         // Add port
101         if (!hasPort) {
102             out = String.format("%s:%d", out, (useHttps) ? 3905 : 3904);
103         }
104
105         return out;
106
107     }
108 }