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 public class CommonHttpClient {
39 public static final int HTTPS_PORT = 3905;
41 private String AUTH_STR;
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());
52 public HttpGet getReq(URI uri, int timeoutMs) throws Exception {
53 HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
54 if (AUTH_STR != null) {
55 out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
57 out.setConfig(getConfig(timeoutMs));
61 public HttpPost postReq(String url) throws Exception {
62 HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
63 if (AUTH_STR != null) {
64 out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
66 out.setConfig(getConfig(0));
70 private RequestConfig getConfig(int timeoutMs) {
71 Builder builder = RequestConfig.custom();
72 builder.setSocketTimeout(timeoutMs + 5000);
73 return builder.build();
76 public CloseableHttpClient getClient() {
77 return getClient(false);
80 public CloseableHttpClient getClient(boolean useHttps) {
81 return HttpClientBuilder.create().build();
84 public String formatHostString(String host) {
85 return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
88 public String formatHostString(String host, boolean useHttps) {
89 // Trim trailing slash
90 String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
92 boolean hasProto = out.startsWith("http");
93 boolean hasPort = out.contains(":");
97 out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
102 out = String.format("%s:%d", out, (useHttps) ? 3905 : 3904);