aboutsummaryrefslogtreecommitdiffstats
path: root/appc-common
diff options
context:
space:
mode:
authorSkip Wonnell <skip@att.com>2018-01-10 18:53:54 -0600
committerPatrick Brady <pb071s@att.com>2018-01-11 22:28:44 +0000
commit6be4c0af1971d3e000d279df2d9bc00517cb4cae (patch)
tree5a36a59c3f4051d618947284c824246026d22769 /appc-common
parent45a2f9496998236dc9b3c6ebcb6008ef11f80fc5 (diff)
Sonar blocker issue resolution
Sonar issue resolutions for RestClientInvoker.java Resolve Blocker issue: https://sonar.onap.org/issues/search#issues=AWBzdRXQ-08if2a6vsl1 And other sonar findings: https://sonar.onap.org/issues/search#issues=AWBzdRXP-08if2a6vslw https://sonar.onap.org/issues/search#issues=AWBzdRXP-08if2a6vslx https://sonar.onap.org/issues/search#issues=AWBzdRXP-08if2a6vsly https://sonar.onap.org/issues/search#issues=AWBzdRXQ-08if2a6vslz https://sonar.onap.org/issues/search#issues=AWBzdRXQ-08if2a6vsl0 https://sonar.onap.org/issues/search#issues=AWBzdRXP-08if2a6vslv https://sonar.onap.org/issues/search#issues=AWBzdRXQ-08if2a6vsl2 Change-Id: Ic9d834f223db49bf0c02e51f334b3f93ace263cd Issue-ID: APPC-387 Signed-off-by: Skip Wonnell <skip@att.com>
Diffstat (limited to 'appc-common')
-rw-r--r--appc-common/src/main/java/org/onap/appc/rest/client/RestClientInvoker.java154
1 files changed, 78 insertions, 76 deletions
diff --git a/appc-common/src/main/java/org/onap/appc/rest/client/RestClientInvoker.java b/appc-common/src/main/java/org/onap/appc/rest/client/RestClientInvoker.java
index f2bec45ca..90c97eb42 100644
--- a/appc-common/src/main/java/org/onap/appc/rest/client/RestClientInvoker.java
+++ b/appc-common/src/main/java/org/onap/appc/rest/client/RestClientInvoker.java
@@ -9,23 +9,36 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
+ *
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
* ============LICENSE_END=========================================================
*/
package org.onap.appc.rest.client;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.Socket;
+import java.net.URL;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
@@ -40,6 +53,7 @@ import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
@@ -47,38 +61,29 @@ import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.onap.appc.exceptions.APPCException;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.MalformedURLException;
-import java.net.Socket;
-import java.net.URL;
-import java.security.*;
-import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-
+@SuppressWarnings("deprecation")
public class RestClientInvoker {
- public RestClientInvoker(URL url){
- this.url=url;
- }
- private RestClientInvoker(){}
-
- private URL url=null;
- private String basicAuth=null;
-
private static final EELFLogger LOG = EELFManager.getInstance().getLogger(RestClientInvoker.class);
- private static final String OPERATION_HTTPS="https";
- private static final String OPERATION_APPLICATION_JSON= " application/json";
+ private static final String OPERATION_HTTPS = "https";
+ private static final String OPERATION_APPLICATION_JSON = " application/json";
+ private static final String BASIC = "Basic ";
+
+ private URL url = null;
+ private String basicAuth = null;
+ public RestClientInvoker(URL url) {
+ this.url = url;
+ }
/**
- * Sets the basic authentication header for the given user and password. If either entry is null then does not set basic auth
+ * Sets the basic authentication header for the given user and password. If either entry is null
+ * then does not set basic auth
*
- * @param user The user with optional domain name (for AAF)
+ * @param user The user with optional domain name (for AAF)
* @param password The password for the user
*/
public void setAuthentication(String user, String password) {
@@ -88,7 +93,7 @@ public class RestClientInvoker {
}
}
- public HttpResponse doPost (String path , String body) throws APPCException {
+ public HttpResponse doPost(String path, String body) throws APPCException {
HttpPost post;
try {
@@ -99,7 +104,7 @@ public class RestClientInvoker {
post.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
if (basicAuth != null) {
- post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
+ post.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
}
StringEntity entity = new StringEntity(body);
@@ -119,12 +124,13 @@ public class RestClientInvoker {
/**
* This is Generic method that can be used to perform REST Put operation
+ *
* @param path - path for put
* @param body - payload for put action which will be sent as request body.
* @return - HttpResponse object which is returned from put REST call.
* @throws APPCException when error occurs
*/
- public HttpResponse doPut (String path, String body) throws APPCException {
+ public HttpResponse doPut(String path, String body) throws APPCException {
HttpPut put;
try {
URL putUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
@@ -133,7 +139,7 @@ public class RestClientInvoker {
put.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
if (basicAuth != null) {
- put.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
+ put.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
}
StringEntity entity = new StringEntity(body);
@@ -152,7 +158,7 @@ public class RestClientInvoker {
}
}
- public HttpResponse doGet (String path) throws APPCException {
+ public HttpResponse doGet(String path) throws APPCException {
HttpGet get;
try {
URL getUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
@@ -161,63 +167,62 @@ public class RestClientInvoker {
get.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
if (basicAuth != null) {
- get.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
+ get.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
}
} catch (Exception e) {
throw new APPCException(e);
}
- HttpClient client = getHttpClient();
- try {
+
+ try (CloseableHttpClient client = getHttpClient()) {
return client.execute(get);
} catch (IOException e) {
throw new APPCException(e);
}
}
- @SuppressWarnings("deprecation")
- private HttpClient getHttpClient() throws APPCException {
- HttpClient client;
+ private CloseableHttpClient getHttpClient() throws APPCException {
switch (url.getProtocol()) {
case OPERATION_HTTPS:
- try {
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- trustStore.load(null, null);
- MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
- sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
-
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
-
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- registry.register(new Scheme(OPERATION_HTTPS, sf, 443));
- registry.register(new Scheme(OPERATION_HTTPS, sf, 8443));
- registry.register(new Scheme("http", sf, 8181));
-
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
- client = new DefaultHttpClient(ccm, params);
- } catch (Exception e) {
- LOG.error("Error creating HTTP Client. Creating default client.", e);
- client = new DefaultHttpClient();
- }
- break;
+ return createHttpsClient();
case "http":
- client = new DefaultHttpClient();
- break;
+ return new DefaultHttpClient();
default:
- throw new APPCException(
- "The url did not start with http[s]");
+ throw new APPCException("The url did not start with http[s]");
+ }
+ }
+
+
+ private CloseableHttpClient createHttpsClient() {
+ try {
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ trustStore.load(null, null);
+ MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
+ sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+
+ HttpParams params = new BasicHttpParams();
+ HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
+ HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
+
+ SchemeRegistry registry = new SchemeRegistry();
+ registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
+ registry.register(new Scheme(OPERATION_HTTPS, sf, 443));
+ registry.register(new Scheme(OPERATION_HTTPS, sf, 8443));
+ registry.register(new Scheme("http", sf, 8181));
+
+ ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
+ return new DefaultHttpClient(ccm, params);
+ } catch (Exception e) {
+ LOG.error("Error creating HTTPs Client. Creating default client.", e);
+ return new DefaultHttpClient();
}
- return client;
}
- @SuppressWarnings("deprecation")
+
private static class MySSLSocketFactory extends SSLSocketFactory {
private SSLContext sslContext = SSLContext.getInstance("TLS");
- private MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException,
- KeyStoreException, UnrecoverableKeyException {
+ private MySSLSocketFactory(KeyStore truststore)
+ throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
@@ -237,14 +242,11 @@ public class RestClientInvoker {
}
};
- sslContext.init(null, new TrustManager[]{
- tm
- }, null);
+ sslContext.init(null, new TrustManager[] {tm}, null);
}
@Override
- public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
- throws IOException {
+ public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}