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