2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.dmaap.dbcapi.util;
22 import javax.servlet.http.HttpServletRequest;
23 import org.onap.dmaap.dbcapi.model.Dmaap;
24 import org.onap.dmaap.dbcapi.service.DmaapService;
26 public class PermissionBuilder {
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;
38 public PermissionBuilder(DmaapConfig dmaapConfig, DmaapService dmaapService) {
39 this.dmaapConfig = dmaapConfig;
40 this.dmaapService = dmaapService;
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;
51 public String buildPermission(HttpServletRequest httpRequest) {
53 StringBuilder sb = new StringBuilder(apiNamespace);
54 sb.append(NS_SEPARATOR)
55 .append(getPermissionType(httpRequest.getPathInfo()))
56 .append(PERM_SEPARATOR)
58 .append(PERM_SEPARATOR)
59 .append(httpRequest.getMethod());
64 private void initFields() {
65 apiNamespace = dmaapConfig.getProperty(API_NS_PROP, DEFAULT_API_NS);
66 updateDmaapInstance();
69 private String getDmaapName() {
70 Dmaap dmaap = dmaapService.getDmaap();
71 return ( dmaap != null ) ? dmaap.getDmaapName() : BOOT_INSTANCE;
74 private String getPermissionType(String pathInfo) {
75 char pathSeparator = '/';
76 String[] pathSlices = pathInfo.split(String.valueOf(pathSeparator));
80 String getInstance() {