[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / resources / AuthorizationFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
23 import javax.ws.rs.container.ContainerRequestContext;
24 import javax.ws.rs.container.ContainerRequestFilter;
25
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;
30
31
32 @Authorization
33 public class AuthorizationFilter extends BaseLoggingClass implements ContainerRequestFilter   {
34
35         private static final String AAF_CADI_FLAG = "enableCADI";
36         private final ResponseBuilder responseBuilder = new ResponseBuilder();
37         private final boolean isCadiEnabled;
38
39
40         public AuthorizationFilter() {
41                 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
42                 String flag = dmaapConfig.getProperty(AAF_CADI_FLAG, "false");
43                 isCadiEnabled = "true".equalsIgnoreCase(flag);
44         }
45
46         @Override
47         public void filter(ContainerRequestContext requestContext) {
48
49                 if(!isCadiEnabled) {
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"));
55
56                         try {
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());
64                         }
65                 }
66         }
67
68 }