2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap.http;
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;
37 abstract class CommonHttpClient {
39 private static final int HTTP_PORT = 3904;
40 private static final int HTTPS_PORT = 3905;
41 private static final int TIMEOUT_OFFSET = 5000;
43 private String authStr;
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());
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.");
59 HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
60 out.setHeader("Authorization", String.format("Basic %s", authStr));
61 out.setConfig(getConfig(timeoutMs));
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.");
70 HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
71 out.setHeader("Authorization", String.format("Basic %s", authStr));
72 out.setConfig(getConfig(0));
76 private RequestConfig getConfig(int timeoutMs) {
77 Builder builder = RequestConfig.custom();
78 builder.setSocketTimeout(timeoutMs + TIMEOUT_OFFSET);
79 return builder.build();
82 protected CloseableHttpClient getClient() {
83 return HttpClientBuilder.create().build();
86 protected String formatHostString(String host) {
87 return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
90 private String formatHostString(String host, boolean useHttps) {
91 // Trim trailing slash
92 String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
94 boolean hasProtocol = out.startsWith("http");
95 boolean hasPort = out.contains(":");
99 out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
103 out = String.format("%s:%d", out, (useHttps) ? HTTPS_PORT : HTTP_PORT);