Remove major and minor code smells in dr-node
[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
21 package org.onap.dmaap.datarouter.node;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import java.io.IOException;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import org.onap.aaf.cadi.PropAccess;
33 import org.onap.aaf.cadi.filter.CadiFilter;
34
35
36 public class DRNodeCadiFilter extends CadiFilter {
37
38     private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeServlet.class);
39
40     DRNodeCadiFilter(boolean init, PropAccess access) throws ServletException {
41         super(init, access);
42     }
43
44     @Override
45     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
46             throws IOException, ServletException {
47         HttpServletRequest httpRequest = (HttpServletRequest) request;
48         String path = httpRequest.getPathInfo();
49         if (!(path.startsWith("/internal"))) {
50             if (!("POST".equalsIgnoreCase(httpRequest.getMethod()))) {
51                 if ("DELETE".equalsIgnoreCase(httpRequest.getMethod()) && path.startsWith("/delete")) {
52                     chain.doFilter(request, response);
53                 } else {
54                     doFilterWithFeedId(request, response, chain);
55                 }
56             }
57         } else {
58             chain.doFilter(request, response);
59         }
60     }
61
62     private String getFeedId(ServletRequest request, ServletResponse response) {
63         HttpServletRequest req = (HttpServletRequest) request;
64         HttpServletResponse resp = (HttpServletResponse) response;
65         String fileid = req.getPathInfo();
66         if (fileid == null) {
67             logger.error("NODE0105 Rejecting bad URI for PUT " + req.getPathInfo() + " from " + req.getRemoteAddr());
68             try {
69                 resp.sendError(HttpServletResponse.SC_NOT_FOUND,
70                         "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.");
71             } catch (IOException e) {
72                 logger.error("NODE0541 DRNodeCadiFilter.getFeedId: ", e);
73             }
74             return null;
75         }
76         String feedid = "";
77
78         if (fileid.startsWith("/publish/")) {
79             fileid = fileid.substring(9);
80             int index = fileid.indexOf('/');
81             if (index == -1 || index == fileid.length() - 1) {
82                 logger.error("NODE0105 Rejecting bad URI for PUT (publish) of " + req.getPathInfo() + " from " + req
83                         .getRemoteAddr());
84                 try {
85                     resp.sendError(HttpServletResponse.SC_NOT_FOUND,
86                             "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  "
87                                     + "Possible missing fileid.");
88                 } catch (IOException e) {
89                     logger.error("NODE0542 DRNodeCadiFilter.getFeedId: ", e);
90                 }
91                 return null;
92             }
93             feedid = fileid.substring(0, index);
94         }
95         return feedid;
96     }
97
98     private void doFilterWithFeedId(ServletRequest request, ServletResponse response, FilterChain chain)
99             throws IOException, ServletException {
100         String feedId = getFeedId(request, response);
101         String aafDbInstance = NodeConfigManager.getInstance().getAafInstance(feedId);
102         if (aafDbInstance != null && !"".equals(aafDbInstance) && !"legacy".equalsIgnoreCase(aafDbInstance)) {
103             logger.info("DRNodeCadiFilter - doFilter: FeedId - " + feedId + ":" + "AAF Instance -" + aafDbInstance);
104             super.doFilter(request, response, chain);
105         } else {
106             logger.info("DRNodeCadiFilter - doFilter: FeedId - " + feedId + ":" + "Legacy Feed");
107             chain.doFilter(request, response);
108         }
109     }
110 }