code changes for platform hardening appc adapters
[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 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          if (AUTH_STR == null) {
54             throw new Exception("All DMaaP requests require authentication and none was provided.");
55         }
56
57         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
58         out.setHeader("Authorization", String.format("Basic %s", AUTH_STR)); 
59         out.setConfig(getConfig(timeoutMs));
60         return out;
61     }
62
63     public HttpPost postReq(String url) throws Exception {
64         if (AUTH_STR == null) {
65             throw new Exception("All DMaaP requests require authentication and none was provided.");
66         }
67
68         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
69         out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
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 + 5000);
77         return builder.build();
78     }
79
80     public CloseableHttpClient getClient() {
81         return getClient(false);
82     }
83
84     public CloseableHttpClient getClient(boolean useHttps) {
85         return HttpClientBuilder.create().build();
86     }
87
88     public String formatHostString(String host) {
89         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
90     }
91
92     public String formatHostString(String host, boolean useHttps) {
93         // Trim trailing slash
94         String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
95
96         boolean hasProto = out.startsWith("http");
97         boolean hasPort = out.contains(":");
98
99         // Add protocol
100         if (!hasProto) {
101             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
102         }
103
104         // Add port
105         if (!hasPort) {
106             out = String.format("%s:%d", out, (useHttps) ? 3905 : 3904);
107         }
108
109         return out;
110
111     }
112 }