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.sdc.listener;
 
  27 import java.io.IOException;
 
  28 import java.io.UnsupportedEncodingException;
 
  29 import java.net.Socket;
 
  31 import java.net.UnknownHostException;
 
  32 import java.security.KeyManagementException;
 
  33 import java.security.KeyStore;
 
  34 import java.security.KeyStoreException;
 
  35 import java.security.NoSuchAlgorithmException;
 
  36 import java.security.UnrecoverableKeyException;
 
  37 import java.security.cert.CertificateException;
 
  38 import java.security.cert.X509Certificate;
 
  40 import java.util.Map.Entry;
 
  42 import javax.net.ssl.SSLContext;
 
  43 import javax.net.ssl.TrustManager;
 
  44 import javax.net.ssl.X509TrustManager;
 
  46 import org.apache.commons.codec.binary.Base64;
 
  47 import org.apache.commons.io.IOUtils;
 
  48 import org.apache.http.HttpResponse;
 
  49 import org.apache.http.HttpVersion;
 
  50 import org.apache.http.client.HttpClient;
 
  51 import org.apache.http.client.methods.HttpPost;
 
  52 import org.apache.http.conn.ClientConnectionManager;
 
  53 import org.apache.http.conn.scheme.PlainSocketFactory;
 
  54 import org.apache.http.conn.scheme.Scheme;
 
  55 import org.apache.http.conn.scheme.SchemeRegistry;
 
  56 import org.apache.http.conn.ssl.SSLSocketFactory;
 
  57 import org.apache.http.entity.StringEntity;
 
  58 import org.apache.http.impl.client.DefaultHttpClient;
 
  59 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
 
  60 import org.apache.http.params.BasicHttpParams;
 
  61 import org.apache.http.params.HttpParams;
 
  62 import org.apache.http.params.HttpProtocolParams;
 
  63 import org.apache.http.protocol.HTTP;
 
  64 import org.onap.appc.exceptions.APPCException;
 
  65 import com.att.eelf.configuration.EELFLogger;
 
  66 import com.att.eelf.configuration.EELFManager;
 
  68 public class ProviderOperations {
 
  70     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(ProviderOperations.class);
 
  72     private static String basic_auth;
 
  74     public static ProviderResponse post(URL url, String json, Map<String, String> adtl_headers) throws APPCException {
 
  76             throw new APPCException("Provided message was null");
 
  81             post = new HttpPost(url.toExternalForm());
 
  82             post.setHeader("Content-Type", "application/json");
 
  83             post.setHeader("Accept", "application/json");
 
  86             if (basic_auth != null) {
 
  87                 post.setHeader("Authorization", "Basic " + basic_auth);
 
  90             if (adtl_headers != null) {
 
  91                 for (Entry<String, String> header : adtl_headers.entrySet()) {
 
  92                     post.setHeader(header.getKey(), header.getValue());
 
  96             StringEntity entity = new StringEntity(json);
 
  97             entity.setContentType("application/json");
 
  98             post.setEntity(new StringEntity(json));
 
  99         } catch (UnsupportedEncodingException e) {
 
 100             throw new APPCException(e);
 
 103         HttpClient client = getHttpClient(url);
 
 106         String respBody = null;
 
 108             HttpResponse response = client.execute(post);
 
 109             httpCode = response.getStatusLine().getStatusCode();
 
 110             respBody = IOUtils.toString(response.getEntity().getContent());
 
 111             return new ProviderResponse(httpCode, respBody);
 
 112         } catch (IOException e) {
 
 113             throw new APPCException(e);
 
 118      * Sets the basic authentication header for the given user and password. If either entry is null then set basic auth
 
 122      *            The user with optional domain name (for AAF)
 
 124      *            The password for the user
 
 125      * @return The new value of the basic auth string that will be used in the request headers
 
 127     public static String setAuthentication(String user, String password) {
 
 128         if (user != null && password != null) {
 
 129             String authStr = user + ":" + password;
 
 130             basic_auth = new String(Base64.encodeBase64(authStr.getBytes()));
 
 137     @SuppressWarnings("deprecation")
 
 138     private static HttpClient getHttpClient(URL url) throws APPCException {
 
 140         if (url.getProtocol().equals("https")) {
 
 142                 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
 
 143                 trustStore.load(null, null);
 
 144                 MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
 
 145                 sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
 
 147                 HttpParams params = new BasicHttpParams();
 
 148                 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
 
 149                 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
 
 151                 SchemeRegistry registry = new SchemeRegistry();
 
 152                 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
 
 153                 registry.register(new Scheme("https", sf, 443));
 
 154                 registry.register(new Scheme("https", sf, 8443));
 
 155                 registry.register(new Scheme("http", sf, 8181));
 
 157                 ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
 
 158                 client = new DefaultHttpClient(ccm, params);
 
 159             } catch (Exception e) {
 
 160                 client = new DefaultHttpClient();
 
 162         } else if (url.getProtocol().equals("http")) {
 
 163             client = new DefaultHttpClient();
 
 165             throw new APPCException(
 
 166                 "The provider.topology.url property is invalid. The url did not start with http[s]");
 
 171     @SuppressWarnings("deprecation")
 
 172     public static class MySSLSocketFactory extends SSLSocketFactory {
 
 173         private SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
 
 175         public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException,
 
 176                         KeyStoreException, UnrecoverableKeyException {
 
 179             TrustManager tm = new X509TrustManager() {
 
 181                 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 
 185                 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 
 189                 public X509Certificate[] getAcceptedIssuers() {
 
 194             sslContext.init(null, new TrustManager[] {
 
 200         public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
 
 201             throws IOException, UnknownHostException {
 
 202             return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
 
 206         public Socket createSocket() throws IOException {
 
 207             return sslContext.getSocketFactory().createSocket();