Remove major and minor code smells in dr-prov
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / BaseServlet.java
index deb78b1..7475b6b 100755 (executable)
@@ -132,6 +132,32 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
     private static final String DEFAULT_PROVSRVR_NAME = "dmaap-dr-prov";
     private static final String STATIC_ROUTING_NODES = ""; //Adding new param for static Routing - Rally:US664862-1610
 
+    //Common Errors
+    public static final String MISSING_ON_BEHALF = "Missing X-DMAAP-DR-ON-BEHALF-OF header.";
+    public static final String MISSING_FEED = "Missing or bad feed number.";
+    public static final String POLICY_ENGINE = "Policy Engine disallows access.";
+    public static final String UNAUTHORIZED = "Unauthorized.";
+    public static final String BAD_SUB = "Missing or bad subscription number.";
+    public static final String BAD_JSON = "Badly formed JSON";
+    public static final String BAD_URL = "Bad URL.";
+
+    public static final String API = "/api/";
+    public static final String LOGS = "/logs/";
+    public static final String TEXT_CT = "text/plain";
+    public static final String INGRESS = "/ingress/";
+    public static final String EGRESS = "/egress/";
+    public static final String NETWORK = "/network/";
+    public static final String GROUPID = "groupid";
+    public static final String FEEDID = "feedid";
+    public static final String FEEDIDS = "feedids";
+    public static final String SUBID = "subid";
+    public static final String EVENT_TYPE = "eventType";
+    public static final String OUTPUT_TYPE = "output_type";
+    public static final String START_TIME = "start_time";
+    public static final String END_TIME = "end_time";
+    public static final String REASON_SQL = "reasonSQL";
+
+
     /**
      * A boolean to trigger one time "provisioning changed" event on startup
      */
@@ -285,9 +311,8 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
         try {
             thishost = InetAddress.getLocalHost();
             loopback = InetAddress.getLoopbackAddress();
-            //checkHttpsRelaxation(); //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
         } catch (UnknownHostException e) {
-            // ignore
+            intlogger.info("BaseServlet.init: " + e.getMessage(), e);
         }
     }
 
@@ -380,7 +405,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 return "Unauthorized address: " + remote;
             }
         } catch (UnknownHostException e) {
-            intlogger.error("PROV0051 BaseServlet.isAuthorizedForProvisioning: ", e.getMessage());
+            intlogger.error("PROV0051 BaseServlet.isAuthorizedForProvisioning: " + e.getMessage(), e);
             return "Unauthorized address: " + remote;
         }
         // Does remote have a valid certificate?
@@ -429,7 +454,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 return true;
             }
         } catch (UnknownHostException e) {
-            intlogger.error("PROV0052 BaseServlet.isAuthorizedForInternal: ", e.getMessage());
+            intlogger.error("PROV0052 BaseServlet.isAuthorizedForInternal: " + e.getMessage(), e);
         }
         return false;
     }
@@ -473,7 +498,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 }
             }
         } catch (UnknownHostException e) {
-            intlogger.error("PROV0053 BaseServlet.addressMatchesNetwork: ", e.getMessage());
+            intlogger.error("PROV0053 BaseServlet.addressMatchesNetwork: " + e.getMessage(), e);
             return false;
         }
         return true;
@@ -519,7 +544,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             thisPod = InetAddress.getLocalHost().getHostName();
         } catch (UnknownHostException e) {
             thisPod = "";
-            intlogger.warn("PROV0014 Cannot determine the name of this provisioning server.");
+            intlogger.warn("PROV0014 Cannot determine the name of this provisioning server.", e);
         }
 
         // Normalize the nodes, and fill in nodeAddresses
@@ -530,7 +555,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 intlogger.debug("PROV0003 DNS lookup: " + nodes[i] + " => " + na[i].toString());
             } catch (UnknownHostException e) {
                 na[i] = null;
-                intlogger.warn("PROV0004 Cannot lookup " + nodes[i] + ": " + e.getMessage());
+                intlogger.warn("PROV0004 Cannot lookup " + nodes[i] + ": " + e.getMessage(), e);
             }
         }
 
@@ -558,7 +583,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 intlogger.debug("PROV0003 DNS lookup: " + pods[i] + " => " + na[i].toString());
             } catch (UnknownHostException e) {
                 na[i] = null;
-                intlogger.warn("PROV0004 Cannot lookup " + pods[i] + ": " + e.getMessage());
+                intlogger.warn("PROV0004 Cannot lookup " + pods[i] + ": " + e.getMessage(), e);
             }
         }
         podAddresses = na;
@@ -584,32 +609,12 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(MAILCONFIG_FILE)) {
                 mailprops.load(inStream);
             } catch (IOException e) {
-                intlogger.error("PROV9003 Opening properties: " + e.getMessage());
+                intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e);
                 System.exit(1);
             }
         }
     }
 
