|
|
|
@ -32,96 +32,59 @@ import java.util.StringTokenizer;
|
|
|
|
|
*/
|
|
|
|
|
public class PermissionManager {
|
|
|
|
|
|
|
|
|
|
private static PermissionManager permissionManager;
|
|
|
|
|
private static PermissionHolder rootNode;
|
|
|
|
|
private static PermissionManager permissionManager;
|
|
|
|
|
private static PermissionTree permissionTree; // holds the permissions at runtime.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return permissionManager;
|
|
|
|
|
}
|
|
|
|
|
private 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);
|
|
|
|
|
public static PermissionManager getInstance() {
|
|
|
|
|
if (permissionManager == null) {
|
|
|
|
|
synchronized (PermissionManager.class) {
|
|
|
|
|
if (permissionManager == null) {
|
|
|
|
|
permissionManager = new PermissionManager();
|
|
|
|
|
permissionTree = new PermissionTree();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tempRoot.addPermission(permission.getMethod(), permission); //setting permission to the vertex
|
|
|
|
|
try {
|
|
|
|
|
return PermissionUtils.putPermission(permission);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while adding the permission : " +
|
|
|
|
|
permission.getName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addPermissions(List<Permission> permissions) throws DeviceManagementException{
|
|
|
|
|
for(Permission permission:permissions){
|
|
|
|
|
this.addPermission(permission);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return permissionManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initializePermissions(InputStream permissionStream) throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
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)){
|
|
|
|
|
this.addPermissions(permissionConfiguration.getPermissions());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while initializing Data Source config", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public boolean addPermission(Permission permission) throws DeviceManagementException {
|
|
|
|
|
permissionTree.addPermission(permission); // adding a permission to the tree
|
|
|
|
|
try {
|
|
|
|
|
return PermissionUtils.putPermission(permission);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while adding the permission : " +
|
|
|
|
|
permission.getName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PermissionHolder addPermissionNode(PermissionHolder parent, PermissionHolder child) {
|
|
|
|
|
PermissionHolder existChild = parent.getChild(child.getPathName());
|
|
|
|
|
if (existChild == null) {
|
|
|
|
|
parent.addChild(child);
|
|
|
|
|
return child;
|
|
|
|
|
public boolean addPermissions(List<Permission> permissions) throws DeviceManagementException {
|
|
|
|
|
for (Permission permission : permissions) {
|
|
|
|
|
this.addPermission(permission);
|
|
|
|
|
}
|
|
|
|
|
return existChild;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("*");
|
|
|
|
|
public void initializePermissions(InputStream permissionStream) throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
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) {
|
|
|
|
|
this.addPermissions(permissionConfiguration.getPermissions());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while initializing Data Source config", e);
|
|
|
|
|
}
|
|
|
|
|
if (tempRoot == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return tempRoot.getPermission(httpMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Permission getPermission(String url, String httpMethod) {
|
|
|
|
|
return permissionTree.getPermission(url, httpMethod);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|