2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.resources;
24 import jakarta.ws.rs.container.ContainerRequestContext;
25 import jakarta.ws.rs.container.ContainerRequestFilter;
26 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
27 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
28 import org.onap.dmaap.dbcapi.service.ApiService;
29 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 public class AuthorizationFilter extends BaseLoggingClass implements ContainerRequestFilter {
35 private static final String AAF_CADI_FLAG = "enableCADI";
36 private final ResponseBuilder responseBuilder = new ResponseBuilder();
37 private final boolean isCadiEnabled;
40 public AuthorizationFilter() {
41 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
42 String flag = dmaapConfig.getProperty(AAF_CADI_FLAG, "false");
43 isCadiEnabled = "true".equalsIgnoreCase(flag);
47 public void filter(ContainerRequestContext requestContext) {
50 ApiService apiResp = new ApiService()
51 .setAuth(requestContext.getHeaderString("Authorization"))
52 .setUriPath(requestContext.getUriInfo().getPath())
53 .setHttpMethod(requestContext.getMethod())
54 .setRequestId(requestContext.getHeaderString("X-ECOMP-RequestID"));
57 apiResp.checkAuthorization();
58 } catch (AuthenticationErrorException ae) {
59 errorLogger.error("Error", ae);
60 requestContext.abortWith(responseBuilder.unauthorized(apiResp.getErr().getMessage()));
61 } catch (Exception e) {
62 errorLogger.error("Error", e);
63 requestContext.abortWith(responseBuilder.unavailable());