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 org.onap.dmaap.datarouter.provisioning;
\r
27 import com.att.eelf.configuration.EELFLogger;
\r
28 import com.att.eelf.configuration.EELFManager;
\r
29 import java.io.IOException;
\r
30 import java.util.ArrayList;
\r
31 import java.util.Collection;
\r
32 import java.util.List;
\r
33 import javax.servlet.ServletConfig;
\r
34 import javax.servlet.ServletException;
\r
35 import javax.servlet.http.HttpServletRequest;
\r
36 import javax.servlet.http.HttpServletResponse;
\r
37 import org.json.JSONArray;
\r
38 import org.json.JSONException;
\r
39 import org.json.JSONObject;
\r
40 import org.json.JSONTokener;
\r
41 import org.onap.dmaap.datarouter.provisioning.beans.EventLogRecord;
\r
42 import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute;
\r
43 import org.onap.dmaap.datarouter.provisioning.eelf.EelfMsgs;
\r
44 import org.onap.dmaap.datarouter.provisioning.utils.DB;
\r
47 * This servlet handles redirects for the <publishURL> on the provisioning server, which is generated by the
\r
48 * provisioning server to handle a particular subscriptions to a feed. See the <b>File Publishing and Delivery API</b>
\r
49 * document for details on how these methods should be invoked.
\r
51 * @author Robert Eby
\r
52 * @version $Id: PublishServlet.java,v 1.8 2014/03/12 19:45:41 eby Exp $
\r
54 @SuppressWarnings("serial")
\r
55 public class PublishServlet extends BaseServlet {
\r
57 private int nextNode;
\r
58 private String provstring;
\r
59 private List<IngressRoute> irt;
\r
60 //Adding EELF Logger Rally:US664892
\r
61 private static EELFLogger eelflogger = EELFManager.getInstance()
\r
62 .getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet");
\r
63 private static final Object lock = new Object();
\r
67 public void init(ServletConfig config) throws ServletException {
\r
71 irt = new ArrayList<>();
\r
76 public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
\r
77 setIpAndFqdnForEelf("doDelete");
\r
78 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
\r
79 redirect(req, resp);
\r
83 public void doGet(HttpServletRequest req, HttpServletResponse resp) {
\r
84 setIpAndFqdnForEelf("doGet");
\r
85 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(BEHALF_HEADER), getIdFromPath(req) + "");
\r
86 redirect(req, resp);
\r
90 public void doPut(HttpServletRequest req, HttpServletResponse resp) {
\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
97 public void doPost(HttpServletRequest req, HttpServletResponse resp) {
\r
98 setIpAndFqdnForEelf("doPost");
\r
99 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF, req.getHeader(BEHALF_HEADER));
\r
100 redirect(req, resp);
\r
104 private void redirect(HttpServletRequest req, HttpServletResponse resp) {
\r
106 String[] nodes = getNodes();
\r
107 if (nodes == null || nodes.length == 0) {
\r
108 resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
\r
109 "There are no nodes defined in the DR network.");
\r
111 EventLogRecord elr = new EventLogRecord(req);
\r
112 int feedid = checkPath(req);
\r
114 String message = (feedid == -1)
\r
115 ? "Invalid request - Missing or bad feed number."
\r
116 : "Invalid request - Missing file ID.";
\r
117 elr.setMessage(message);
\r
118 elr.setResult(HttpServletResponse.SC_NOT_FOUND);
\r
119 eventlogger.info(elr);
\r
121 resp.sendError(HttpServletResponse.SC_NOT_FOUND, message);
\r
123 // Generate new URL
\r
124 String nextnode = getRedirectNode(feedid, req);
\r
125 nextnode = nextnode + ":" + DB.getHttpsPort();
\r
126 String newurl = "https://" + nextnode + "/publish" + req.getPathInfo();
\r
127 String qs = req.getQueryString();
\r
129 newurl += "?" + qs;
\r
132 // Log redirect in event log
\r
133 String message = "Redirected to: " + newurl;
\r
134 elr.setMessage(message);
\r
135 elr.setResult(HttpServletResponse.SC_MOVED_PERMANENTLY);
\r
136 eventlogger.info(elr);
\r
138 resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
\r
139 resp.setHeader("Location", newurl);
\r
142 } catch (IOException ioe) {
\r
143 intlogger.error("IOException" + ioe.getMessage());
\r
148 private String getRedirectNode(int feedid, HttpServletRequest req) {
\r
149 // Check to see if the IRT needs to be updated
\r
150 Poker p = Poker.getPoker();
\r
151 String s = p.getProvisioningString();
\r
152 synchronized (lock) {
\r
153 if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) {
\r
154 // Provisioning string has changed -- update the IRT
\r
156 JSONObject jo = new JSONObject(new JSONTokener(provstring));
\r
157 JSONArray ja = jo.getJSONArray("ingress");
\r
158 List<IngressRoute> newlist = new ArrayList<>();
\r
159 for (int i = 0; i < ja.length(); i++) {
\r
160 IngressRoute iroute = new IngressRoute(ja.getJSONObject(i));
\r
161 newlist.add(iroute);
\r
167 // Look in IRT for next node
\r
168 for (IngressRoute route : irt) {
\r
169 if (route.matches(feedid, req)) {
\r
170 // pick a node at random from the list
\r
171 Collection<String> nodes = route.getNodes();
\r
172 String[] arr = nodes.toArray(new String[0]);
\r
173 long id = System.currentTimeMillis() % arr.length;
\r
174 String node = arr[(int) id];
\r
175 intlogger.info("Redirecting to " + node + " because of route " + route);
\r
180 // No IRT rule matches, do round robin of all active nodes
\r
181 String[] nodes = getNodes();
\r
182 if (nextNode >= nodes.length) // The list of nodes may have grown/shrunk
\r
186 return nodes[nextNode++];
\r
189 private int checkPath(HttpServletRequest req) {
\r
190 String path = req.getPathInfo();
\r
191 if (path == null || path.length() < 2) {
\r
194 path = path.substring(1);
\r
195 int ix = path.indexOf('/');
\r
196 if (ix < 0 || ix == path.length() - 1) {
\r
200 int feedid = Integer.parseInt(path.substring(0, ix));
\r
201 String provData = Poker.getPoker().getProvisioningString();
\r
202 JSONArray feeds = new JSONObject(provData).getJSONArray("feeds");
\r
203 for (int n = 0; n < feeds.length(); ++n) {
\r
204 if (feeds.getJSONObject(n).getInt("feedid") == feedid) {
\r
209 } catch (NumberFormatException | JSONException e) {
\r