2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.adapter.messaging.dmaap.http;
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;
35 public class CommonHttpClient {
37 public static final int HTTPS_PORT = 3905;
39 private String AUTH_STR;
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());
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));
55 out.setConfig(getConfig(timeoutMs));
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));
64 out.setConfig(getConfig(0));
68 private RequestConfig getConfig(int timeoutMs) {
69 Builder builder = RequestConfig.custom();
70 builder.setSocketTimeout(timeoutMs + 5000);
71 return builder.build();
74 public CloseableHttpClient getClient() {
75 return getClient(false);
78 public CloseableHttpClient getClient(boolean useHttps) {
79 return HttpClientBuilder.create().build();
82 public String formatHostString(String host) {
83 return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
86 public String formatHostString(String host, boolean useHttps) {
87 // Trim trailing slash
88 String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
90 boolean hasProto = out.startsWith("http");
91 boolean hasPort = out.contains(":");
95 out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
100 out = String.format("%s:%d", out, (useHttps) ? 3905 : 3904);