|
|
|
@ -33,16 +33,17 @@ import java.util.StringTokenizer;
|
|
|
|
|
public class PermissionManager {
|
|
|
|
|
|
|
|
|
|
private static PermissionManager permissionManager;
|
|
|
|
|
private static PermissionHolder rootNode;
|
|
|
|
|
private static PermissionTree permissionTree; // holds the permissions at runtime.
|
|
|
|
|
|
|
|
|
|
private PermissionManager(){};
|
|
|
|
|
private PermissionManager() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PermissionManager getInstance() {
|
|
|
|
|
if (permissionManager == null) {
|
|
|
|
|
synchronized (PermissionManager.class) {
|
|
|
|
|
if (permissionManager == null) {
|
|
|
|
|
permissionManager = new PermissionManager();
|
|
|
|
|
rootNode = new PermissionHolder("/"); // initializing the root node.
|
|
|
|
|
permissionTree = new PermissionTree();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -50,14 +51,7 @@ public class PermissionManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addPermission(Permission permission) throws DeviceManagementException {
|
|
|
|
|
StringTokenizer st = new StringTokenizer(permission.getUrl(), "/");
|
|
|
|
|
PermissionHolder tempRoot = rootNode;
|
|
|
|
|
PermissionHolder tempChild;
|
|
|
|
|
while(st.hasMoreTokens()) {
|
|
|
|
|
tempChild = new PermissionHolder(st.nextToken());
|
|
|
|
|
tempRoot = addPermissionNode(tempRoot, tempChild);
|
|
|
|
|
}
|
|
|
|
|
tempRoot.addPermission(permission.getMethod(), permission); //setting permission to the vertex
|
|
|
|
|
permissionTree.addPermission(permission); // adding a permission to the tree
|
|
|
|
|
try {
|
|
|
|
|
return PermissionUtils.putPermission(permission);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
@ -66,8 +60,8 @@ public class PermissionManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addPermissions(List<Permission> permissions) throws DeviceManagementException{
|
|
|
|
|
for(Permission permission:permissions){
|
|
|
|
|
public boolean addPermissions(List<Permission> permissions) throws DeviceManagementException {
|
|
|
|
|
for (Permission permission : permissions) {
|
|
|
|
|
this.addPermission(permission);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -75,13 +69,13 @@ public class PermissionManager {
|
|
|
|
|
|
|
|
|
|
public void initializePermissions(InputStream permissionStream) throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
if(permissionStream != null){
|
|
|
|
|
if (permissionStream != null) {
|
|
|
|
|
/* Un-marshaling Device Management configuration */
|
|
|
|
|
JAXBContext cdmContext = JAXBContext.newInstance(PermissionConfiguration.class);
|
|
|
|
|
Unmarshaller unmarshaller = cdmContext.createUnmarshaller();
|
|
|
|
|
PermissionConfiguration permissionConfiguration = (PermissionConfiguration)
|
|
|
|
|
unmarshaller.unmarshal(permissionStream);
|
|
|
|
|
if((permissionConfiguration != null) && (permissionConfiguration.getPermissions() != null)){
|
|
|
|
|
if (permissionConfiguration != null && permissionConfiguration.getPermissions() != null) {
|
|
|
|
|
this.addPermissions(permissionConfiguration.getPermissions());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -90,38 +84,7 @@ public class PermissionManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PermissionHolder addPermissionNode(PermissionHolder parent, PermissionHolder child) {
|
|
|
|
|
PermissionHolder existChild = parent.getChild(child.getPathName());
|
|
|
|
|
if (existChild == null) {
|
|
|
|
|
parent.addChild(child);
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
return existChild;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Permission getPermission(String url, String httpMethod) {
|
|
|
|
|
StringTokenizer st = new StringTokenizer(url, "/");
|
|
|
|
|
PermissionHolder tempRoot = rootNode;
|
|
|
|
|
PermissionHolder previousRoot;
|
|
|
|
|
while (st.hasMoreTokens()) {
|
|
|
|
|
String currentToken = st.nextToken();
|
|
|
|
|
previousRoot = tempRoot;
|
|
|
|
|
tempRoot = tempRoot.getChild(currentToken);
|
|
|
|
|
if (tempRoot == null) {
|
|
|
|
|
tempRoot = previousRoot;
|
|
|
|
|
int leftTokens = st.countTokens();
|
|
|
|
|
for (int i = 0; i <= leftTokens; i++) {
|
|
|
|
|
if (tempRoot == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
tempRoot = tempRoot.getChild("*");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (tempRoot == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return tempRoot.getPermission(httpMethod);
|
|
|
|
|
return permissionTree.getPermission(url, httpMethod);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|