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.rest.client;
27 import java.io.IOException;
28 import java.io.UnsupportedEncodingException;
29 import java.net.MalformedURLException;
30 import java.net.Socket;
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;
39 import javax.net.ssl.SSLContext;
40 import javax.net.ssl.TrustManager;
41 import javax.net.ssl.X509TrustManager;
42 import org.apache.commons.codec.binary.Base64;
43 import org.apache.http.HttpHeaders;
44 import org.apache.http.HttpResponse;
45 import org.apache.http.HttpVersion;
46 import org.apache.http.client.HttpClient;
47 import org.apache.http.client.methods.HttpGet;
48 import org.apache.http.client.methods.HttpPost;
49 import org.apache.http.client.methods.HttpPut;
50 import org.apache.http.conn.ClientConnectionManager;
51 import org.apache.http.conn.scheme.PlainSocketFactory;
52 import org.apache.http.conn.scheme.Scheme;
53 import org.apache.http.conn.scheme.SchemeRegistry;
54 import org.apache.http.conn.ssl.SSLSocketFactory;
55 import org.apache.http.entity.StringEntity;
56 import org.apache.http.impl.client.CloseableHttpClient;
57 import org.apache.http.impl.client.DefaultHttpClient;
58 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
59 import org.apache.http.params.BasicHttpParams;
60 import org.apache.http.params.HttpParams;
61 import org.apache.http.params.HttpProtocolParams;
62 import org.apache.http.protocol.HTTP;
63 import org.onap.appc.exceptions.APPCException;
64 import com.att.eelf.configuration.EELFLogger;
65 import com.att.eelf.configuration.EELFManager;
67 @SuppressWarnings("deprecation")
68 public class RestClientInvoker {
70 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(RestClientInvoker.class);
71 private static final String OPERATION_HTTPS = "https";
72 private static final String OPERATION_APPLICATION_JSON = " application/json";
73 private static final String BASIC = "Basic ";
75 private URL url = null;
76 private String basicAuth = null;
78 public RestClientInvoker(URL url) {
83 * Sets the basic authentication header for the given user and password. If either entry is null
84 * then does not set basic auth
86 * @param user The user with optional domain name (for AAF)
87 * @param password The password for the user
89 public void setAuthentication(String user, String password) {
90 if (user != null && password != null) {
91 String authStr = user + ":" + password;
92 basicAuth = new String(Base64.encodeBase64(authStr.getBytes()));
96 public HttpResponse doPost(String path, String body) throws APPCException {
101 URL postUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
102 post = new HttpPost(postUrl.toExternalForm());
103 post.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
104 post.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
106 if (basicAuth != null) {
107 post.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
110 StringEntity entity = new StringEntity(body);
111 entity.setContentType(OPERATION_APPLICATION_JSON);
112 post.setEntity(new StringEntity(body));
113 } catch (MalformedURLException | UnsupportedEncodingException e) {
114 throw new APPCException(e);
116 HttpClient client = getHttpClient();
119 return client.execute(post);
120 } catch (IOException e) {
121 throw new APPCException(e);
126 * This is Generic method that can be used to perform REST Put operation
128 * @param path - path for put
129 * @param body - payload for put action which will be sent as request body.
130 * @return - HttpResponse object which is returned from put REST call.
131 * @throws APPCException when error occurs
133 public HttpResponse doPut(String path, String body) throws APPCException {
136 URL putUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
137 put = new HttpPut(putUrl.toExternalForm());
138 put.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
139 put.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
141 if (basicAuth != null) {
142 put.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
145 StringEntity entity = new StringEntity(body);
146 entity.setContentType(OPERATION_APPLICATION_JSON);
147 put.setEntity(new StringEntity(body));
148 } catch (UnsupportedEncodingException | MalformedURLException e) {
149 throw new APPCException(e);
152 HttpClient client = getHttpClient();
155 return client.execute(put);
156 } catch (IOException e) {
157 throw new APPCException(e);
161 public HttpResponse doGet(String path) throws APPCException {
164 URL getUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
165 get = new HttpGet(getUrl.toExternalForm());
166 get.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
167 get.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
169 if (basicAuth != null) {
170 get.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
173 } catch (Exception e) {
174 throw new APPCException(e);
177 try (CloseableHttpClient client = getHttpClient()) {
178 return client.execute(get);
179 } catch (IOException e) {
180 throw new APPCException(e);
184 private CloseableHttpClient getHttpClient() throws APPCException {
185 switch (url.getProtocol()) {
186 case OPERATION_HTTPS:
187 return createHttpsClient();
189 return new DefaultHttpClient();
191 throw new APPCException("The url did not start with http[s]");
196 private CloseableHttpClient createHttpsClient() {
198 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
199 trustStore.load(null, null);
200 MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
201 sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
203 HttpParams params = new BasicHttpParams();
204 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
205 HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
207 SchemeRegistry registry = new SchemeRegistry();
208 registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
209 registry.register(new Scheme(OPERATION_HTTPS, sf, 443));
210 registry.register(new Scheme(OPERATION_HTTPS, sf, 8443));
211 registry.register(new Scheme("http", sf, 8181));
213 ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
214 return new DefaultHttpClient(ccm, params);
215 } catch (Exception e) {
216 LOG.error("Error creating HTTPs Client. Creating default client.", e);
217 return new DefaultHttpClient();
221 private static class MySSLSocketFactory extends SSLSocketFactory {
222 private SSLContext sslContext = SSLContext.getInstance("TLS");
224 private MySSLSocketFactory(KeyStore truststore)
225 throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
228 TrustManager tm = new X509TrustManager() {
230 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
231 LOG.debug("Inside checkClientTrusted");
235 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
236 LOG.debug("Inside checkServerTrusted");
240 public X509Certificate[] getAcceptedIssuers() {
241 return new X509Certificate[1];
245 sslContext.init(null, new TrustManager[] {tm}, null);
249 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
250 return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
254 public Socket createSocket() throws IOException {
255 return sslContext.getSocketFactory().createSocket();