DMAAP-DR Header Injection fix
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / NodeServlet.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24
25 package org.onap.dmaap.datarouter.node;
26
27 import static org.onap.dmaap.datarouter.node.NodeUtils.sendResponseError;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.FileWriter;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.io.Writer;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.util.Enumeration;
42 import java.util.regex.Pattern;
43 import javax.servlet.http.HttpServlet;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import org.jetbrains.annotations.Nullable;
47 import org.onap.dmaap.datarouter.node.eelf.EelfMsgs;
48 import org.slf4j.MDC;
49
50 /**
51  * Servlet for handling all http and https requests to the data router node.
52  *
53  * <p>Handled requests are:
54  * <br>
55  * GET http://<i>node</i>/internal/fetchProv - fetch the provisioning data
56  * <br>
57  * PUT/DELETE https://<i>node</i>/internal/publish/<i>fileid</i> - n2n transfer
58  * <br>
59  * PUT/DELETE https://<i>node</i>/publish/<i>feedid</i>/<i>fileid</i> - publsh request
60  */
61 public class NodeServlet extends HttpServlet {
62
63     private static final String FROM = " from ";
64     private static final String INVALID_REQUEST_URI = "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.";
65     private static final String IO_EXCEPTION = "IOException";
66     private static final String ON_BEHALF_OF = "X-DMAAP-DR-ON-BEHALF-OF";
67     private static NodeConfigManager config;
68     private static Pattern metaDataPattern;
69     private static EELFLogger eelfLogger = EELFManager.getInstance().getLogger(NodeServlet.class);
70
71     static {
72         final String ws = "\\s*";
73         // assume that \\ and \" have been replaced by X
74         final String string = "\"[^\"]*\"";
75         final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?";
76         final String value = "(?:" + string + "|" + number + "|null|true|false)";
77         final String item = string + ws + ":" + ws + value + ws;
78         final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws;
79         metaDataPattern = Pattern.compile(object, Pattern.DOTALL);
80     }
81
82     private final Delivery delivery;
83
84     NodeServlet(Delivery delivery) {
85         this.delivery = delivery;
86     }
87
88     /**
89      * Get the NodeConfigurationManager.
90      */
91     @Override
92     public void init() {
93         config = NodeConfigManager.getInstance();
94         eelfLogger.debug("NODE0101 Node Servlet Configured");
95     }
96
97     private boolean down(HttpServletResponse resp) {
98         if (config.isShutdown() || !config.isConfigured()) {
99             sendResponseError(resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE, eelfLogger);
100             eelfLogger.error("NODE0102 Rejecting request: Service is being quiesced");
101             return true;
102         }
103         return false;
104     }
105
106     /**
107      * Handle a GET for /internal/fetchProv.
108      */
109     @Override
110     protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
111         NodeUtils.setIpAndFqdnForEelf("doGet");
112         NodeUtils.setRequestIdAndInvocationId(req);
113         eelfLogger.info(EelfMsgs.ENTRY);
114         try {
115             eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
116                     getIdFromPath(req) + "");
117             if (down(resp)) {
118                 return;
119             }
120             String path = req.getPathInfo();
121             String qs = req.getQueryString();
122             String ip = req.getRemoteAddr();
123             if (qs != null) {
124                 path = path + "?" + qs;
125             }
126             if ("/internal/fetchProv".equals(path)) {
127                 config.gofetch(ip);
128                 resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
129                 return;
130             } else if (path.startsWith("/internal/resetSubscription/")) {
131                 String subid = path.substring(28);
132                 if (subid.length() != 0 && subid.indexOf('/') == -1) {
133                     NodeServer.resetQueue(subid, ip);
134                     resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
135                     return;
136                 }
137             }
138
139             eelfLogger.debug("NODE0103 Rejecting invalid GET of " + path + FROM + ip);
140             sendResponseError(resp, HttpServletResponse.SC_NOT_FOUND, eelfLogger);
141         } finally {
142             eelfLogger.info(EelfMsgs.EXIT);
143         }
144     }
145
146     /**
147      * Handle all PUT requests.
148      */
149     @Override
150     protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
151         NodeUtils.setIpAndFqdnForEelf("doPut");
152         NodeUtils.setRequestIdAndInvocationId(req);
153         eelfLogger.info(EelfMsgs.ENTRY);
154         eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
155                 getIdFromPath(req) + "");
156         try {
157             common(req, resp, true);
158         } catch (IOException ioe) {
159             eelfLogger.error(IO_EXCEPTION, ioe);
160             eelfLogger.info(EelfMsgs.EXIT);
161         }
162     }
163
164     /**
165      * Handle all DELETE requests.
166      */
167     @Override
168     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) {
169         NodeUtils.setIpAndFqdnForEelf("doDelete");
170         NodeUtils.setRequestIdAndInvocationId(req);
171         eelfLogger.info(EelfMsgs.ENTRY);
172         eelfLogger.debug(EelfMsgs.MESSAGE_WITH_BEHALF_AND_FEEDID, req.getHeader(ON_BEHALF_OF),
173                 getIdFromPath(req) + "");
174         try {
175             common(req, resp, false);
176         } catch (IOException ioe) {
177             eelfLogger.error(IO_EXCEPTION, ioe);
178             eelfLogger.info(EelfMsgs.EXIT);
179         }
180     }
181
182     private void common(HttpServletRequest req, HttpServletResponse resp, boolean isput) throws IOException {
183         final String PUBLISH = "/publish/";
184         final String INTERNAL_PUBLISH = "/internal/publish/";
185         final String HTTPS = "https://";
186         final String USER = " user ";
187         String fileid = getFileId(req, resp);
188         if (fileid == null) {
189             return;
190         }
191         String feedid = null;
192         String user = null;
193         String ip = req.getRemoteAddr();
194         String lip = req.getLocalAddr();
195         String pubid = null;
196         String rcvd = NodeUtils.logts(System.currentTimeMillis()) + ";from=" + ip + ";by=" + lip;
197         Target[] targets = null;
198         boolean isAAFFeed = false;
199         if (fileid.startsWith("/delete/")) {
200             deleteFile(req, resp, fileid, pubid);
201             return;
202         }
203         String credentials = req.getHeader("Authorization");
204         if (credentials == null) {
205             eelfLogger.error("NODE0306 Rejecting unauthenticated PUT or DELETE of " + req.getPathInfo() + FROM + req
206                     .getRemoteAddr());
207             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Authorization header required");
208             eelfLogger.info(EelfMsgs.EXIT);
209             return;
210         }
211         if (fileid.startsWith(PUBLISH)) {
212             fileid = fileid.substring(9);
213             int index = fileid.indexOf('/');
214             if (index == -1 || index == fileid.length() - 1) {
215                 eelfLogger.error("NODE0205 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + FROM + req
216                         .getRemoteAddr());
217                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
218                         "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  Possible missing fileid.");
219                 eelfLogger.info(EelfMsgs.EXIT);
220                 return;
221             }
222             feedid = fileid.substring(0, index);
223
224             if (config.getCadiEnabled()) {
225                 String path = req.getPathInfo();
226                 if (!path.startsWith("/internal") && feedid != null) {
227                     String aafInstance = config.getAafInstance(feedid);
228                     if (!("legacy".equalsIgnoreCase(aafInstance))) {
229                         isAAFFeed = true;
230                         String permission = config.getPermission(aafInstance);
231                         eelfLogger.debug("NodeServlet.common() permission string - " + permission);
232                         //Check in CADI Framework API if user has AAF permission or not
233                         if (!req.isUserInRole(permission)) {
234                             String message = "AAF disallows access to permission string - " + permission;
235                             eelfLogger.error("NODE0307 Rejecting unauthenticated PUT or DELETE of " + req.getPathInfo()
236                                     + FROM + req.getRemoteAddr());
237                             resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
238                             eelfLogger.info(EelfMsgs.EXIT);
239                             return;
240                         }
241                     }
242                 }
243             }
244
245             fileid = fileid.substring(index + 1);
246             pubid = config.getPublishId();
247             targets = config.getTargets(feedid);
248         } else if (fileid.startsWith(INTERNAL_PUBLISH)) {
249             if (!config.isAnotherNode(credentials, ip)) {
250                 eelfLogger.error("NODE0107 Rejecting unauthorized node-to-node transfer attempt from " + ip);
251                 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
252                 eelfLogger.info(EelfMsgs.EXIT);
253                 return;
254             }
255             fileid = fileid.substring(18);
256             if (req.getHeader("X-DMAAP-DR-PUBLISH-ID") != null && !req.getHeader("X-DMAAP-DR-PUBLISH-ID").matches("^[a-zA-Z0-9_]+$")) {
257                 String reason = "Error validating header";
258                 eelfLogger.error(reason);
259                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, reason);
260                 eelfLogger.info(EelfMsgs.EXIT);
261                 return;
262             }
263             pubid = req.getHeader("X-DMAAP-DR-PUBLISH-ID");
264             user = "datartr";   // SP6 : Added usr as datartr to avoid null entries for internal routing
265             targets = config.parseRouting(req.getHeader("X-DMAAP-DR-ROUTING"));
266         } else {
267             eelfLogger.error("NODE0204 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + FROM + req
268                     .getRemoteAddr());
269             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
270                     INVALID_REQUEST_URI);
271             eelfLogger.info(EelfMsgs.EXIT);
272             return;
273         }
274         if (fileid.indexOf('/') != -1) {
275             eelfLogger.error("NODE0202 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + FROM + req
276                     .getRemoteAddr());
277             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
278                     INVALID_REQUEST_URI);
279             eelfLogger.info(EelfMsgs.EXIT);
280             return;
281         }
282         String qs = req.getQueryString();
283         if (qs != null) {
284             fileid = fileid + "?" + qs;
285         }
286         String hp = config.getMyName();
287         int xp = config.getExtHttpsPort();
288         if (xp != 443) {
289             hp = hp + ":" + xp;
290         }
291         String logurl = HTTPS + hp + INTERNAL_PUBLISH + fileid;
292         if (feedid != null) {
293             logurl = HTTPS + hp + PUBLISH + feedid + "/" + fileid;
294             //Cadi code starts
295             if (!isAAFFeed) {
296                 String reason = config.isPublishPermitted(feedid, credentials, ip);
297                 if (reason != null) {
298                     eelfLogger.error("NODE0111 Rejecting unauthorized publish attempt to feed " + PathUtil
299                             .cleanString(feedid) + " fileid " + PathUtil.cleanString(fileid) + FROM + PathUtil
300                             .cleanString(ip) + " reason " + PathUtil.cleanString(reason));
301                     resp.sendError(HttpServletResponse.SC_FORBIDDEN, reason);
302                     eelfLogger.info(EelfMsgs.EXIT);
303                     return;
304                 }
305                 user = config.getAuthUser(feedid, credentials);
306             } else {
307                 String reason = config.isPublishPermitted(feedid, ip);
308                 if (reason != null) {
309                     eelfLogger.error("NODE0111 Rejecting unauthorized publish attempt to feed " + PathUtil
310                             .cleanString(feedid) + " fileid " + PathUtil.cleanString(fileid) + FROM + PathUtil
311                             .cleanString(ip) + " reason   Invalid AAF user- " + PathUtil.cleanString(reason));
312                     String message = "Invalid AAF user- " + PathUtil.cleanString(reason);
313                     eelfLogger.debug("NODE0308 Rejecting unauthenticated PUT or DELETE of " + PathUtil
314                             .cleanString(req.getPathInfo()) + FROM + PathUtil.cleanString(req.getRemoteAddr()));
315                     resp.sendError(HttpServletResponse.SC_FORBIDDEN, message);
316                     return;
317                 }
318                 if ((req.getUserPrincipal() != null) && (req.getUserPrincipal().getName() != null)) {
319                     String userName = req.getUserPrincipal().getName();
320                     String[] attid = userName.split("@");
321                     user = attid[0];
322                 } else {
323                     user = "AAFUser";
324                 }
325             }
326             //Cadi code Ends
327             String newnode = config.getIngressNode(feedid, user, ip);
328             if (newnode != null) {
329                 String port = "";
330                 int iport = config.getExtHttpsPort();
331                 if (iport != 443) {
332                     port = ":" + iport;
333                 }
334                 String redirto = HTTPS + newnode + port + PUBLISH + feedid + "/" + fileid;
335                 eelfLogger
336                         .debug("NODE0108 Redirecting publish attempt for feed " + PathUtil.cleanString(feedid) + USER
337                                 + PathUtil.cleanString(user) + " ip " + PathUtil.cleanString(ip) + " to " + PathUtil
338                                 .cleanString(redirto));  //Fortify scan fixes - log forging
339                 resp.sendRedirect(PathUtil.cleanString(redirto));         //Fortify scan fixes-open redirect - 2 issues
340                 eelfLogger.info(EelfMsgs.EXIT);
341                 return;
342             }
343             resp.setHeader("X-DMAAP-DR-PUBLISH-ID", pubid);
344         }
345         if (req.getPathInfo().startsWith(INTERNAL_PUBLISH)) {
346             feedid = req.getHeader("X-DMAAP-DR-FEED-ID");
347         }
348         String fbase = PathUtil.cleanString(config.getSpoolDir() + "/" + pubid);  //Fortify scan fixes-Path manipulation
349         File data = new File(fbase);
350         File meta = new File(fbase + ".M");
351         Writer mw = null;
352         try {
353             StringBuilder mx = new StringBuilder();
354             mx.append(req.getMethod()).append('\t').append(fileid).append('\n');
355             Enumeration hnames = req.getHeaderNames();
356             String ctype = null;
357             boolean hasRequestIdHeader = false;
358             boolean hasInvocationIdHeader = false;
359             while (hnames.hasMoreElements()) {
360                 String hn = (String) hnames.nextElement();
361                 String hnlc = hn.toLowerCase();
362                 if ((isput && ("content-type".equals(hnlc)
363                         || "content-language".equals(hnlc)
364                         || "content-md5".equals(hnlc)
365                         || "content-range".equals(hnlc)))
366                         || "x-dmaap-dr-meta".equals(hnlc)
367                         || (feedid == null && "x-dmaap-dr-received".equals(hnlc))
368                         || (hnlc.startsWith("x-") && !hnlc.startsWith("x-dmaap-dr-"))) {
369                     Enumeration hvals = req.getHeaders(hn);
370                     while (hvals.hasMoreElements()) {
371                         String hv = (String) hvals.nextElement();
372                         if ("content-type".equals(hnlc)) {
373                             ctype = hv;
374                         }
375                         if ("x-onap-requestid".equals(hnlc)) {
376                             hasRequestIdHeader = true;
377                         }
378                         if ("x-invocationid".equals(hnlc)) {
379                             hasInvocationIdHeader = true;
380                         }
381                         if ("x-dmaap-dr-meta".equals(hnlc)) {
382                             if (hv.length() > 4096) {
383                                 eelfLogger.error("NODE0109 Rejecting publish attempt with metadata too long for feed "
384                                         + PathUtil.cleanString(feedid) + USER + PathUtil.cleanString(user) + " ip "
385                                         + PathUtil.cleanString(ip));  //Fortify scan fixes - log forging
386                                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Metadata too long");
387                                 eelfLogger.info(EelfMsgs.EXIT);
388                                 return;
389                             }
390                             if (!metaDataPattern.matcher(hv.replaceAll("\\\\.", "X")).matches()) {
391                                 eelfLogger.error("NODE0109 Rejecting publish attempt with malformed metadata for feed "
392                                         + PathUtil.cleanString(feedid) + USER + PathUtil.cleanString(user) + " ip "
393                                         + PathUtil.cleanString(ip));  //Fortify scan fixes - log forging
394                                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed metadata");
395                                 eelfLogger.info(EelfMsgs.EXIT);
396                                 return;
397                             }
398                         }
399                         mx.append(hn).append('\t').append(hv).append('\n');
400                     }
401                 }
402             }
403             if (!hasRequestIdHeader) {
404                 mx.append("X-ONAP-RequestID\t").append(MDC.get("RequestId")).append('\n');
405             }
406             if (!hasInvocationIdHeader) {
407                 mx.append("X-InvocationID\t").append(MDC.get("InvocationId")).append('\n');
408             }
409             mx.append("X-DMAAP-DR-RECEIVED\t").append(rcvd).append('\n');
410             String metadata = mx.toString();
411             long exlen = getExlen(req);
412             String message = writeInputStreamToFile(req, data);
413             if (message != null) {
414                 StatusLog.logPubFail(pubid, feedid, logurl, req.getMethod(), ctype, exlen, data.length(), ip, user,
415                         message);
416                 throw new IOException(message);
417             }
418             Path dpath = Paths.get(fbase);
419             for (Target t : targets) {
420                 DestInfo di = t.getDestInfo();
421                 if (di == null) {
422                     //Handle this? : unknown destination
423                     continue;
424                 }
425                 String dbase = PathUtil
426                         .cleanString(di.getSpool() + "/" + pubid);  //Fortify scan fixes-Path Manipulation
427                 Files.createLink(Paths.get(dbase), dpath);
428                 mw = new FileWriter(meta);
429                 mw.write(metadata);
430                 if (di.getSubId() == null) {
431                     mw.write("X-DMAAP-DR-ROUTING\t" + t.getRouting() + "\n");
432                 }
433                 mw.close();
434                 if (!meta.renameTo(new File(dbase + ".M"))) {
435                     eelfLogger.error("Rename of file " + dbase + " failed.");
436                 }
437             }
438             resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
439             try {
440                 resp.getOutputStream().close();
441             } catch (IOException ioe) {
442                 StatusLog.logPubFail(pubid, feedid, logurl, req.getMethod(), ctype, exlen, data.length(), ip, user,
443                         ioe.getMessage());
444                 //Fortify scan fixes - log forging
445                 eelfLogger.error("NODE0110 IO Exception while closing IO stream " + PathUtil.cleanString(feedid)
446                         + USER + PathUtil.cleanString(user) + " ip " + PathUtil.cleanString(ip) + " " + ioe
447                         .toString(), ioe);
448                 throw ioe;
449             }
450
451             StatusLog.logPub(pubid, feedid, logurl, req.getMethod(), ctype, data.length(), ip, user,
452                     HttpServletResponse.SC_NO_CONTENT);
453         } catch (IOException ioe) {
454             eelfLogger.error("NODE0110 IO Exception receiving publish attempt for feed " + feedid + USER + user
455                     + " ip " + ip + " " + ioe.toString(), ioe);
456             eelfLogger.info(EelfMsgs.EXIT);
457             throw ioe;
458         } finally {
459             if (mw != null) {
460                 try {
461                     mw.close();
462                 } catch (Exception e) {
463                     eelfLogger.error("NODE0532 Exception common: " + e);
464                 }
465             }
466             try {
467                 Files.delete(data.toPath());
468                 Files.delete(meta.toPath());
469             } catch (Exception e) {
470                 eelfLogger.error("NODE0533 Exception common: " + e);
471             }
472         }
473     }
474
475     private String writeInputStreamToFile(HttpServletRequest req, File data) {
476         byte[] buf = new byte[1024 * 1024];
477         int bytesRead;
478         try (OutputStream dos = new FileOutputStream(data);
479                 InputStream is = req.getInputStream()) {
480             while ((bytesRead = is.read(buf)) > 0) {
481                 dos.write(buf, 0, bytesRead);
482             }
483         } catch (IOException ioe) {
484             eelfLogger.error("NODE0530 Exception common: " + ioe, ioe);
485             eelfLogger.info(EelfMsgs.EXIT);
486             return ioe.getMessage();
487         }
488         return null;
489     }
490
491     private long getExlen(HttpServletRequest req) {
492         long exlen = -1;
493         try {
494             exlen = Long.parseLong(req.getHeader("Content-Length"));
495         } catch (Exception e) {
496             eelfLogger.error("NODE0529 Exception common: " + e);
497         }
498         return exlen;
499     }
500
501     private void deleteFile(HttpServletRequest req, HttpServletResponse resp, String fileid, String pubid) {
502         final String FROM_DR_MESSAGE = ".M) from DR Node: ";
503         try {
504             fileid = fileid.substring(8);
505             int index = fileid.indexOf('/');
506             if (index == -1 || index == fileid.length() - 1) {
507                 eelfLogger.error("NODE0112 Rejecting bad URI for DELETE of " + req.getPathInfo() + FROM + req
508                         .getRemoteAddr());
509                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
510                         "Invalid request URI. Expecting <subId>/<pubId>.");
511                 eelfLogger.info(EelfMsgs.EXIT);
512                 return;
513             }
514             String subscriptionId = fileid.substring(0, index);
515             int subId = Integer.parseInt(subscriptionId);
516             pubid = fileid.substring(index + 1);
517             String errorMessage = "Unable to delete files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
518                     + config.getMyName() + ".";
519             int subIdDir = subId - (subId % 100);
520             if (!isAuthorizedToDelete(resp, subscriptionId, errorMessage)) {
521                 return;
522             }
523             boolean result = delivery.markTaskSuccess(config.getSpoolBase() + "/s/" + subIdDir + "/" + subId, pubid);
524             if (result) {
525                 eelfLogger.debug("NODE0115 Successfully deleted files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
526                         + config.getMyName());
527                 resp.setStatus(HttpServletResponse.SC_OK);
528                 eelfLogger.info(EelfMsgs.EXIT);
529             } else {
530                 eelfLogger.error("NODE0116 " + errorMessage);
531                 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found on server.");
532                 eelfLogger.info(EelfMsgs.EXIT);
533             }
534         } catch (IOException ioe) {
535             eelfLogger.error("NODE0117 Unable to delete files (" + pubid + ", " + pubid + FROM_DR_MESSAGE
536                     + config.getMyName(), ioe);
537             eelfLogger.info(EelfMsgs.EXIT);
538         }
539     }
540
541     @Nullable
542     private String getFileId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
543         if (down(resp)) {
544             eelfLogger.info(EelfMsgs.EXIT);
545             return null;
546         }
547         if (!req.isSecure()) {
548             eelfLogger.error(
549                     "NODE0104 Rejecting insecure PUT or DELETE of " + req.getPathInfo() + FROM + req
550                             .getRemoteAddr());
551             resp.sendError(HttpServletResponse.SC_FORBIDDEN, "https required on publish requests");
552             eelfLogger.info(EelfMsgs.EXIT);
553             return null;
554         }
555         String fileid = req.getPathInfo();
556         if (fileid == null) {
557             eelfLogger.error("NODE0201 Rejecting bad URI for PUT or DELETE of " + req.getPathInfo() + FROM + req
558                     .getRemoteAddr());
559             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
560                     INVALID_REQUEST_URI);
561             eelfLogger.info(EelfMsgs.EXIT);
562             return null;
563         }
564         return fileid;
565     }
566
567     private boolean isAuthorizedToDelete(HttpServletResponse resp, String subscriptionId, String errorMessage)
568             throws IOException {
569         try {
570             boolean deletePermitted = config.isDeletePermitted(subscriptionId);
571             if (!deletePermitted) {
572                 eelfLogger.error("NODE0113 " + errorMessage + " Error: Subscription "
573                         + subscriptionId + " is not a privileged subscription");
574                 resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
575                 eelfLogger.info(EelfMsgs.EXIT);
576                 return false;
577             }
578         } catch (NullPointerException npe) {
579             eelfLogger.error("NODE0114 " + errorMessage + " Error: Subscription " + subscriptionId
580                     + " does not exist", npe);
581             resp.sendError(HttpServletResponse.SC_NOT_FOUND);
582             eelfLogger.info(EelfMsgs.EXIT);
583             return false;
584         }
585         return true;
586     }
587
588     private int getIdFromPath(HttpServletRequest req) {
589         String path = req.getPathInfo();
590         if (path == null || path.length() < 2) {
591             return -1;
592         }
593         try {
594             return Integer.parseInt(path.substring(1));
595         } catch (NumberFormatException e) {
596             return -1;
597         }
598     }
599 }