Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / SubscriptionServlet.java
index 125c50d..1f7c291 100644 (file)
 \r
 package org.onap.dmaap.datarouter.provisioning;\r
 \r
+import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.io.IOException;\r
 import java.io.InvalidObjectException;\r
 import java.net.HttpURLConnection;\r
 import java.net.URL;\r
+import java.util.ArrayList;\r
 import java.util.List;\r
-import java.util.Vector;\r
-\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
-\r
 import org.json.JSONException;\r
 import org.json.JSONObject;\r
 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;\r
+import org.onap.dmaap.datarouter.provisioning.utils.SynchronizerTask;\r
 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;\r
 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;\r
 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;\r
 \r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-\r
-import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.sendResponseError;\r
-\r
 /**\r
  * This servlet handles provisioning for the <subscriptionURL> which is generated by the provisioning server to\r
  * handle the inspection, modification, and deletion of a particular subscription to a feed. It supports DELETE to\r
@@ -76,7 +74,8 @@ public class SubscriptionServlet extends ProxyServlet {
         setIpFqdnRequestIDandInvocationIDForEelf("doDelete", req);\r
         eelfLogger.info(EelfMsgs.ENTRY);\r
         try {\r
-            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID,\r
+                    req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
             EventLogRecord elr = new EventLogRecord(req);\r
             String message = isAuthorizedForProvisioning(req);\r
             if (message != null) {\r
@@ -177,7 +176,8 @@ public class SubscriptionServlet extends ProxyServlet {
         setIpFqdnRequestIDandInvocationIDForEelf("doGet", req);\r
         eelfLogger.info(EelfMsgs.ENTRY);\r
         try {\r
-            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID,\r
+                    req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
             EventLogRecord elr = new EventLogRecord(req);\r
             String message = isAuthorizedForProvisioning(req);\r
             if (message != null) {\r
@@ -253,7 +253,8 @@ public class SubscriptionServlet extends ProxyServlet {
         setIpFqdnRequestIDandInvocationIDForEelf("doPut", req);\r
         eelfLogger.info(EelfMsgs.ENTRY);\r
         try {\r
-            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+            eelfLogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_SUBID,\r
+                    req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
             EventLogRecord elr = new EventLogRecord(req);\r
             String message = isAuthorizedForProvisioning(req);\r
             if (message != null) {\r
@@ -363,8 +364,8 @@ public class SubscriptionServlet extends ProxyServlet {
             sub.setSubid(oldsub.getSubid());\r
             sub.setFeedid(oldsub.getFeedid());\r
             sub.setSubscriber(bhdr);    // set from X-DMAAP-DR-ON-BEHALF-OF header\r
-\r
-            String subjectgroup = (req.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")); //Adding for group feature:Rally US708115\r
+            //Adding for group feature:Rally US708115\r
+            String subjectgroup = (req.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP"));\r
             if (!oldsub.getSubscriber().equals(sub.getSubscriber()) && subjectgroup == null) {\r
                 message = "This subscriber must be modified by the same subscriber that created it.";\r
                 elr.setMessage(message);\r
@@ -387,7 +388,7 @@ public class SubscriptionServlet extends ProxyServlet {
                     eventlogger.error("PROV0192 SubscriptionServlet.doPut: " + ioe.getMessage(), ioe);\r
                 }\r
 \r
-                /**Change Owner ship of Subscriber     Adding for group feature:Rally US708115*/\r
+                /**Change Owner ship of Subscriber.     Adding for group feature:Rally US708115*/\r
                 if (jo.has("changeowner") && subjectgroup != null) {\r
                     try {\r
                         Boolean changeowner = (Boolean) jo.get("changeowner");\r
@@ -399,7 +400,7 @@ public class SubscriptionServlet extends ProxyServlet {
                         eventlogger.error("PROV0193 SubscriptionServlet.doPut: " + je.getMessage(), je);\r
                     }\r
                 }\r
-                /***End of change ownership*/\r
+                /***End of change ownership.*/\r
 \r
                 provisioningDataChanged();\r
             } else {\r
@@ -488,12 +489,12 @@ public class SubscriptionServlet extends ProxyServlet {
             try {\r
                 // Only the active POD sends notifications\r
                 boolean active = SynchronizerTask.getSynchronizer().isActive();\r
-                boolean b = jo.getBoolean("failed");\r
-                if (active && !b) {\r
+                boolean bool = jo.getBoolean("failed");\r
+                if (active && !bool) {\r
                     // Notify all nodes to reset the subscription\r
-                    SubscriberNotifyThread t = new SubscriberNotifyThread();\r
-                    t.resetSubscription(subid);\r
-                    t.start();\r
+                    SubscriberNotifyThread thread = new SubscriberNotifyThread();\r
+                    thread.resetSubscription(subid);\r
+                    thread.start();\r
                 }\r
                 // send response\r
                 elr.setResult(HttpServletResponse.SC_ACCEPTED);\r
@@ -515,41 +516,44 @@ public class SubscriptionServlet extends ProxyServlet {
      * A Thread class used to serially send reset notifications to all nodes in the DR network, when a POST is received\r
      * for a subscription.\r
      */\r
-    public class SubscriberNotifyThread extends Thread {\r
+    public static class SubscriberNotifyThread extends Thread {\r
 \r
-        public static final String URL_TEMPLATE = "http://%s/internal/resetSubscription/%d";\r
-        private List<String> urls = new Vector<>();\r
+        static final String URL_TEMPLATE = "http://%s/internal/resetSubscription/%d";\r
+        private List<String> urls = new ArrayList<>();\r
 \r
-        public SubscriberNotifyThread() {\r
+        SubscriberNotifyThread() {\r
             setName("SubscriberNotifyThread");\r
         }\r
 \r
-        public void resetSubscription(int subid) {\r
+        void resetSubscription(int subid) {\r
             for (String nodename : BaseServlet.getNodes()) {\r
-                String u = String.format(URL_TEMPLATE, nodename, subid);\r
-                urls.add(u);\r
+                String url = String.format(URL_TEMPLATE, nodename, subid);\r
+                urls.add(url);\r
             }\r
         }\r
 \r
         @Override\r
         public void run() {\r
-\r
             try {\r
                 while (!urls.isEmpty()) {\r
-                    String u = urls.remove(0);\r
-                    try {\r
-                        URL url = new URL(u);\r
-                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();\r
-                        conn.connect();\r
-                        conn.getContentLength();    // Force the GET through\r
-                        conn.disconnect();\r
-                    } catch (IOException e) {\r
-                        intlogger.info("PROV0194 Error accessing URL: " + u + ": " + e.getMessage(), e);\r
-                    }\r
+                    String url = urls.remove(0);\r
+                    forceGetThrough(url);\r
                 }\r
             } catch (Exception e) {\r
                 intlogger.warn("PROV0195 Caught exception in SubscriberNotifyThread: " + e.getMessage(), e);\r
             }\r
         }\r
+\r
+        private void forceGetThrough(String url) {\r
+            try {\r
+                URL urlObj = new URL(url);\r
+                HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();\r
+                conn.connect();\r
+                conn.getContentLength();    // Force the GET through\r
+                conn.disconnect();\r
+            } catch (IOException e) {\r
+                intlogger.info("PROV0194 Error accessing URL: " + url + ": " + e.getMessage(), e);\r
+            }\r
+        }\r
     }\r
 }\r