CADI authentication and authorization filters
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / util / PermissionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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 package org.onap.dmaap.dbcapi.util;
21
22 import javax.servlet.http.HttpServletRequest;
23 import org.onap.dmaap.dbcapi.model.Dmaap;
24 import org.onap.dmaap.dbcapi.service.DmaapService;
25
26 public class PermissionBuilder {
27
28     static final String API_NS_PROP = "ApiNamespace";
29     static final String DEFAULT_API_NS = "org.onap.dmaap-bc.api";
30     static final String BOOT_INSTANCE = "boot";
31     private static final String PERM_SEPARATOR = "|";
32     private static final String NS_SEPARATOR = ".";
33     private DmaapConfig dmaapConfig;
34     private DmaapService dmaapService;
35     private String instance;
36     private String apiNamespace;
37
38     public PermissionBuilder(DmaapConfig dmaapConfig, DmaapService dmaapService) {
39         this.dmaapConfig = dmaapConfig;
40         this.dmaapService = dmaapService;
41         initFields();
42     }
43
44     public synchronized void updateDmaapInstance() {
45         if(instance == null || instance.isEmpty() || instance.equalsIgnoreCase(BOOT_INSTANCE)) {
46             String dmaapName = getDmaapName();
47             instance = (dmaapName == null || dmaapName.isEmpty()) ? BOOT_INSTANCE : dmaapName;
48         }
49     }
50
51     public String buildPermission(HttpServletRequest httpRequest) {
52
53         StringBuilder sb = new StringBuilder(apiNamespace);
54         sb.append(NS_SEPARATOR)
55             .append(getPermissionType(httpRequest.getPathInfo()))
56             .append(PERM_SEPARATOR)
57             .append(instance)
58             .append(PERM_SEPARATOR)
59             .append(httpRequest.getMethod());
60         return sb.toString();
61     }
62
63
64     private void initFields() {
65         apiNamespace = dmaapConfig.getProperty(API_NS_PROP, DEFAULT_API_NS);
66         updateDmaapInstance();
67     }
68
69     private String getDmaapName() {
70         Dmaap dmaap = dmaapService.getDmaap();
71         return ( dmaap != null ) ? dmaap.getDmaapName() : BOOT_INSTANCE;
72     }
73
74     private String getPermissionType(String pathInfo) {
75         char pathSeparator = '/';
76         String relativePath = (pathInfo.charAt(pathInfo.length()-1) == pathSeparator) ?
77             pathInfo.substring(0,pathInfo.length()-1) : pathInfo;
78
79         String[] pathSlices = relativePath.split(String.valueOf(pathSeparator));
80         return pathSlices[pathSlices.length-1];
81     }
82
83     String getInstance() {
84         return instance;
85     }
86 }