30ad1618d13458e19f2d7b50dc6222b1418252e3
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / DRNodeCadiFilter.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dmaap.datarouter.node;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aaf.cadi.PropAccess;
25 import org.onap.aaf.cadi.filter.CadiFilter;
26
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.IOException;
34
35
36 public class DRNodeCadiFilter extends CadiFilter {
37     private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeServlet.class);
38
39     DRNodeCadiFilter(boolean init, PropAccess access) throws ServletException {
40         super(init, access);
41     }
42
43     @Override
44     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
45         HttpServletRequest httpRequest = (HttpServletRequest) request;
46         String path = httpRequest.getPathInfo();
47         if (!(path.startsWith("/internal"))) {
48             if (!(httpRequest.getMethod().equalsIgnoreCase("POST"))) {
49                 if (httpRequest.getMethod().equalsIgnoreCase("DELETE") && path.startsWith("/delete")) {
50                     chain.doFilter(request, response);
51                 } else {
52                     String feedId = getFeedId(request, response);
53                     String aafDbInstance = NodeConfigManager.getInstance().getAafInstance(feedId);
54                     if (aafDbInstance != null && !aafDbInstance.equals("") && !aafDbInstance.equalsIgnoreCase("legacy")) {
55                         logger.info("DRNodeCadiFilter - doFilter: FeedId - " + feedId + ":" + "AAF Instance -" + aafDbInstance);
56                         super.doFilter(request, response, chain);
57                     } else {
58                         logger.info("DRNodeCadiFilter - doFilter: FeedId - " + feedId + ":" + "Legacy Feed");
59                         chain.doFilter(request, response);
60                     }
61                 }
62             }
63         } else {
64             chain.doFilter(request, response);
65         }
66     }
67
68     private String getFeedId(ServletRequest request, ServletResponse response) {
69         HttpServletRequest req = (HttpServletRequest) request;
70         HttpServletResponse resp = (HttpServletResponse) response;
71         String fileid = req.getPathInfo();
72         if (fileid == null) {
73             logger.error("NODE0105 Rejecting bad URI for PUT " + req.getPathInfo() + " from " + req.getRemoteAddr());
74             try {
75                 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
76             } catch (IOException e) {
77                 logger.error("NODE0541 DRNodeCadiFilter.getFeedId: ", e.getMessage());
78             }
79             return null;
80         }
81         String feedid = "";
82
83         if (fileid.startsWith("/publish/")) {
84             fileid = fileid.substring(9);
85             int i = fileid.indexOf('/');
86             if (i == -1 || i == fileid.length() - 1) {
87                 logger.error("NODE0105 Rejecting bad URI for PUT (publish) of " + req.getPathInfo() + " from " + req.getRemoteAddr());
88                 try {
89                     resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  Possible missing fileid.");
90                 } catch (IOException e) {
91                     logger.error("NODE0542 DRNodeCadiFilter.getFeedId: ", e.getMessage());
92                 }
93                 return null;
94             }
95             feedid = fileid.substring(0, i);
96         }
97         return feedid;
98     }
99
100 }