2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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=========================================================
 
  22 package org.openecomp.appc.adapter.dmaap;
 
  26 import org.apache.commons.codec.binary.Base64;
 
  27 import org.apache.http.client.config.RequestConfig;
 
  28 import org.apache.http.client.config.RequestConfig.Builder;
 
  29 import org.apache.http.client.methods.HttpGet;
 
  30 import org.apache.http.client.methods.HttpPost;
 
  31 import org.apache.http.impl.client.CloseableHttpClient;
 
  32 import org.apache.http.impl.client.HttpClientBuilder;
 
  34 public class CommonHttpClient {
 
  36     public static final int HTTPS_PORT = 3905;
 
  38     private String AUTH_STR;
 
  40     protected void setBasicAuth(String username, String password) {
 
  41         if (username != null && password != null) {
 
  42             String plain = String.format("%s:%s", username, password);
 
  43             AUTH_STR = Base64.encodeBase64String(plain.getBytes());
 
  49     public HttpGet getReq(URI uri, int timeoutMs) throws Exception {
 
  50         HttpGet out = (uri == null) ? new HttpGet() : new HttpGet(uri);
 
  51         if (AUTH_STR != null) {
 
  52                 out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
 
  54         out.setConfig(getConfig(timeoutMs));
 
  58     public HttpPost postReq(String url) throws Exception {
 
  59         HttpPost out = (url == null) ? new HttpPost() : new HttpPost(url);
 
  60         if (AUTH_STR != null) {
 
  61                 out.setHeader("Authorization", String.format("Basic %s", AUTH_STR));
 
  63         out.setConfig(getConfig(0));
 
  67     private RequestConfig getConfig(int timeoutMs) {
 
  68         Builder builder = RequestConfig.custom();
 
  69         builder.setSocketTimeout(timeoutMs + 5000);
 
  70         return builder.build();
 
  73     public CloseableHttpClient getClient() {
 
  74         return getClient(false);
 
  77     public CloseableHttpClient getClient(boolean useHttps) {
 
  78         return HttpClientBuilder.create().build();
 
  81     public String formatHostString(String host) {
 
  82         return formatHostString(host, host.contains(String.valueOf(HTTPS_PORT)));
 
  85     public String formatHostString(String host, boolean useHttps) {
 
  86         // Trim trailing slash
 
  87         String out = host.endsWith("/") ? host.substring(0, host.length() - 1) : host;
 
  89         boolean hasProto = out.startsWith("http");
 
  90         boolean hasPort = out.contains(":");
 
  94             out = String.format("%s%s", (useHttps) ? "https://" : "http://", out);
 
  99             out = String.format("%s:%d", out, (useHttps) ? 3905 : 3904);