2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pdpx.main.rest;
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;
33 import java.util.concurrent.atomic.AtomicBoolean;
36 * Filter that verifies that the API services (i.e., decision services) are enabled
37 * before allowing the request through.
39 public class XacmlPdpServiceFilter implements Filter {
42 * Services the are always available, even when the API is disabled.
44 public static final Set<String> PERMANENT_SERVICES = Set.of("healthcheck", "statistics", "metrics");
47 private static final AtomicBoolean apiDisabled = new AtomicBoolean(true);
51 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
52 throws IOException, ServletException {
54 HttpServletRequest request = (HttpServletRequest) servletRequest;
55 HttpServletResponse response = (HttpServletResponse) servletResponse;
57 if (apiDisabled.get() && !PERMANENT_SERVICES.contains(getUriSuffix(request))) {
58 response.setStatus(HttpServletResponse.SC_CONFLICT);
60 filterChain.doFilter(servletRequest, servletResponse);
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));
71 * Determines if API services are enabled.
73 * @return {@code true}, if API services are enabled
75 public static boolean isApiEnabled() {
76 return !apiDisabled.get();
80 * Enables the API services.
82 public static void enableApi() {
83 apiDisabled.set(false);
87 * Disables the API services.
89 public static void disableApi() {
90 apiDisabled.set(true);