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;
23 import javax.ws.rs.container.ContainerRequestContext;
24 import javax.ws.rs.container.ContainerRequestFilter;
26 import org.apache.log4j.Logger;
27 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
28 import org.onap.dmaap.dbcapi.service.ApiService;
29 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 public class AuthorizationFilter implements ContainerRequestFilter {
35 private static final String AAF_FLAG = "UseAAF";
36 private final Logger logger = Logger.getLogger(AuthorizationFilter.class.getName());
37 private final ResponseBuilder responseBuilder = new ResponseBuilder();
38 private final boolean isAafEnabled;
41 public AuthorizationFilter() {
42 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
43 String flag = dmaapConfig.getProperty(AAF_FLAG, "false");
44 isAafEnabled = "true".equalsIgnoreCase(flag);
48 public void filter(ContainerRequestContext requestContext) {
51 ApiService apiResp = new ApiService()
52 .setAuth(requestContext.getHeaderString("Authorization"))
53 .setUriPath(requestContext.getUriInfo().getPath())
54 .setHttpMethod(requestContext.getMethod())
55 .setRequestId(requestContext.getHeaderString("X-ECOMP-RequestID"));
58 apiResp.checkAuthorization();
59 } catch (AuthenticationErrorException ae) {
60 logger.error("Error", ae);
61 requestContext.abortWith(responseBuilder.unauthorized(apiResp.getErr().getMessage()));
62 } catch (Exception e) {
63 logger.error("Error", e);
64 requestContext.abortWith(responseBuilder.unavailable());