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