X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FPublishServlet.java;h=d500bb31ea49a58708b6bd13f11262bfe670191e;hp=e736d79ca5b50b14a8068f6b5bd825d5aafa3648;hb=4261823d84c2b911b68cdf4cb4dc3be429ebe285;hpb=13639e1b05d8c8b5b1e9efd543573834501aefaa diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java index e736d79c..d500bb31 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java @@ -1,192 +1,192 @@ -/******************************************************************************* - * ============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.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Properties; - -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 File Publishing and Delivery API 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 irt; - //Adding EELF Logger Rally:US664892 - private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet"); - - - @Override - public void init(ServletConfig config) throws ServletException { - super.init(config); - next_node = 0; - provstring = ""; - irt = new ArrayList(); - - } - @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) throws IOException { - 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); - } - } - } - 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 (provstring) { - 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 newlist = new ArrayList(); - 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 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; - } - } -} +/******************************************************************************* + * ============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.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Properties; + +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 File Publishing and Delivery API 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 irt; + //Adding EELF Logger Rally:US664892 + private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet"); + + + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + next_node = 0; + provstring = ""; + irt = new ArrayList(); + + } + @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) throws IOException { + 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); + } + } + } + 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 (provstring) { + 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 newlist = new ArrayList(); + 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 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; + } + } +}