LIST, RENAME, COPY, DELETE, EDITFILE, ADDFOLDER, DESCRIBEPOLICYFILE, VIEWPOLICY, ADDSUBSCOPE, SWITCHVERSION, EXPORT, SEARCHLIST
}
+ private PolicyController policyController;
+ public PolicyController getPolicyController() {
+ return policyController;
+ }
+
+ public void setPolicyController(PolicyController policyController) {
+ this.policyController = policyController;
+ }
+
private static String CONTENTTYPE = "application/json";
private static String SUPERADMIN = "super-admin";
private static String SUPEREDITOR = "super-editor";
private static Path closedLoopJsonLocation;
private static JsonArray policyNames;
+ private String testUserId = null;
public static JsonArray getPolicyNames() {
return policyNames;
closedLoopJsonLocation = Paths.get(XACMLProperties
.getProperty(XACMLRestProperties.PROP_ADMIN_CLOSEDLOOP));
FileInputStream inputStream = null;
+ JsonReader jsonReader = null;
String location = closedLoopJsonLocation.toString();
try {
inputStream = new FileInputStream(location);
- if (location.endsWith("json")) {
- JsonReader jsonReader = null;
+ if (location.endsWith("json")) {
jsonReader = Json.createReader(inputStream);
policyNames = jsonReader.readArray();
serviceTypeNamesList = new ArrayList<>();
String name = policyName.getJsonString("serviceTypePolicyName").getString();
serviceTypeNamesList.add(name);
}
- jsonReader.close();
}
} catch (FileNotFoundException e) {
LOGGER.error("Exception Occured while initializing the JSONConfig file"+e);
}finally{
try {
- inputStream.close();
+ if(inputStream != null){
+ inputStream.close();
+ }
+ if(jsonReader != null){
+ jsonReader.close();
+ }
} catch (IOException e) {
LOGGER.error("Exception Occured while closing the File InputStream"+e);
}
// Process form file field (input type="file").
files.put(item.getName(), item.getInputStream());
if(item.getName().endsWith(".xls")){
+ OutputStream outputStream = null;
try{
File file = new File(item.getName());
- OutputStream outputStream = new FileOutputStream(file);
+ outputStream = new FileOutputStream(file);
IOUtils.copy(item.getInputStream(), outputStream);
outputStream.close();
newFile = file.toString();
importController.importRepositoryFile(newFile, request);
}catch(Exception e){
LOGGER.error("Upload error : " + e);
+ }finally{
+ if(outputStream != null){
+ outputStream.close();
+ }
}
}
}
if(params.has("policyList")){
policyList = (JSONArray) params.get("policyList");
}
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
List<JSONObject> resultList = new ArrayList<>();
try {
//Get the Login Id of the User from Request
String userId = UserUtils.getUserSession(request).getOrgUserId();
//Check if the Role and Scope Size are Null get the values from db.
- List<Object> userRoles = PolicyController.getRoles(userId);
+ List<Object> userRoles = controller.getRoles(userId);
roles = new ArrayList<>();
scopes = new HashSet<>();
for(Object role: userRoles){
if(scopes.isEmpty()){
return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
}
+ Set<String> tempScopes = scopes;
+ for(String scope : tempScopes){
+ List<Object> scopesList = queryPolicyEditorScopes(scope);
+ if(!scopesList.isEmpty()){
+ for(int i = 0; i < scopesList.size(); i++){
+ PolicyEditorScopes tempScope = (PolicyEditorScopes) scopesList.get(i);
+ scopes.add(tempScope.getScopeName());
+ }
+ }
+ }
}
if(policyList!= null){
for(int i = 0; i < policyList.length(); i++){
List<Object> filterdatas = controller.getData(PolicyVersion.class);
for(Object filter : filterdatas){
PolicyVersion filterdata = (PolicyVersion) filter;
- String scopeName = filterdata.getPolicyName().substring(0, filterdata.getPolicyName().lastIndexOf(File.separator));
- if(scopes.contains(scopeName)){
- policyData.add(filterdata);
+ try{
+ String scopeName = filterdata.getPolicyName().substring(0, filterdata.getPolicyName().lastIndexOf(File.separator));
+ if(scopes.contains(scopeName)){
+ policyData.add(filterdata);
+ }
+ }catch(Exception e){
+ LOGGER.error("Exception occured while filtering policyversion data"+e);
}
}
}
}
String activePolicy = null;
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
if(params.toString().contains("activeVersion")){
String activeVersion = params.getString("activeVersion");
String highestVersion = params.get("highestVersion").toString();
path = path.replace("/", ".");
}else{
path = path.replace("/", ".");
+ policyName = path;
}
if(path.contains("Config_")){
path = path.replace(".Config_", ":Config_");
}else if(path.contains("Decision_")){
path = path.replace(".Decision_", ":Decision_");
}
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String[] split = path.split(":");
String query = "FROM PolicyEntity where policyName = '"+split[1]+"' and scope ='"+split[0]+"'";
List<Object> queryData = controller.getDataByQuery(query);
if(!queryData.isEmpty()){
PolicyEntity entity = (PolicyEntity) queryData.get(0);
File temp = null;
+ BufferedWriter bw = null;
try {
temp = File.createTempFile(policyName, ".tmp");
- BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
+ bw = new BufferedWriter(new FileWriter(temp));
bw.write(entity.getPolicyData());
bw.close();
object = HumanPolicyComponent.DescribePolicy(temp);
if(temp != null){
temp.delete();
}
+ if(bw != null){
+ try {
+ bw.close();
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured while Closing the File Writer"+e);
+ }
+ }
}
}else{
return error("Error Occured while Describing the Policy");
Set<String> scopes = null;
List<String> roles = null;
try {
+ PolicyController controller = getPolicyControllerInstance();
//Get the Login Id of the User from Request
- String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String testUserID = getTestUserId();
+ String userId = testUserID != null ? testUserID : UserUtils.getUserSession(request).getOrgUserId();
//Check if the Role and Scope Size are Null get the values from db.
- List<Object> userRoles = PolicyController.getRoles(userId);
+ List<Object> userRoles = controller.getRoles(userId);
roles = new ArrayList<>();
scopes = new HashSet<>();
for(Object role: userRoles){
}
}
}
- if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST) ) {
- if(scopes.isEmpty()){
- return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
- }
- }
List<JSONObject> resultList = new ArrayList<>();
boolean onlyFolders = params.getBoolean("onlyFolders");
path = path.replaceAll("..xml", "").trim();
}
-
+ if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST) ) {
+ if(scopes.isEmpty()){
+ return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
+ }else{
+ if(!"/".equals(path)){
+ String tempScope = path.substring(1, path.length());
+ tempScope = tempScope.replace("/", File.separator);
+ scopes.add(tempScope);
+ }
+ }
+ }
+
if("/".equals(path)){
if(roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)){
List<Object> scopesList = queryPolicyEditorScopes(null);
for(Object scope : scopes){
JSONObject el = new JSONObject();
List<Object> scopesList = queryPolicyEditorScopes(scope.toString());
- PolicyEditorScopes scopeById = (PolicyEditorScopes) scopesList.get(0);
- el.put("name", scopeById.getScopeName());
- el.put("date", scopeById.getCreatedDate());
- el.put("size", "");
- el.put("type", "dir");
- el.put("createdBy", scopeById.getUserCreatedBy().getUserName());
- el.put("modifiedBy", scopeById.getUserModifiedBy().getUserName());
- resultList.add(el);
+ if(!scopesList.isEmpty()){
+ PolicyEditorScopes scopeById = (PolicyEditorScopes) scopesList.get(0);
+ el.put("name", scopeById.getScopeName());
+ el.put("date", scopeById.getCreatedDate());
+ el.put("size", "");
+ el.put("type", "dir");
+ el.put("createdBy", scopeById.getUserCreatedBy().getUserName());
+ el.put("modifiedBy", scopeById.getUserModifiedBy().getUserName());
+ resultList.add(el);
+ }
}
}
}else{
if(scopeName == null){
scopeNamequery = "from PolicyEditorScopes";
}else{
- scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"'";
+ scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"%'";
}
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
List<Object> scopesList = controller.getDataByQuery(scopeNamequery);
return scopesList;
}
//Get Active Policy List based on Scope Selection form Policy Version table
private void activePolicyList(String scopeName, List<JSONObject> resultList, List<String> roles, Set<String> scopes, boolean onlyFolders){
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
if(scopeName.contains("/")){
scopeName = scopeName.replace("/", File.separator);
}
if(scopeName.contains("\\")){
scopeName = scopeName.replace("\\", "\\\\\\\\");
}
- String query = "from PolicyVersion where POLICY_NAME like'" +scopeName+"%'";
- String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"%'";
+ String query = "from PolicyVersion where POLICY_NAME like '" +scopeName+"%'";
+ String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like '" +scopeName+"%'";
List<Object> activePolicies = controller.getDataByQuery(query);
List<Object> scopesList = controller.getDataByQuery(scopeNamequery);
for(Object list : scopesList){
}
private String getUserName(String loginId){
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
UserInfo userInfo = (UserInfo) controller.getEntityItem(UserInfo.class, "userLoginId", loginId);
if(userInfo == null){
return SUPERADMIN;
scopeName = scopeName.replace("\\", "\\\\\\\\");
newScopeName = newScopeName.replace("\\", "\\\\\\\\");
}
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String query = "from PolicyVersion where POLICY_NAME like'" +scopeName+"%'";
String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"%'";
List<Object> activePolicies = controller.getDataByQuery(query);
private JSONObject policyRename(String oldPath, String newPath, String userId) throws ServletException {
try {
PolicyEntity entity = null;
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String policyVersionName = newPath.replace(".xml", "");
String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf(".")).replace("/", File.separator);
try {
ConfigurationDataEntity configEntity = entity.getConfigurationData();
ActionBodyEntity actionEntity = entity.getActionBodyEntity();
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String oldPolicyNameWithoutExtension = removeoldPolicyExtension;
String newPolicyNameWithoutExtension = removenewPolicyExtension;
configEntity.setConfigurationName(configEntity.getConfigurationName().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension));
controller.updateData(configEntity);
String newConfigurationName = configEntity.getConfigurationName();
- File file = new File(PolicyController.configHome + File.separator + oldConfigurationName);
+ File file = new File(PolicyController.getConfigHome() + File.separator + oldConfigurationName);
if(file.exists()){
- File renamefile = new File(PolicyController.configHome + File.separator + newConfigurationName);
+ File renamefile = new File(PolicyController.getConfigHome() + File.separator + newConfigurationName);
file.renameTo(renamefile);
}
}else if(newpolicyName.contains("Action_")){
actionEntity.setActionBody(actionEntity.getActionBody().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension));
controller.updateData(actionEntity);
String newConfigurationName = actionEntity.getActionBodyName();
- File file = new File(PolicyController.actionHome + File.separator + oldConfigurationName);
+ File file = new File(PolicyController.getActionHome() + File.separator + oldConfigurationName);
if(file.exists()){
- File renamefile = new File(PolicyController.actionHome + File.separator + newConfigurationName);
+ File renamefile = new File(PolicyController.getActionHome() + File.separator + newConfigurationName);
file.renameTo(renamefile);
}
}
}
private JSONObject cloneRecord(String newpolicyName, String oldScope, String removeoldPolicyExtension, String newScope, String removenewPolicyExtension, PolicyEntity entity, String userId) throws ServletException{
+ FileWriter fw = null;
String queryEntityName = null;
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
PolicyEntity cloneEntity = new PolicyEntity();
cloneEntity.setPolicyName(newpolicyName);
removeoldPolicyExtension = removeoldPolicyExtension.replace(".xml", "");
controller.saveData(configurationDataEntity);
ConfigurationDataEntity configEntiy = (ConfigurationDataEntity) controller.getEntityItem(ConfigurationDataEntity.class, "configurationName", queryEntityName);
cloneEntity.setConfigurationData(configEntiy);
+ String newConfigurationName = configEntiy.getConfigurationName();
+ try {
+ fw = new FileWriter(PolicyController.getConfigHome() + File.separator + newConfigurationName);
+ BufferedWriter bw = new BufferedWriter(fw);
+ bw.write(configEntiy.getConfigBody());
+ bw.close();
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured While cloning the configuration file"+e);
+ }
}else if(newpolicyName.contains("Action_")){
ActionBodyEntity actionBodyEntity = new ActionBodyEntity();
actionBodyEntity.setActionBodyName(entity.getActionBodyEntity().getActionBodyName().replace(oldScope+"."+oldConfigRemoveExtension, newScope+"."+newConfigRemoveExtension));
controller.saveData(actionBodyEntity);
ActionBodyEntity actionEntiy = (ActionBodyEntity) controller.getEntityItem(ActionBodyEntity.class, "actionBodyName", queryEntityName);
cloneEntity.setActionBodyEntity(actionEntiy);
+ String newConfigurationName = actionEntiy.getActionBodyName();
+ try {
+ fw = new FileWriter(PolicyController.getActionHome() + File.separator + newConfigurationName);
+ BufferedWriter bw = new BufferedWriter(fw);
+ bw.write(actionEntiy.getActionBody());
+ bw.close();
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured While cloning the configuration file"+e);
+ }
+ }
+ if(fw != null){
+ try {
+ fw.close();
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured While closing the File input stream"+e);
+ }
}
cloneEntity.setDeleted(entity.isDeleted());
cloneEntity.setCreatedBy(userId);
}
String[] oldPolicySplit = oldPolicyCheck.split(":");
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
PolicyEntity entity = null;
boolean success = false;
//Check PolicyEntity table with newPolicy Name
String policyEntityquery = "FROM PolicyEntity where policyName = '"+newPolicySplit[1]+"' and scope ='"+newPolicySplit[0]+"'";
List<Object> queryData = controller.getDataByQuery(policyEntityquery);
+ if(!queryData.isEmpty()){
+ return error("Policy already exists with same name");
+ }
+
+ //Query the Policy Entity with oldPolicy Name
+ policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
+ queryData = controller.getDataByQuery(policyEntityquery);
if(!queryData.isEmpty()){
entity = (PolicyEntity) queryData.get(0);
}
if(entity != null){
- //if a policy exists with new name check if it is deleted or not
- if(entity.isDeleted()){
- //Check Policy Group Entity table if policy has been pushed or not
- String query = "from PolicyGroupEntity where policyid = '"+entity.getPolicyId()+"'";
- List<Object> object = controller.getDataByQuery(query);
- if(object == null){
- //if PolicyGroupEntity data is empty delete the entry from database
- controller.deleteData(entity);
- //Query the Policy Entity with oldPolicy Name
- policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
- queryData = controller.getDataByQuery(policyEntityquery);
- if(!queryData.isEmpty()){
- entity = (PolicyEntity) queryData.get(0);
- }
- if(entity != null){
- cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], newPolicySplit[0], newPolicySplit[1], entity, userId);
- success = true;
- }
- }else{
- return error("Policy Clone failed due to policy with new name existing in PDP Group.");
- }
- }else{
- return error("Policy Clone failed due to same name existing.");
- }
- }else{
- //Query the Policy Entity with oldPolicy Name
- policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
- queryData = controller.getDataByQuery(policyEntityquery);
- if(!queryData.isEmpty()){
- entity = (PolicyEntity) queryData.get(0);
- }
- if(entity != null){
- cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], newPolicySplit[0], newPolicySplit[1], entity, userId);
- success = true;
- }
+ cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], newPolicySplit[0], newPolicySplit[1], entity, userId);
+ success = true;
}
+
if(success){
PolicyVersion entityItem = new PolicyVersion();
entityItem.setActiveVersion(Integer.parseInt(version));
//Delete Policy or Scope Functionality
private JSONObject delete(JSONObject params, HttpServletRequest request) throws ServletException {
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
PolicyRestController restController = new PolicyRestController();
PolicyEntity policyEntity = null;
String policyNamewithoutExtension;
private JSONObject editFile(JSONObject params) throws ServletException {
// get content
try {
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String mode = params.getString("mode");
String path = params.getString("path");
LOGGER.debug("editFile path: {}"+ path);
//Add Scopes
private JSONObject addFolder(JSONObject params, HttpServletRequest request) throws ServletException {
- PolicyController controller = new PolicyController();
+ PolicyController controller = getPolicyControllerInstance();
String name = "";
try {
String userId = UserUtils.getUserSession(request).getOrgUserId();
}
LOGGER.debug("addFolder path: {} name: {}" + path +name);
if(!name.equals("")){
+ if(name.startsWith(File.separator)){
+ name = name.substring(1);
+ }
PolicyEditorScopes entity = (PolicyEditorScopes) controller.getEntityItem(PolicyEditorScopes.class, "scopeName", name);
if(entity == null){
UserInfo userInfo = new UserInfo();
userInfo.setUserLoginId(userId);
PolicyEditorScopes newScope = new PolicyEditorScopes();
- String scopeName = null;
- if(name.startsWith(File.separator)){
- scopeName = name.substring(1);
- }else{
- scopeName = name;
- }
- newScope.setScopeName(scopeName);
+ newScope.setScopeName(name);
newScope.setUserCreatedBy(userInfo);
newScope.setUserModifiedBy(userInfo);
controller.saveData(newScope);
throw new ServletException(e);
}
}
+
+ private PolicyController getPolicyControllerInstance(){
+ return policyController != null ? getPolicyController() : new PolicyController();
+ }
+
+ public String getTestUserId() {
+ return testUserId;
+ }
+
+ public void setTestUserId(String testUserId) {
+ this.testUserId = testUserId;
+ }
}
\ No newline at end of file