1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.provisioning;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStream;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Collection;
\r
31 import java.util.List;
\r
32 import java.util.Properties;
\r
34 import javax.servlet.ServletConfig;
\r
35 import javax.servlet.ServletException;
\r
36 import javax.servlet.http.HttpServletRequest;
\r
37 import javax.servlet.http.HttpServletResponse;
\r
39 import org.json.JSONArray;
\r
40 import org.json.JSONObject;
\r
41 import org.json.JSONTokener;
\r
43 import com.att.eelf.configuration.EELFLogger;
\r
44 import com.att.eelf.configuration.EELFManager;
\r
45 import com.att.research.datarouter.provisioning.beans.EventLogRecord;
\r
46 import com.att.research.datarouter.provisioning.beans.Feed;
\r
47 import com.att.research.datarouter.provisioning.beans.IngressRoute;
\r
48 import com.att.research.datarouter.provisioning.eelf.EelfMsgs;
\r
49 import com.att.research.datarouter.provisioning.utils.*;
\r
52 * This servlet handles redirects for the <publishURL> on the provisioning server,
\r
53 * which is generated by the provisioning server to handle a particular subscriptions to a feed.
\r
54 * See the <b>File Publishing and Delivery API</b> document for details on how these methods
\r
55 * should be invoked.
\r
57 * @author Robert Eby
\r
58 * @version $Id: PublishServlet.java,v 1.8 2014/03/12 19:45:41 eby Exp $
\r
60 @SuppressWarnings("serial")
\r
61 public class PublishServlet extends BaseServlet {
\r
62 private int next_node;
\r
63 private String provstring;
\r
64 private List<IngressRoute> irt;
\r
65 //Adding EELF Logger Rally:US664892
\r
66 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.provisioning.PublishServlet");
\r
70 public void init(ServletConfig config) throws ServletException {
\r
74 irt = new ArrayList<IngressRoute>();
\r
78 public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
79 setIpAndFqdnForEelf("doDelete");
\r
80 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
81 redirect(req, resp);
\r
84 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
85 setIpAndFqdnForEelf("doGet");
\r
86 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
87 redirect(req, resp);
\r
90 public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
91 setIpAndFqdnForEelf("doPut");
\r
92 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER),getIdFromPath(req)+"");
\r
93 redirect(req, resp);
\r
96 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
97 setIpAndFqdnForEelf("doPost");
\r
98 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
\r
99 redirect(req, resp);
\r
101 private void redirect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
\r
102 String[] nodes = getNodes();
\r
103 if (nodes == null || nodes.length == 0) {
\r
104 resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "There are no nodes defined in the DR network.");
\r
106 EventLogRecord elr = new EventLogRecord(req);
\r
107 int feedid = checkPath(req);
\r
109 String message = (feedid == -1)
\r
110 ? "Invalid request - Missing or bad feed number."
\r
111 : "Invalid request - Missing file ID.";
\r
112 elr.setMessage(message);
\r
113 elr.setResult(HttpServletResponse.SC_NOT_FOUND);
\r
114 eventlogger.info(elr);
\r
116 resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
\r
118 // Generate new URL
\r
119 String nextnode = getRedirectNode(feedid, req);
\r
120 nextnode = nextnode+":"+DB.HTTPS_PORT;
\r
121 String newurl = "https://" + nextnode + "/publish" + req.getPathInfo();
\r
122 String qs = req.getQueryString();
\r
124 newurl += "?" + qs;
\r
126 // Log redirect in event log
\r
127 String message = "Redirected to: "+newurl;
\r
128 elr.setMessage(message);
\r
129 elr.setResult(HttpServletResponse.SC_MOVED_PERMANENTLY);
\r
130 eventlogger.info(elr);
\r
132 resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
\r
133 resp.setHeader("Location", newurl);
\r
137 private String getRedirectNode(int feedid, HttpServletRequest req) {
\r
138 // Check to see if the IRT needs to be updated
\r
139 Poker p = Poker.getPoker();
\r
140 String s = p.getProvisioningString();
\r
141 synchronized (provstring) {
\r
142 if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) {
\r
143 // Provisioning string has changed -- update the IRT
\r
145 JSONObject jo = new JSONObject(new JSONTokener(provstring));
\r
146 JSONArray ja = jo.getJSONArray("ingress");
\r
147 List<IngressRoute> newlist = new ArrayList<IngressRoute>();
\r
148 for (int i = 0; i < ja.length(); i++) {
\r
149 IngressRoute iroute = new IngressRoute(ja.getJSONObject(i));
\r
150 newlist.add(iroute);
\r
156 // Look in IRT for next node
\r
157 for (IngressRoute route : irt) {
\r
158 if (route.matches(feedid, req)) {
\r
159 // pick a node at random from the list
\r
160 Collection<String> nodes = route.getNodes();
\r
161 String[] arr = nodes.toArray(new String[0]);
\r
162 long id = System.currentTimeMillis() % arr.length;
\r
163 String node = arr[(int) id];
\r
164 intlogger.info("Redirecting to "+node+" because of route "+route);
\r
169 // No IRT rule matches, do round robin of all active nodes
\r
170 String[] nodes = getNodes();
\r
171 if (next_node >= nodes.length) // The list of nodes may have grown/shrunk
\r
173 return nodes[next_node++];
\r
175 private int checkPath(HttpServletRequest req) {
\r
176 String path = req.getPathInfo();
\r
177 if (path == null || path.length() < 2)
\r
179 path = path.substring(1);
\r
180 int ix = path.indexOf('/');
\r
181 if (ix < 0 || ix == path.length()-1)
\r
184 int feedid = Integer.parseInt(path.substring(0, ix));
\r
185 if (!Feed.isFeedValid(feedid))
\r
188 } catch (NumberFormatException e) {
\r