2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.http;
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.http.client.config.RequestConfig;
30 import org.apache.http.client.config.RequestConfig.Builder;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.impl.client.CloseableHttpClient;
34 import org.apache.http.impl.client.HttpClientBuilder;
36 abstract class CommonHttpClient {
38 private static final int HTTP_PORT = 3904;
39 private static final int HTTPS_PORT = 3905;
40 private static final int TIMEOUT_OFFSET = 5000;
42 private String authStr;
44 protected void setBasicAuth(String username, String password) {
45 if (username != null && password != null) {
46 String plain = String.format("%s:%s", username, password);
47 authStr = Base64.encodeBase64String(plain.getBytes());
53 protected HttpGet getReq(URI uri, int timeoutMs) throws AuthenticationException {
55 HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
56 if (authStr != null) {
57 out.setHeader("Authorization", String.format("Basic %s", authStr));
60 out.setConfig(getConfig(timeoutMs));
64 protected HttpPost postReq(String url) throws AuthenticationException {
66 HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
67 if (authStr != null) {
68 out.setHeader("Authorization", String.format("Basic %s", authStr));
70 out.setConfig(getConfig(0));
74 private RequestConfig getConfig(int timeoutMs) {
75 Builder builder = RequestConfig.custom();
76 builder.setSocketTimeout(timeoutMs + TIMEOUT_OFFSET);
77 return builder.build();
80 protected CloseableHttpClient getClient() {
81 return HttpClientBuilder.create().build();
84 protected String formatHostString(String host) {
85 return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
88 private String formatHostString(String host, boolean useHttps) {
89 // Trim trailing slash
90 String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
92 boolean hasProtocol = out.startsWith("http");
93 boolean hasPort = out.contains(":");
97 out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
101 out = String.format("%s:%d", out, (useHttps) ? HTTPS_PORT : HTTP_PORT);