X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Faaf%2FAafConnection.java;h=934e541ed513c5fd8da0cae7e9ff7a54fbada330;hb=03b16f6df6fe5545a4ed219916c77dd696694ea2;hp=01d103d0eb85c1fb6dd0097d59f474b352689ea2;hpb=503da874ce876dbe463bcc1a03f63ea0f48fe650;p=dmaap%2Fdbcapi.git diff --git a/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java b/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java index 01d103d..934e541 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java +++ b/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java @@ -3,6 +3,8 @@ * org.onap.dmaap * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2019 IBM. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,25 +37,27 @@ import java.net.UnknownHostException; import java.net.ConnectException; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import org.apache.commons.codec.binary.Base64; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum; -import org.onap.dmaap.dbcapi.service.DmaapService; import org.onap.dmaap.dbcapi.util.DmaapConfig; public class AafConnection extends BaseLoggingClass { - - + + private String aafCred; private String unit_test; - + private HttpsURLConnection uc; @@ -63,23 +67,23 @@ public class AafConnection extends BaseLoggingClass { unit_test = p.getProperty( "UnitTest", "No" ); } - + private boolean makeConnection( String pURL ) { - + try { URL u = new URL( pURL ); uc = (HttpsURLConnection) u.openConnection(); uc.setInstanceFollowRedirects(false); logger.info( "successful connect to " + pURL ); return(true); - } catch ( UnknownHostException uhe ) { + } catch ( UnknownHostException uhe ) { errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() ); - uhe.printStackTrace(); + logger.error("Error", uhe); return(false); } catch (Exception e) { - errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage() ); - e.printStackTrace(); + logger.error("Error", e); + errorLogger.error(DmaapbcLogMessageEnum.HTTP_CONNECTION_ERROR, pURL, e.getMessage()); return(false); } @@ -94,9 +98,9 @@ public class AafConnection extends BaseLoggingClass { sb.append( line ); } } catch (IOException ex ) { - errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION, ex.getMessage()); + errorLogger.error( DmaapbcLogMessageEnum.IO_EXCEPTION + ex.getMessage(),ex); } - + return sb.toString(); } @@ -107,11 +111,11 @@ public class AafConnection extends BaseLoggingClass { String auth = "Basic " + Base64.encodeBase64String(aafCred.getBytes()); int rc = -1; - + if ( ! makeConnection( pURL ) ) { return rc; }; - + byte[] postData = obj.getBytes(); //logger.info( "post fields=" + postData ); //byte isn't very readable @@ -128,6 +132,10 @@ public class AafConnection extends BaseLoggingClass { uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + uc.setSSLSocketFactory(sc.getSocketFactory()); OutputStream os = null; @@ -137,21 +145,26 @@ public class AafConnection extends BaseLoggingClass { os.write( postData ); } catch (ProtocolException pe) { + logger.error("Error", pe); // Rcvd error instead of 100-Continue try { // work around glitch in Java 1.7.0.21 and likely others // without this, Java will connect multiple times to the server to run the same request uc.setDoOutput(false); } catch (Exception e) { + logger.error("Error", e); } } catch ( SSLHandshakeException she ) { + logger.error("Error", she); errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL); } catch ( UnknownHostException uhe ) { + logger.error("Error", uhe); errorLogger.error(DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION, pURL, uhe.getMessage() ); rc = 500; return rc; } catch ( ConnectException ce ) { - if ( unit_test.equals( "Yes" ) ) { + logger.error("Error", ce); + if ( "Yes".equals(unit_test) ) { rc = 201; return rc; } @@ -162,6 +175,7 @@ public class AafConnection extends BaseLoggingClass { try { rc = uc.getResponseCode(); } catch ( SSLHandshakeException she ) { + logger.error("Error", she); errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL); rc = 500; return rc; @@ -190,16 +204,16 @@ public class AafConnection extends BaseLoggingClass { } } catch (Exception e) { - System.err.println("Unable to read response " ); - e.printStackTrace(); + logger.error("Unable to read response "); + logger.error("Error", e); } finally { try { uc.disconnect(); - } catch ( Exception e ) {} - } - //return responseBody; - + } catch ( Exception e ) { + logger.error("Error", e); + } + } return rc; } @@ -239,19 +253,22 @@ public class AafConnection extends BaseLoggingClass { os.write( postData ); } catch (ProtocolException pe) { + logger.error("Error", pe); // Rcvd error instead of 100-Continue try { // work around glitch in Java 1.7.0.21 and likely others // without this, Java will connect multiple times to the server to run the same request uc.setDoOutput(false); } catch (Exception e) { + logger.error("Error", e); } } catch ( SSLHandshakeException she ) { - errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL); + errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR +"For:- "+pURL,she); } try { rc = uc.getResponseCode(); } catch ( SSLHandshakeException she ) { + logger.error("Error", she); errorLogger.error( DmaapbcLogMessageEnum.SSL_HANDSHAKE_ERROR, pURL); rc = 500; return rc; @@ -280,14 +297,33 @@ public class AafConnection extends BaseLoggingClass { } } catch (Exception e) { - System.err.println("Unable to read response " ); - e.printStackTrace(); - } - //return responseBody; - + logger.error("Unable to read response "); + logger.error("Error", e); + } return rc; } + + private TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() + { + return null; + } + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) + { + //No need to implement. + } + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) + { + //No need to implement. + } + } + }; }