-    /**
-     * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047. Check if HTTPS Relexaction is enabled
-     *
-     * @author vs215k
-     **/
-    private void checkHttpsRelaxation() {
-        if (!mailSendFlag) {
-            Properties p = (new DB()).getProperties();
-            intlogger.info("HTTPS relaxation: " + p.get("org.onap.dmaap.datarouter.provserver.https.relaxation"));
-
-            if (p.get("org.onap.dmaap.datarouter.provserver.https.relaxation").equals("true")) {
-                try {
-                    notifyPSTeam(p.get("org.onap.dmaap.datarouter.provserver.https.relax.notify").toString());
-                } catch (Exception e) {
-                    intlogger.warn("Exception: " + e.getMessage());
-                }
-            }
-            mailSendFlag = true;
-        }
-    }
 
     /**
      * Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.
@@ -617,7 +622,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
      * @param email - list of email ids to notify if HTTP relexcation is enabled.
      * @author vs215k
      **/
-    private void notifyPSTeam(String email) throws Exception {
+    private void notifyPSTeam(String email) {
         loadMailProperties(); //Load HTTPS Relex mail properties.
         String[] emails = email.split(Pattern.quote("|"));
 
@@ -646,14 +651,16 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             mp.addBodyPart(htmlPart);
             msg.setContent(mp);
 
-            System.out.println(mailprops.get("com.att.dmaap.datarouter.mail.body").toString()
+            intlogger.info(mailprops.get("com.att.dmaap.datarouter.mail.body").toString()
                     .replace("[SERVER]", InetAddress.getLocalHost().getHostName()));
 
             Transport.send(msg);
             intlogger.info("HTTPS relaxation mail is sent to - : " + email);
 
         } catch (MessagingException e) {
-            intlogger.error("Invalid email address, unable to send https relaxation mail to - : " + email);
+            intlogger.error("Invalid email address, unable to send https relaxation mail to - : " + email, e);
+        } catch (UnknownHostException uhe) {
+            intlogger.error("UnknownHostException", uhe);
         }
     }
 
@@ -748,7 +755,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             rv = bean.doInsert(conn);
         } catch (SQLException e) {
             rv = false;
-            intlogger.warn("PROV0005 doInsert: " + e.getMessage());
+            intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
         } finally {
             if (conn != null) {
                 db.release(conn);
@@ -772,7 +779,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             rv = bean.doUpdate(conn);
         } catch (SQLException e) {
             rv = false;
-            intlogger.warn("PROV0006 doUpdate: " + e.getMessage());
+            intlogger.warn("PROV0006 doUpdate: " + e.getMessage(), e);
         } finally {
             if (conn != null) {
                 db.release(conn);
@@ -796,7 +803,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             rv = bean.doDelete(conn);
         } catch (SQLException e) {
             rv = false;
-            intlogger.warn("PROV0007 doDelete: " + e.getMessage());
+            intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
         } finally {
             if (conn != null) {
                 db.release(conn);
@@ -807,7 +814,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
 
     private static boolean getBoolean(Map<String, String> map, String name) {
         String s = map.get(name);
-        return (s != null) && s.equalsIgnoreCase("true");
+        return (s != null) && "true".equalsIgnoreCase(s);
     }
 
     private static String getString(Map<String, String> map, String name, String dflt) {
@@ -959,7 +966,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                     return true;
                 }
             } catch (JSONException e) {
-                intlogger.error("JSONException: " + e.getMessage());
+                intlogger.error("JSONException: " + e.getMessage(), e);
             }
         }
         return false;
@@ -1048,7 +1055,7 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
             MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
             MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
         } catch (Exception e) {
-            intlogger.error("Exception: " + e.getMessage());
+            intlogger.error("Exception: " + e.getMessage(), e);
         }
 
     }
@@ -1086,12 +1093,12 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 default:
                     action = "*";
             }
-            if (aafInstance == null || aafInstance.equals("")) {
+            if (aafInstance == null || "".equals(aafInstance)) {
                 aafInstance = props.getProperty(AAF_INSTANCE, "org.onap.dmaap-dr.NoInstanceDefined");
             }
             return type + "|" + aafInstance + "|" + action;
         } catch (Exception e) {
-            intlogger.error("PROV7005 BaseServlet.getFeedPermission: ", e.getMessage());
+            intlogger.error("PROV7005 BaseServlet.getFeedPermission: " + e.getMessage(), e);
         }
         return null;
     }
@@ -1134,12 +1141,12 @@ public class BaseServlet extends HttpServlet implements ProvDataProvider {
                 default:
                     action = "*";
             }
-            if (aafInstance == null || aafInstance.equals("")) {
+            if (aafInstance == null || "".equals(aafInstance)) {
                 aafInstance = props.getProperty(AAF_INSTANCE, "org.onap.dmaap-dr.NoInstanceDefined");
             }
             return type + "|" + aafInstance + "|" + action;
         } catch (Exception e) {
-            intlogger.error("PROV7005 BaseServlet.getSubscriberPermission: ", e.getMessage());
+            intlogger.error("PROV7005 BaseServlet.getSubscriberPermission: " + e.getMessage(), e);
         }
         return null;
     }