Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpServiceFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest;
23
24 import jakarta.servlet.Filter;
25 import jakarta.servlet.FilterChain;
26 import jakarta.servlet.ServletException;
27 import jakarta.servlet.ServletRequest;
28 import jakarta.servlet.ServletResponse;
29 import jakarta.servlet.http.HttpServletRequest;
30 import jakarta.servlet.http.HttpServletResponse;
31 import java.io.IOException;
32 import java.util.Set;
33 import java.util.concurrent.atomic.AtomicBoolean;
34
35 /**
36  * Filter that verifies that the API services (i.e., decision services) are enabled
37  * before allowing the request through.
38  */
39 public class XacmlPdpServiceFilter implements Filter {
40
41     /**
42      * Services the are always available, even when the API is disabled.
43      */
44     public static final Set<String> PERMANENT_SERVICES = Set.of("healthcheck", "statistics", "metrics");
45
46
47     private static final AtomicBoolean apiDisabled = new AtomicBoolean(true);
48
49
50     @Override
51     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
52                     throws IOException, ServletException {
53
54         HttpServletRequest request = (HttpServletRequest) servletRequest;
55         HttpServletResponse response = (HttpServletResponse) servletResponse;
56
57         if (apiDisabled.get() && !PERMANENT_SERVICES.contains(getUriSuffix(request))) {
58             response.setStatus(HttpServletResponse.SC_CONFLICT);
59         } else {
60             filterChain.doFilter(servletRequest, servletResponse);
61         }
62     }
63
64     private String getUriSuffix(HttpServletRequest request) {
65         String uri = request.getRequestURI();
66         int index = uri.lastIndexOf('/');
67         return (index < 0 ? uri : uri.substring(index + 1));
68     }
69
70     /**
71      * Determines if API services are enabled.
72      *
73      * @return {@code true}, if API services are enabled
74      */
75     public static boolean isApiEnabled() {
76         return !apiDisabled.get();
77     }
78
79     /**
80      * Enables the API services.
81      */
82     public static void enableApi() {
83         apiDisabled.set(false);
84     }
85
86     /**
87      * Disables the API services.
88      */
89     public static void disableApi() {
90         apiDisabled.set(true);
91     }
92 }