MrTopicConnection.java: Fixed sonar issue
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / client / MrTopicConnection.java
index e1ea955..492037c 100644 (file)
@@ -33,7 +33,6 @@ import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLException;
 
 import org.apache.commons.codec.binary.Base64;
-import org.apache.log4j.Logger;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.MR_Cluster;
@@ -47,13 +46,14 @@ public class MrTopicConnection extends BaseLoggingClass  {
        
        private  String mmProvCred; 
        private String unit_test;
-       
+       private boolean useAAF;
 
 
        public MrTopicConnection(String user, String pwd ) {
                mmProvCred = new String( user + ":" + pwd );
                DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
         unit_test = p.getProperty( "UnitTest", "No" );
+       useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
        }
        
        public boolean makeTopicConnection( MR_Cluster cluster, String topic, String overrideFqdn ) {
@@ -80,7 +80,7 @@ public class MrTopicConnection extends BaseLoggingClass  {
                        return(true);
                } catch (Exception e) {
             logger.error("Unexpected error during openConnection of " + pURL );
-            e.printStackTrace();
+            logger.error("Error", e);;
             return(false);
         }
 
@@ -96,7 +96,7 @@ public class MrTopicConnection extends BaseLoggingClass  {
                        return(true);
                } catch (Exception e) {
             logger.error("Unexpected error during openConnection of " + pURL );
-            e.printStackTrace();
+            logger.error("error", e);
             return(false);
         }
 
@@ -126,8 +126,10 @@ public class MrTopicConnection extends BaseLoggingClass  {
                try {
                        byte[] postData = postMessage.getBytes();
                        logger.info( "post fields=" + postMessage );
-                       uc.setRequestProperty("Authorization", auth);
-                       logger.info( "Authenticating with " + auth );
+                       if ( useAAF ) {
+                               uc.setRequestProperty("Authorization", auth);
+                               logger.info( "Authenticating with " + auth );
+                       }
                        uc.setRequestMethod("POST");
                        uc.setRequestProperty("Content-Type", "application/json");
                        uc.setRequestProperty( "charset", "utf-8");
@@ -144,13 +146,10 @@ public class MrTopicConnection extends BaseLoggingClass  {
 
             } catch (ProtocolException 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) {
-                 }
+               callSetDoOutputOnError();
+                 
             }  catch ( SSLException se ) {
+               logger.error("Error", se);
                        response.setCode(500);
                        response.setMessage( se.getMessage());
                        return response;
@@ -191,16 +190,28 @@ public class MrTopicConnection extends BaseLoggingClass  {
                                response.setCode(500);
                                response.setMessage( "Unable to read response");
                                logger.warn( response.getMessage() );
-               e.printStackTrace();
+               logger.error("Error", e);
                        }
         }
                finally {
                        try {
                                uc.disconnect();
-                       } catch ( Exception e ) {}
+                       } catch ( Exception e ) {
+                               logger.error("Error", e);
+                       }
                }
                return response;
 
        }
+       
+       public void callSetDoOutputOnError() {
+               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);
+        }
+       }
 
 }