Fixing code as part of dmaap-775
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / PublishServlet.java
index 4cefdf1..0728595 100644 (file)
-/*******************************************************************************
- * ============LICENSE_START==================================================
- * * org.onap.dmaap
- * * ===========================================================================
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * * ===========================================================================
- * * 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.
- * * ============LICENSE_END====================================================
- * *
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
- * *
- ******************************************************************************/
-
-
-package org.onap.dmaap.datarouter.provisioning;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-import org.json.JSONTokener;
-import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;
-import org.onap.dmaap.datarouter.provisioning.beans.Feed;
-import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute;
-import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;
-import org.onap.dmaap.datarouter.provisioning.utils.*;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-
-/**
- * This servlet handles redirects for the <publishURL> on the provisioning server,
- * which is generated by the provisioning server to handle a particular subscriptions to a feed.
- * See the <b>File Publishing and Delivery API</b> document for details on how these methods
- * should be invoked.
- *
- * @author Robert Eby
- * @version $Id: PublishServlet.java,v 1.8 2014/03/12 19:45:41 eby Exp $
- */
-@SuppressWarnings("serial")
-public class PublishServlet extends BaseServlet {
-    private int next_node;
-    private String provstring;
-    private List<IngressRoute> irt;
-    //Adding EELF Logger Rally:US664892
-    private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet");
-    private static final Object lock = new Object();
-
-
-    @Override
-    public void init(ServletConfig config) throws ServletException {
-        super.init(config);
-        next_node = 0;
-        provstring = "";
-        irt = new ArrayList<IngressRoute>();
-
-    }
-    @Override
-    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-        setIpAndFqdnForEelf("doDelete");
-        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
-        redirect(req, resp);
-    }
-    @Override
-    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-        setIpAndFqdnForEelf("doGet");
-        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
-        redirect(req, resp);
-    }
-    @Override
-    public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-        setIpAndFqdnForEelf("doPut");
-        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
-        redirect(req, resp);
-    }
-    @Override
-    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
-        setIpAndFqdnForEelf("doPost");
-        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
-        redirect(req, resp);
-
-    }
-    private void redirect(HttpServletRequest req, HttpServletResponse resp)  {
-        try {
-            String[] nodes = getNodes();
-            if (nodes == null || nodes.length == 0) {
-                resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "There are no nodes defined in the DR network.");
-            } else {
-                EventLogRecord elr = new EventLogRecord(req);
-                int feedid = checkPath(req);
-                if (feedid < 0) {
-                    String message = (feedid == -1)
-                            ? "Invalid request - Missing or bad feed number."
-                            : "Invalid request - Missing file ID.";
-                    elr.setMessage(message);
-                    elr.setResult(HttpServletResponse.SC_NOT_FOUND);
-                    eventlogger.info(elr);
-
-                    resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
-                } else {
-                    // Generate new URL
-                    String nextnode = getRedirectNode(feedid, req);
-                    nextnode = nextnode + ":" + DB.HTTPS_PORT;
-                    String newurl = "https://" + nextnode + "/publish" + req.getPathInfo();
-                    String qs = req.getQueryString();
-                    if (qs != null)
-                        newurl += "?" + qs;
-
-                    // Log redirect in event log
-                    String message = "Redirected to: " + newurl;
-                    elr.setMessage(message);
-                    elr.setResult(HttpServletResponse.SC_MOVED_PERMANENTLY);
-                    eventlogger.info(elr);
-
-                    resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
-                    resp.setHeader("Location", newurl);
-                }
-            }
-        } catch (IOException ioe) {
-            intlogger.error("IOException" + ioe.getMessage());
-
-        }
-    }
-    private String getRedirectNode(int feedid, HttpServletRequest req) {
-        // Check to see if the IRT needs to be updated
-        Poker p = Poker.getPoker();
-        String s = p.getProvisioningString();
-        synchronized (lock) {
-            if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) {
-                // Provisioning string has changed -- update the IRT
-                provstring = s;
-                JSONObject jo = new JSONObject(new JSONTokener(provstring));
-                JSONArray ja = jo.getJSONArray("ingress");
-                List<IngressRoute> newlist = new ArrayList<IngressRoute>();
-                for (int i = 0; i < ja.length(); i++) {
-                    IngressRoute iroute = new IngressRoute(ja.getJSONObject(i));
-                    newlist.add(iroute);
-                }
-                irt = newlist;
-            }
-        }
-
-        // Look in IRT for next node
-        for (IngressRoute route : irt) {
-            if (route.matches(feedid, req)) {
-                // pick a node at random from the list
-                Collection<String> nodes = route.getNodes();
-                String[] arr = nodes.toArray(new String[0]);
-                long id = System.currentTimeMillis() % arr.length;
-                String node = arr[(int) id];
-                intlogger.info("Redirecting to "+node+" because of route "+route);
-                return node;
-            }
-        }
-
-        // No IRT rule matches, do round robin of all active nodes
-        String[] nodes = getNodes();
-        if (next_node >= nodes.length)    // The list of nodes may have grown/shrunk
-            next_node = 0;
-        return nodes[next_node++];
-    }
-    private int checkPath(HttpServletRequest req) {
-        String path = req.getPathInfo();
-        if (path == null || path.length() < 2)
-            return -1;
-        path = path.substring(1);
-        int ix = path.indexOf('/');
-        if (ix < 0 || ix == path.length()-1)
-            return -2;
-        try {
-            int feedid = Integer.parseInt(path.substring(0, ix));
-            if (!Feed.isFeedValid(feedid))
-                return -1;
-            return feedid;
-        } catch (NumberFormatException e) {
-            return -1;
-        }
-    }
-}
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * *\r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * *\r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+\r
+\r
+package org.onap.dmaap.datarouter.provisioning;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+import javax.servlet.ServletConfig;\r
+import javax.servlet.ServletException;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+import org.json.JSONArray;\r
+import org.json.JSONObject;\r
+import org.json.JSONTokener;\r
+import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;\r
+import org.onap.dmaap.datarouter.provisioning.beans.Feed;\r
+import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute;\r
+import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;\r
+import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+\r
+/**\r
+ * This servlet handles redirects for the &lt;publishURL&gt; on the provisioning server, which is generated by the\r
+ * provisioning server to handle a particular subscriptions to a feed. See the <b>File Publishing and Delivery API</b>\r
+ * document for details on how these methods should be invoked.\r
+ *\r
+ * @author Robert Eby\r
+ * @version $Id: PublishServlet.java,v 1.8 2014/03/12 19:45:41 eby Exp $\r
+ */\r
+@SuppressWarnings("serial")\r
+public class PublishServlet extends BaseServlet {\r
+\r
+    private int next_node;\r
+    private String provstring;\r
+    private List<IngressRoute> irt;\r
+    //Adding EELF Logger Rally:US664892\r
+    private static EELFLogger eelflogger = EELFManager.getInstance()\r
+        .getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet");\r
+    private static final Object lock = new Object();\r
+\r
+\r
+    @Override\r
+    public void init(ServletConfig config) throws ServletException {\r
+        super.init(config);\r
+        next_node = 0;\r
+        provstring = "";\r
+        irt = new ArrayList<IngressRoute>();\r
+\r
+    }\r
+\r
+    @Override\r
+    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+        setIpAndFqdnForEelf("doDelete");\r
+        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+        redirect(req, resp);\r
+    }\r
+\r
+    @Override\r
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+        setIpAndFqdnForEelf("doGet");\r
+        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+        redirect(req, resp);\r
+    }\r
+\r
+    @Override\r
+    public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+        setIpAndFqdnForEelf("doPut");\r
+        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");\r
+        redirect(req, resp);\r
+    }\r
+\r
+    @Override\r
+    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {\r
+        setIpAndFqdnForEelf("doPost");\r
+        eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));\r
+        redirect(req, resp);\r
+\r
+    }\r
+\r
+    private void redirect(HttpServletRequest req, HttpServletResponse resp) {\r
+        try {\r
+            String[] nodes = getNodes();\r
+            if (nodes == null || nodes.length == 0) {\r
+                resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,\r
+                    "There are no nodes defined in the DR network.");\r
+            } else {\r
+                EventLogRecord elr = new EventLogRecord(req);\r
+                int feedid = checkPath(req);\r
+                if (feedid < 0) {\r
+                    String message = (feedid == -1)\r
+                        ? "Invalid request - Missing or bad feed number."\r
+                        : "Invalid request - Missing file ID.";\r
+                    elr.setMessage(message);\r
+                    elr.setResult(HttpServletResponse.SC_NOT_FOUND);\r
+                    eventlogger.info(elr);\r
+\r
+                    resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);\r
+                } else {\r
+                    // Generate new URL\r
+                    String nextnode = getRedirectNode(feedid, req);\r
+                    nextnode = nextnode + ":" + DB.getHttpsPort();\r
+                    String newurl = "https://" + nextnode + "/publish" + req.getPathInfo();\r
+                    String qs = req.getQueryString();\r
+                    if (qs != null) {\r
+                        newurl += "?" + qs;\r
+                    }\r
+\r
+                    // Log redirect in event log\r
+                    String message = "Redirected to: " + newurl;\r
+                    elr.setMessage(message);\r
+                    elr.setResult(HttpServletResponse.SC_MOVED_PERMANENTLY);\r
+                    eventlogger.info(elr);\r
+\r
+                    resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);\r
+                    resp.setHeader("Location", newurl);\r
+                }\r
+            }\r
+        } catch (IOException ioe) {\r
+            intlogger.error("IOException" + ioe.getMessage());\r
+\r
+        }\r
+    }\r
+\r
+    private String getRedirectNode(int feedid, HttpServletRequest req) {\r
+        // Check to see if the IRT needs to be updated\r
+        Poker p = Poker.getPoker();\r
+        String s = p.getProvisioningString();\r
+        synchronized (lock) {\r
+            if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) {\r
+                // Provisioning string has changed -- update the IRT\r
+                provstring = s;\r
+                JSONObject jo = new JSONObject(new JSONTokener(provstring));\r
+                JSONArray ja = jo.getJSONArray("ingress");\r
+                List<IngressRoute> newlist = new ArrayList<IngressRoute>();\r
+                for (int i = 0; i < ja.length(); i++) {\r
+                    IngressRoute iroute = new IngressRoute(ja.getJSONObject(i));\r
+                    newlist.add(iroute);\r
+                }\r
+                irt = newlist;\r
+            }\r
+        }\r
+\r
+        // Look in IRT for next node\r
+        for (IngressRoute route : irt) {\r
+            if (route.matches(feedid, req)) {\r
+                // pick a node at random from the list\r
+                Collection<String> nodes = route.getNodes();\r
+                String[] arr = nodes.toArray(new String[0]);\r
+                long id = System.currentTimeMillis() % arr.length;\r
+                String node = arr[(int) id];\r
+                intlogger.info("Redirecting to " + node + " because of route " + route);\r
+                return node;\r
+            }\r
+        }\r
+\r
+        // No IRT rule matches, do round robin of all active nodes\r
+        String[] nodes = getNodes();\r
+        if (next_node >= nodes.length)    // The list of nodes may have grown/shrunk\r
+        {\r
+            next_node = 0;\r
+        }\r
+        return nodes[next_node++];\r
+    }\r
+\r
+    private int checkPath(HttpServletRequest req) {\r
+        String path = req.getPathInfo();\r
+        if (path == null || path.length() < 2) {\r
+            return -1;\r
+        }\r
+        path = path.substring(1);\r
+        int ix = path.indexOf('/');\r
+        if (ix < 0 || ix == path.length() - 1) {\r
+            return -2;\r
+        }\r
+        try {\r
+            int feedid = Integer.parseInt(path.substring(0, ix));\r
+            if (!Feed.isFeedValid(feedid)) {\r
+                return -1;\r
+            }\r
+            return feedid;\r
+        } catch (NumberFormatException e) {\r
+            return -1;\r
+        }\r
+    }\r
+}\r