1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
30 import java.io.FileOutputStream;
31 import java.io.FileWriter;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.io.Writer;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Enumeration;
40 import java.util.regex.Pattern;
41 import javax.servlet.http.HttpServlet;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import org.apache.log4j.Logger;
45 import org.jetbrains.annotations.Nullable;
46 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
49 import static org.onap.dmaap.datarouter.node.NodeUtils.*;
52 * Servlet for handling all http and https requests to the data router node
54 * Handled requests are:
56 * GET http://<i>node</i>/internal/fetchProv - fetch the provisioning data
58 * PUT/DELETE https://<i>node</i>/internal/publish/<i>fileid</i> - n2n transfer
60 * PUT/DELETE https://<i>node</i>/publish/<i>feedid</i>/<i>fileid</i> - publsh request
62 public class NodeServlet extends HttpServlet {
64 private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeServlet");
65 private static NodeConfigManager config;
66 private static Pattern MetaDataPattern;
67 //Adding EELF Logger Rally:US664892
68 private static EELFLogger eelflogger = EELFManager.getInstance()
69 .getLogger(NodeServlet.class);
70 private final Delivery delivery;
73 final String ws = "\\s*";
74 // assume that \\ and \" have been replaced by X
75 final String string = "\"[^\"]*\"";
76 //String string = "\"(?:[^\"\\\\]|\\\\.)*\"";
77 final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?";
78 final String value = "(?:" + string + "|" + number + "|null|true|false)";
79 final String item = string + ws + ":" + ws + value + ws;
80 final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws;
81 MetaDataPattern = Pattern.compile(object, Pattern.DOTALL);
84 NodeServlet(Delivery delivery) {
85 this.delivery = delivery;
89 * Get the NodeConfigurationManager
92 config = NodeConfigManager.getInstance();
93 logger.info("NODE0101 Node Servlet Configured");
96 private boolean down(HttpServletResponse resp) throws IOException {
97 if (config.isShutdown() || !config.isConfigured()) {
98 sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, logger);
99 logger.info("NODE0102 Rejecting request: Service is being quiesced");
106 * Handle a GET for /internal/fetchProv
108 protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
109 NodeUtils.setIpAndFqdnForEelf("doGet");
110 NodeUtils.setRequestIdAndInvocationId(req);
111 eelflogger.info(EelfMsgs.ENTRY);
113 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"),
114 getIdFromPath(req) + "");
120 } catch (IOException ioe) {
121 logger.error("IOException" + ioe.getMessage());
123 String path = req.getPathInfo();
124 String qs = req.getQueryString();
125 String ip = req.getRemoteAddr();
127 path = path + "?" + qs;
129 if ("/internal/fetchProv".equals(path)) {
131 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
133 } else if (path.startsWith("/internal/resetSubscription/")) {
134 String subid = path.substring(28);
135 if (subid.length() != 0 && subid.indexOf('/') == -1) {
136 NodeMain.resetQueue(subid, ip);
137 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
142 logger.info("NODE0103 Rejecting invalid GET of " + path + " from " + ip);
143 sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, logger);
145 eelflogger.info(EelfMsgs.EXIT);
150 * Handle all PUT requests
152 protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
153 NodeUtils.setIpAndFqdnForEelf("doPut");
154 NodeUtils.setRequestIdAndInvocationId(req);
155 eelflogger.info(EelfMsgs.ENTRY);
156 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"),
157 getIdFromPath(req) + "");
159 common(req, resp, true);
160 } catch (IOException ioe) {
161 logger.error("IOException" + ioe.getMessage());
162 eelflogger.info(EelfMsgs.EXIT);
167 * Handle all DELETE requests
169 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
170 NodeUtils.setIpAndFqdnForEelf("doDelete");
171 NodeUtils.setRequestIdAndInvocationId(req);
172 eelflogger.info(EelfMsgs.ENTRY);
173 eelflogger.info(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader("X-DMAAP-DR-ON-BEHALF-OF"),
174 getIdFromPath(req) + "");
176 common(req, resp, false);
177 } catch (IOException ioe) {
178 logger.error("IOException " + ioe.getMessage());
179 eelflogger.info(EelfMsgs.EXIT);
183 private void common(HttpServletRequest req, HttpServletResponse resp, boolean isput) throws IOException {
184 String fileid = getFileId(req, resp);
185 if (fileid == null) return;
186 String feedid = null;
188 String ip = req.getRemoteAddr();
189 String lip = req.getLocalAddr();
191 String xpubid = null;
192 String rcvd = NodeUtils.logts(System.currentTimeMillis()) + ";from=" + ip + ";by=" + lip;
193 Target[] targets = null;
194 if (fileid.startsWith("/delete/")) {
195 deleteFile(req, resp, fileid, pubid);
198 String credentials = req.getHeader("Authorization");
199 if (credentials == null) {
200 logger.info("NODE0106 Rejecting unauthenticated PUT or DELETE of " + req.getPathInfo() + " from " + req
202 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Authorization header required");
203 eelflogger.info(EelfMsgs.EXIT);
206 if (fileid.startsWith("/publish/")) {
207 fileid = fileid.substring(9);
208 int i = fileid.indexOf('/');
209 if (i == -1 || i == fileid.length() - 1) {
210 logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
212 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
213 "Invalid request URI. Expecting <feed-publishing-url>/<fileid>. Possible missing fileid.");
214 eelflogger.info(EelfMsgs.EXIT);
217 feedid = fileid.substring(0, i);
218 fileid = fileid.substring(i + 1);
219 pubid = config.getPublishId();
220 xpubid = req.getHeader("X-DMAAP-DR-PUBLISH-ID");
221 targets = config.getTargets(feedid);
222 } else if (fileid.startsWith("/internal/publish/")) {
223 if (!config.isAnotherNode(credentials, ip)) {
224 logger.info("NODE0107 Rejecting unauthorized node-to-node transfer attempt from " + ip);
225 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
226 eelflogger.info(EelfMsgs.EXIT);
229 fileid = fileid.substring(18);
230 pubid = req.getHeader("X-DMAAP-DR-PUBLISH-ID");
231 targets = config.parseRouting(req.getHeader("X-DMAAP-DR-ROUTING"));
233 logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
235 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
236 "Invalid request URI. Expecting <feed-publishing-url>/<fileid>.");
237 eelflogger.info(EelfMsgs.EXIT);
240 if (fileid.indexOf('/') != -1) {
241 logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
243 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
244 "Invalid request URI. Expecting <feed-publishing-url>/<fileid>.");
245 eelflogger.info(EelfMsgs.EXIT);
248 String qs = req.getQueryString();
250 fileid = fileid + "?" + qs;
252 String hp = config.getMyName();
253 int xp = config.getExtHttpsPort();
257 String logurl = "https://" + hp + "/internal/publish/" + fileid;
258 if (feedid != null) {
259 logurl = "https://" + hp + "/publish/" + feedid + "/" + fileid;
260 String reason = config.isPublishPermitted(feedid, credentials, ip);
261 if (reason != null) {
263 "NODE0111 Rejecting unauthorized publish attempt to feed " + feedid + " fileid " + fileid
265 + ip + " reason " + reason);
266 resp.sendError(HttpServletResponse.SC_FORBIDDEN, reason);
267 eelflogger.info(EelfMsgs.EXIT);
270 user = config.getAuthUser(feedid, credentials);
271 String newnode = config.getIngressNode(feedid, user, ip);
272 if (newnode != null) {
274 int iport = config.getExtHttpsPort();
278 String redirto = "https://" + newnode + port + "/publish/" + feedid + "/" + fileid;
280 "NODE0108 Redirecting publish attempt for feed " + feedid + " user " + user + " ip " + ip
283 resp.sendRedirect(redirto);
284 eelflogger.info(EelfMsgs.EXIT);
287 resp.setHeader("X-DMAAP-DR-PUBLISH-ID", pubid);
289 String fbase = config.getSpoolDir() + "/" + pubid;
290 File data = new File(fbase);
291 File meta = new File(fbase + ".M");
292 OutputStream dos = null;
294 InputStream is = null;
296 StringBuffer mx = new StringBuffer();
297 mx.append(req.getMethod()).append('\t').append(fileid).append('\n');
298 Enumeration hnames = req.getHeaderNames();
300 boolean hasRequestIdHeader = false;
301 boolean hasInvocationIdHeader = false;
302 while (hnames.hasMoreElements()) {
303 String hn = (String) hnames.nextElement();
304 String hnlc = hn.toLowerCase();
305 if ((isput && ("content-type".equals(hnlc) ||
306 "content-language".equals(hnlc) ||
307 "content-md5".equals(hnlc) ||
308 "content-range".equals(hnlc))) ||
309 "x-dmaap-dr-meta".equals(hnlc) ||
310 (feedid == null && "x-dmaap-dr-received".equals(hnlc)) ||
311 (hnlc.startsWith("x-") && !hnlc.startsWith("x-dmaap-dr-"))) {
312 Enumeration hvals = req.getHeaders(hn);
313 while (hvals.hasMoreElements()) {
314 String hv = (String) hvals.nextElement();
315 if ("content-type".equals(hnlc)) {
318 if ("x-onap-requestid".equals(hnlc)) {
319 hasRequestIdHeader = true;
321 if ("x-invocationid".equals(hnlc)) {
322 hasInvocationIdHeader = true;
324 if ("x-dmaap-dr-meta".equals(hnlc)) {
325 if (hv.length() > 4096) {
327 "NODE0109 Rejecting publish attempt with metadata too long for feed " + feedid
328 + " user " + user + " ip " + ip);
329 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Metadata too long");
330 eelflogger.info(EelfMsgs.EXIT);
333 if (!MetaDataPattern.matcher(hv.replaceAll("\\\\.", "X")).matches()) {
335 "NODE0109 Rejecting publish attempt with malformed metadata for feed " + feedid
336 + " user " + user + " ip " + ip);
337 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed metadata");
338 eelflogger.info(EelfMsgs.EXIT);
342 mx.append(hn).append('\t').append(hv).append('\n');
346 if(!hasRequestIdHeader){
347 mx.append("X-ONAP-RequestID\t").append(MDC.get("RequestId")).append('\n');
349 if(!hasInvocationIdHeader){
350 mx.append("X-InvocationID\t").append(MDC.get("InvocationId")).append('\n');
352 mx.append("X-DMAAP-DR-RECEIVED\t").append(rcvd).append('\n');
353 String metadata = mx.toString();
354 byte[] buf = new byte[1024 * 1024];
357 is = req.getInputStream();
358 dos = new FileOutputStream(data);
359 while ((i = is.read(buf)) > 0) {
360 dos.write(buf, 0, i);
366 } catch (IOException ioe) {
369 exlen = Long.parseLong(req.getHeader("Content-Length"));
370 } catch (Exception e) {
372 StatusLog.logPubFail(pubid, feedid, logurl, req.getMethod(), ctype, exlen, data.length(), ip, user,
374 eelflogger.info(EelfMsgs.EXIT);
377 Path dpath = Paths.get(fbase);
378 for (Target t : targets) {
379 DestInfo di = t.getDestInfo();
381 // TODO: unknown destination
384 String dbase = di.getSpool() + "/" + pubid;
385 Files.createLink(Paths.get(dbase), dpath);
386 mw = new FileWriter(meta);
388 if (di.getSubId() == null) {
389 mw.write("X-DMAAP-DR-ROUTING\t" + t.getRouting() + "\n");
392 meta.renameTo(new File(dbase + ".M"));
395 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
396 resp.getOutputStream().close();
397 StatusLog.logPub(pubid, feedid, logurl, req.getMethod(), ctype, data.length(), ip, user,
398 HttpServletResponse.SC_NO_CONTENT);
399 } catch (IOException ioe) {
401 "NODE0110 IO Exception receiving publish attempt for feed " + feedid + " user " + user + " ip " + ip
402 + " " + ioe.toString(), ioe);
403 eelflogger.info(EelfMsgs.EXIT);
409 } catch (Exception e) {
415 } catch (Exception e) {
421 } catch (Exception e) {
426 } catch (Exception e) {
430 } catch (Exception e) {
435 private void deleteFile(HttpServletRequest req, HttpServletResponse resp, String fileid, String pubid) {
437 fileid = fileid.substring(8);
438 int i = fileid.indexOf('/');
439 if (i == -1 || i == fileid.length() - 1) {
440 logger.info("NODE0112 Rejecting bad URI for DELETE of " + req.getPathInfo() + " from " + req
442 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
443 "Invalid request URI. Expecting <subId>/<pubId>.");
444 eelflogger.info(EelfMsgs.EXIT);
447 String subscriptionId = fileid.substring(0, i);
448 int subId = Integer.parseInt(subscriptionId);
449 pubid = fileid.substring(i + 1);
450 String errorMessage = "Unable to delete files (" + pubid + ", " + pubid + ".M) from DR Node: "
451 + config.getMyName() + ".";
452 int subIdDir = subId - (subId % 100);
453 if (!isAuthorizedToDelete(resp, subscriptionId, errorMessage)) {
456 boolean result = delivery.markTaskSuccess(config.getSpoolBase() + "/s/" + subIdDir + "/" + subId, pubid);
458 logger.info("NODE0115 Successfully deleted files (" + pubid + ", " + pubid + ".M) from DR Node: "
459 + config.getMyName());
460 resp.setStatus(HttpServletResponse.SC_OK);
461 eelflogger.info(EelfMsgs.EXIT);
463 logger.error("NODE0116 " + errorMessage);
464 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found on server.");
465 eelflogger.info(EelfMsgs.EXIT);
467 } catch (IOException ioe) {
468 logger.error("NODE0117 Unable to delete files (" + pubid + ", " + pubid + ".M) from DR Node: "
469 + config.getMyName() + ". Error: " + ioe.getMessage());
470 eelflogger.info(EelfMsgs.EXIT);
475 private String getFileId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
477 eelflogger.info(EelfMsgs.EXIT);
480 if (!req.isSecure()) {
482 "NODE0104 Rejecting insecure PUT or DELETE of " + req.getPathInfo() + " from " + req
484 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "https required on publish requests");
485 eelflogger.info(EelfMsgs.EXIT);
488 String fileid = req.getPathInfo();
489 if (fileid == null) {
490 logger.info("NODE0105 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + " from " + req
492 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
493 "Invalid request URI. Expecting <feed-publishing-url>/<fileid>.");
494 eelflogger.info(EelfMsgs.EXIT);
500 private boolean isAuthorizedToDelete(HttpServletResponse resp, String subscriptionId, String errorMessage) throws IOException {
502 boolean deletePermitted = config.isDeletePermitted(subscriptionId);
503 if (!deletePermitted) {
504 logger.error("NODE0113 " + errorMessage + " Error: Subscription "
505 + subscriptionId + " is not a privileged subscription");
506 resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
507 eelflogger.info(EelfMsgs.EXIT);
510 } catch (NullPointerException npe) {
511 logger.error("NODE0114 " + errorMessage + " Error: Subscription " + subscriptionId + " does not exist");
512 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
513 eelflogger.info(EelfMsgs.EXIT);
519 private int getIdFromPath(HttpServletRequest req) {
520 String path = req.getPathInfo();
521 if (path == null || path.length() < 2) {
525 return Integer.parseInt(path.substring(1));
526 } catch (NumberFormatException e) {