* ============LICENSE_START=======================================================
* ONAP Policy Engine
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.onap.policy.components.HumanPolicyComponent;
import org.onap.policy.controller.PolicyController;
import org.onap.policy.controller.PolicyExportAndImportController;
-import org.onap.policy.model.Roles;
import org.onap.policy.rest.XACMLRest;
import org.onap.policy.rest.XACMLRestProperties;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.jpa.PolicyVersion;
import org.onap.policy.rest.jpa.UserInfo;
import org.onap.policy.utils.PolicyUtils;
+import org.onap.policy.utils.UserUtils.Pair;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.util.XACMLPolicyScanner;
import org.onap.portalsdk.core.web.support.UserUtils;
return policyController;
}
- public synchronized static void setPolicyController(PolicyController policyController) {
+ public static synchronized void setPolicyController(PolicyController policyController) {
PolicyManagerServlet.policyController = policyController;
}
protected static void initializeJSONLoad() {
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 = Json.createReader(inputStream);
- policyNames = jsonReader.readArray();
- serviceTypeNamesList = new ArrayList<>();
- for (int i = 0; i < policyNames.size(); i++) {
- javax.json.JsonObject policyName = policyNames.getJsonObject(i);
- String name = policyName.getJsonString("serviceTypePolicyName").getString();
- serviceTypeNamesList.add(name);
- }
+ if (! location.endsWith("json")) {
+ LOGGER.warn("JSONConfig file does not end with extension .json");
+ return;
+ }
+ try (FileInputStream inputStream = new FileInputStream(location);
+ JsonReader jsonReader = Json.createReader(inputStream)) {
+ policyNames = jsonReader.readArray();
+ serviceTypeNamesList = new ArrayList<>();
+ for (int i = 0; i < policyNames.size(); i++) {
+ javax.json.JsonObject policyName = policyNames.getJsonObject(i);
+ String name = policyName.getJsonString("serviceTypePolicyName").getString();
+ serviceTypeNamesList.add(name);
}
- } catch (FileNotFoundException e) {
+ } catch (IOException e) {
LOGGER.error("Exception Occured while initializing the JSONConfig file"+e);
- }finally{
- try {
- if(inputStream != null){
- inputStream.close();
- }
- if(jsonReader != null){
- jsonReader.close();
- }
- } catch (IOException e) {
- LOGGER.error("Exception Occured while closing the File InputStream"+e);
- }
}
}
if (!item.isFormField()) {
// 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 = new FileOutputStream(file);
+ if(item.getName().endsWith(".xls") && item.getSize() <= PolicyController.getFileSizeLimit()){
+ File file = new File(item.getName());
+ try (OutputStream outputStream = new FileOutputStream(file);)
+ {
IOUtils.copy(item.getInputStream(), outputStream);
- outputStream.close();
newFile = file.toString();
PolicyExportAndImportController importController = new PolicyExportAndImportController();
importController.importRepositoryFile(newFile, request);
}catch(Exception e){
LOGGER.error("Upload error : " + e);
- }finally{
- if(outputStream != null){
- outputStream.close();
- }
}
}
+ else if (!item.getName().endsWith(".xls")) {
+ LOGGER.error("Non .xls filetype uploaded: " + item.getName());
+ }
+ else { //uploaded file size is greater than allowed
+ LOGGER.error("Upload file size limit exceeded! File size (Bytes) is: " + item.getSize());
+ }
}
}
- JSONObject responseJsonObject = null;
+ JSONObject responseJsonObject;
responseJsonObject = this.success();
response.setContentType(CONTENTTYPE);
PrintWriter out = response.getWriter();
}
private JSONObject searchPolicyList(JSONObject params, HttpServletRequest request) {
- Set<String> scopes = null;
- List<String> roles = null;
+ Set<String> scopes;
+ List<String> roles;
List<Object> policyData = new ArrayList<>();
JSONArray policyList = null;
if(params.has("policyList")){
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 = controller.getRoles(userId);
- roles = new ArrayList<>();
- scopes = new HashSet<>();
- for(Object role: userRoles){
- Roles userRole = (Roles) role;
- roles.add(userRole.getRole());
- if(userRole.getScope() != null){
- if(userRole.getScope().contains(",")){
- String[] multipleScopes = userRole.getScope().split(",");
- for(int i =0; i < multipleScopes.length; i++){
- scopes.add(multipleScopes[i]);
- }
- }else{
- scopes.add(userRole.getScope());
- }
- }
- }
+ Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ roles = pair.u;
+ scopes = pair.t;
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");
if(policyList!= null){
for(int i = 0; i < policyList.length(); i++){
String policyName = policyList.get(i).toString().replace(".xml", "");
- String version = policyName.substring(policyName.lastIndexOf(".")+1);
- policyName = policyName.substring(0, policyName.lastIndexOf(".")).replace(".", File.separator);
+ String version = policyName.substring(policyName.lastIndexOf('.')+1);
+ policyName = policyName.substring(0, policyName.lastIndexOf('.')).replace(".", File.separator);
if(policyName.contains("\\")){
policyName = policyName.replace("\\", "\\\\");
}
String policyName;
String removeExtension = path.replace(".xml", "");
if(path.startsWith("/")){
- policyName = removeExtension.substring(1, removeExtension.lastIndexOf("."));
+ policyName = removeExtension.substring(1, removeExtension.lastIndexOf('.'));
}else{
- policyName = removeExtension.substring(0, removeExtension.lastIndexOf("."));
+ policyName = removeExtension.substring(0, removeExtension.lastIndexOf('.'));
}
- String activePolicy = null;
+ String activePolicy;
PolicyController controller = getPolicyControllerInstance();
- if(params.toString().contains("activeVersion")){
- String activeVersion = params.getString("activeVersion");
- String highestVersion = params.get("highestVersion").toString();
- if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){
- return error("The Version shouldn't be greater than Highest Value");
- }else{
- activePolicy = policyName + "." + activeVersion + ".xml";
- String dbCheckName = activePolicy.replace("/", ".");
- if(dbCheckName.contains("Config_")){
- dbCheckName = dbCheckName.replace(".Config_", ":Config_");
- }else if(dbCheckName.contains("Action_")){
- dbCheckName = dbCheckName.replace(".Action_", ":Action_");
- }else if(dbCheckName.contains("Decision_")){
- dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
- }
- String[] splitDBCheckName = dbCheckName.split(":");
- String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0";
- SimpleBindings policyParams = new SimpleBindings();
- policyParams.put("splitDBCheckName_1", splitDBCheckName[1]);
- policyParams.put("splitDBCheckName_0", splitDBCheckName[0]);
- List<Object> policyEntity = controller.getDataByQuery(peQuery, policyParams);
- PolicyEntity pentity = (PolicyEntity) policyEntity.get(0);
- if(pentity.isDeleted()){
- return error("The Policy is Not Existing in Workspace");
- }else{
- if(policyName.contains("/")){
- policyName = policyName.replace("/", File.separator);
- }
- policyName = policyName.substring(policyName.indexOf(File.separator)+1);
- if(policyName.contains("\\")){
- policyName = policyName.replace(File.separator, "\\");
- }
- policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName;
- String watchPolicyName = policyName;
- if(policyName.contains("/")){
- policyName = policyName.replace("/", File.separator);
- }
- if(policyName.contains("\\")){
- policyName = policyName.replace("\\", "\\\\");
- }
- String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0";
- //query the database
- controller.executeQuery(query);
- //Policy Notification
- PolicyVersion entity = new PolicyVersion();
- entity.setPolicyName(watchPolicyName);
- entity.setActiveVersion(Integer.parseInt(activeVersion));
- entity.setModifiedBy(userId);
- controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion");
- return success();
- }
- }
+ if(! params.toString().contains("activeVersion")){
+ return controller.switchVersionPolicyContent(policyName);
+ }
+ String activeVersion = params.getString("activeVersion");
+ String highestVersion = params.get("highestVersion").toString();
+ if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){
+ return error("The Version shouldn't be greater than Highest Value");
+ }
+ activePolicy = policyName + "." + activeVersion + ".xml";
+ String dbCheckName = activePolicy.replace("/", ".");
+ if(dbCheckName.contains("Config_")){
+ dbCheckName = dbCheckName.replace(".Config_", ":Config_");
+ }else if(dbCheckName.contains("Action_")){
+ dbCheckName = dbCheckName.replace(".Action_", ":Action_");
+ }else if(dbCheckName.contains("Decision_")){
+ dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
+ }
+ String[] splitDBCheckName = dbCheckName.split(":");
+ String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0";
+ SimpleBindings policyParams = new SimpleBindings();
+ policyParams.put("splitDBCheckName_1", splitDBCheckName[1]);
+ policyParams.put("splitDBCheckName_0", splitDBCheckName[0]);
+ List<Object> policyEntity = controller.getDataByQuery(peQuery, policyParams);
+ PolicyEntity pentity = (PolicyEntity) policyEntity.get(0);
+ if(pentity.isDeleted()){
+ return error("The Policy is Not Existing in Workspace");
}
- return controller.switchVersionPolicyContent(policyName);
+ if(policyName.contains("/")){
+ policyName = policyName.replace("/", File.separator);
+ }
+ policyName = policyName.substring(policyName.indexOf(File.separator)+1);
+ if(policyName.contains("\\")){
+ policyName = policyName.replace(File.separator, "\\");
+ }
+ policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName;
+ String watchPolicyName = policyName;
+ if(policyName.contains("/")){
+ policyName = policyName.replace("/", File.separator);
+ }
+ if(policyName.contains("\\")){
+ policyName = policyName.replace("\\", "\\\\");
+ }
+ String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0";
+ //query the database
+ controller.executeQuery(query);
+ //Policy Notification
+ PolicyVersion entity = new PolicyVersion();
+ entity.setPolicyName(watchPolicyName);
+ entity.setActiveVersion(Integer.parseInt(activeVersion));
+ entity.setModifiedBy(userId);
+ controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion");
+ return success();
}
//Describe Policy
String policyName = null;
if(path.startsWith("/")){
path = path.substring(1);
- policyName = path.substring(path.lastIndexOf("/") +1);
+ policyName = path.substring(path.lastIndexOf('/') +1);
path = path.replace("/", ".");
}else{
path = path.replace("/", ".");
}else{
queryData = controller.getDataByQuery(query, peParams);
}
- if(!queryData.isEmpty()){
- PolicyEntity entity = (PolicyEntity) queryData.get(0);
- File temp = null;
- BufferedWriter bw = null;
- try {
- temp = File.createTempFile(policyName, ".tmp");
- bw = new BufferedWriter(new FileWriter(temp));
- bw.write(entity.getPolicyData());
- bw.close();
- object = HumanPolicyComponent.DescribePolicy(temp);
- } catch (IOException e) {
- LOGGER.error("Exception Occured while Describing the Policy"+e);
- }finally{
- if(temp != null){
- temp.delete();
- }
- if(bw != null){
- try {
- bw.close();
- } catch (IOException e) {
- LOGGER.error("Exception Occured while Closing the File Writer"+e);
- }
+ if(queryData.isEmpty()){
+ return error("Error Occured while Describing the Policy - query is empty");
+ }
+ PolicyEntity entity = (PolicyEntity) queryData.get(0);
+ File temp = null;
+ try {
+ temp = File.createTempFile(policyName, ".tmp");
+ } catch (IOException e) {
+ String message = "Failed to create temp file " + policyName + ".tmp";
+ LOGGER.error(message + e);
+ return error(message);
+ }
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(temp))) {
+ bw.write(entity.getPolicyData());
+ object = HumanPolicyComponent.DescribePolicy(temp);
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured while Describing the Policy"+e);
+ }finally{
+ if(temp != null){
+ try {
+ Files.delete(temp.toPath());
+ } catch (IOException e) {
+ LOGGER.warn("Failed to delete " + temp.getName() + e);
}
}
- }else{
- return error("Error Occured while Describing the Policy");
}
-
return object;
}
//Get the Login Id of the User from Request
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 = controller.getRoles(userId);
- roles = new ArrayList<>();
- scopes = new HashSet<>();
- for(Object role: userRoles){
- Roles userRole = (Roles) role;
- roles.add(userRole.getRole());
- if(userRole.getScope() != null){
- if(userRole.getScope().contains(",")){
- String[] multipleScopes = userRole.getScope().split(",");
- for(int i =0; i < multipleScopes.length; i++){
- scopes.add(multipleScopes[i]);
- }
- }else{
- scopes.add(userRole.getScope());
- }
- }
- }
+ Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ roles = pair.u;
+ scopes = pair.t;
List<JSONObject> resultList = new ArrayList<>();
boolean onlyFolders = params.getBoolean("onlyFolders");
}
}else{
try{
- String scopeName = path.substring(path.indexOf("/") +1);
+ String scopeName = path.substring(path.indexOf('/') +1);
activePolicyList(scopeName, resultList, roles, scopes, onlyFolders);
} catch (Exception ex) {
LOGGER.error("Error Occured While reading Policy Files List"+ex );
}
private List<Object> queryPolicyEditorScopes(String scopeName){
- String scopeNamequery = "";
+ String scopeNamequery;
SimpleBindings params = new SimpleBindings();
if(scopeName == null){
scopeNamequery = "from PolicyEditorScopes";
params.put("scopeName", scopeName + "%");
}
PolicyController controller = getPolicyControllerInstance();
- List<Object> scopesList = null;
+ List<Object> scopesList;
if(PolicyController.isjUnit()){
scopesList = controller.getDataByQuery(scopeNamequery, null);
}else{
}
//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){
+ private void activePolicyList(String inScopeName, List<JSONObject> resultList, List<String> roles, Set<String> scopes, boolean onlyFolders){
PolicyController controller = getPolicyControllerInstance();
+ String scopeName = inScopeName;
if(scopeName.contains("/")){
scopeName = scopeName.replace("/", File.separator);
}
if(scopeName.contains("\\")){
- scopeName = scopeName.replace("\\", "\\\\\\\\");
+ scopeName = scopeName.replace("\\", "\\\\");
}
String query = "from PolicyVersion where POLICY_NAME like :scopeName";
String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like :scopeName";
SimpleBindings params = new SimpleBindings();
params.put("scopeName", scopeName + "%");
- List<Object> activePolicies = null;
- List<Object> scopesList = null;
+ List<Object> activePolicies;
+ List<Object> scopesList;
if(PolicyController.isjUnit()){
activePolicies = controller.getDataByQuery(query, null);
scopesList = controller.getDataByQuery(scopeNamequery, null);
}
}
}
- String scopeNameCheck = null;
+ String scopeNameCheck;
for (Object list : activePolicies) {
PolicyVersion policy = (PolicyVersion) list;
String scopeNameValue = policy.getPolicyName().substring(0, policy.getPolicyName().lastIndexOf(File.separator));
if(roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)){
- if((scopeName.contains("\\\\"))){
+ if(scopeName.contains("\\\\")){
scopeNameCheck = scopeName.replace("\\\\", File.separator);
}else{
scopeNameCheck = scopeName;
String userId = UserUtils.getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
- oldPath = oldPath.substring(oldPath.indexOf("/")+1);
- newPath = newPath.substring(newPath.indexOf("/")+1);
+ oldPath = oldPath.substring(oldPath.indexOf('/')+1);
+ newPath = newPath.substring(newPath.indexOf('/')+1);
+ String checkValidation = null;
if(oldPath.endsWith(".xml")){
+ checkValidation = newPath.replace(".xml", "");
+ checkValidation = checkValidation.substring(checkValidation.indexOf('_') + 1, checkValidation.lastIndexOf("."));
+ checkValidation = checkValidation.substring(checkValidation.lastIndexOf("/")+1);
+ if(!PolicyUtils.policySpecialCharValidator(checkValidation).contains("success")){
+ return error("Policy Rename Failed. The Name contains special characters.");
+ }
JSONObject result = policyRename(oldPath, newPath, userId);
if(!(Boolean)(result.getJSONObject("result").get("success"))){
return result;
scopeName = scopeName.replace("/", File.separator);
newScopeName = newScopeName.replace("/", File.separator);
}
- if(scopeName.contains("\\")){
- scopeName = scopeName.replace("\\", "\\\\\\\\");
- newScopeName = newScopeName.replace("\\", "\\\\\\\\");
- }
+ checkValidation = newScopeName.substring(newScopeName.lastIndexOf(File.separator)+1);
+ if(scopeName.contains("\\")){
+ scopeName = scopeName.replace("\\", "\\\\\\\\");
+ newScopeName = newScopeName.replace("\\", "\\\\\\\\");
+ }
+ if(!PolicyUtils.policySpecialCharValidator(checkValidation).contains("success")){
+ return error("Scope Rename Failed. The Name contains special characters.");
+ }
PolicyController controller = getPolicyControllerInstance();
String query = "from PolicyVersion where POLICY_NAME like :scopeName";
String scopeNamequery = "from PolicyEditorScopes where SCOPENAME like :scopeName";
if(!(Boolean)(result.getJSONObject("result").get("success"))){
isActive = true;
policyActiveInPDP.add(policyOldPath);
- String scope = policyOldPath.substring(0, policyOldPath.lastIndexOf("/"));
+ String scope = policyOldPath.substring(0, policyOldPath.lastIndexOf('/'));
scopeOfPolicyActiveInPDP.add(scope.replace("/", File.separator));
}
}
UserInfo userInfo = new UserInfo();
userInfo.setUserLoginId(userId);
- if(policyActiveInPDP.size() == 0){
+ if(policyActiveInPDP.isEmpty()){
renameScope(scopesList, scopeName, newScopeName, controller);
}else if(rename){
renameScope(scopesList, scopeName, newScopeName, controller);
}
}
- private void renameScope(List<Object> scopesList, String scopeName, String newScopeName, PolicyController controller){
+ private void renameScope(List<Object> scopesList, String inScopeName, String newScopeName, PolicyController controller){
for(Object object : scopesList){
PolicyEditorScopes editorScopeEntity = (PolicyEditorScopes) object;
+ String scopeName = inScopeName;
if(scopeName.contains("\\\\\\\\")){
scopeName = scopeName.replace("\\\\\\\\", File.separator);
newScopeName = newScopeName.replace("\\\\\\\\", File.separator);
private JSONObject policyRename(String oldPath, String newPath, String userId) throws ServletException {
try {
- PolicyEntity entity = null;
+ PolicyEntity entity;
PolicyController controller = getPolicyControllerInstance();
String policyVersionName = newPath.replace(".xml", "");
- String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf(".")).replace("/", File.separator);
+ String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf('.')).replace("/", File.separator);
String oldpolicyVersionName = oldPath.replace(".xml", "");
- String oldpolicyName = oldpolicyVersionName.substring(0, oldpolicyVersionName.lastIndexOf(".")).replace("/", File.separator);
+ String oldpolicyName = oldpolicyVersionName.substring(0, oldpolicyVersionName.lastIndexOf('.')).replace("/", File.separator);
String newpolicyName = newPath.replace("/", ".");
String newPolicyCheck = newpolicyName;
policyParams.put("newPolicySplit_0", newPolicySplit[0]);
List<Object> queryData = controller.getDataByQuery(policyEntityquery, policyParams);
if(!queryData.isEmpty()){
- entity = (PolicyEntity) queryData.get(0);
return error("Policy rename failed. Since, the policy with same name already exists.");
}
//Query the Policy Entity with oldPolicy Name
- String policyEntityCheck = oldPolicySplit[1].substring(0, oldPolicySplit[1].indexOf("."));
+ String policyEntityCheck = oldPolicySplit[1].substring(0, oldPolicySplit[1].indexOf('.'));
String oldpolicyEntityquery = "FROM PolicyEntity where policyName like :policyEntityCheck and scope = :oldPolicySplit_0";
SimpleBindings params = new SimpleBindings();
params.put("policyEntityCheck", policyEntityCheck + "%");
params.put("oldPolicySplit_0", oldPolicySplit[0]);
List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery, params);
if(!oldEntityData.isEmpty()){
- String groupQuery = "FROM PolicyGroupEntity where (";
+ StringBuilder groupQuery = new StringBuilder();
+ groupQuery.append("FROM PolicyGroupEntity where (");
SimpleBindings geParams = new SimpleBindings();
for(int i=0; i<oldEntityData.size(); i++){
entity = (PolicyEntity) oldEntityData.get(i);
if(i == 0){
- groupQuery = groupQuery + "policyid = :policyId";
+ groupQuery.append("policyid = :policyId");
geParams.put("policyId", entity.getPolicyId());
}else{
- groupQuery = groupQuery + " or policyid = :policyId" + i;
+ groupQuery.append(" or policyid = :policyId" + i);
geParams.put("policyId" + i, entity.getPolicyId());
}
}
- groupQuery = groupQuery + ")";
- List<Object> groupEntityData = controller.getDataByQuery(groupQuery, geParams);
- if(groupEntityData.size() > 0){
+ groupQuery.append(")");
+ List<Object> groupEntityData = controller.getDataByQuery(groupQuery.toString(), geParams);
+ if(! groupEntityData.isEmpty()){
return error("Policy rename failed. Since the policy or its version is active in PDP Groups.");
}
for(int i=0; i<oldEntityData.size(); i++){
entity = (PolicyEntity) oldEntityData.get(i);
- checkOldPolicyEntryAndUpdate(entity, newPolicySplit[0] , newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], policyName, newpolicyName, oldpolicyName, userId);
+ String checkEntityName = entity.getPolicyName().replace(".xml", "");
+ checkEntityName = checkEntityName.substring(0, checkEntityName.lastIndexOf('.'));
+ String originalPolicyName = oldpolicyName.substring(oldpolicyName.lastIndexOf(File.separator)+1);
+ if(checkEntityName.equals(originalPolicyName)){
+ checkOldPolicyEntryAndUpdate(entity, newPolicySplit[0] , newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], policyName, newpolicyName, oldpolicyName, userId);
+ }
}
}else{
return error("Policy rename failed due to policy not able to retrieve from database. Please, contact super-admin.");
String oldPolicyNameWithoutExtension = removeoldPolicyExtension;
String newPolicyNameWithoutExtension = removenewPolicyExtension;
if(removeoldPolicyExtension.endsWith(".xml")){
- oldPolicyNameWithoutExtension = oldPolicyNameWithoutExtension.substring(0, oldPolicyNameWithoutExtension.indexOf("."));
- newPolicyNameWithoutExtension = newPolicyNameWithoutExtension.substring(0, newPolicyNameWithoutExtension.indexOf("."));
+ oldPolicyNameWithoutExtension = oldPolicyNameWithoutExtension.substring(0, oldPolicyNameWithoutExtension.indexOf('.'));
+ newPolicyNameWithoutExtension = newPolicyNameWithoutExtension.substring(0, newPolicyNameWithoutExtension.indexOf('.'));
}
entity.setPolicyName(entity.getPolicyName().replace(oldPolicyNameWithoutExtension, newPolicyNameWithoutExtension));
entity.setPolicyData(entity.getPolicyData().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension));
entity.setScope(newScope);
entity.setModifiedBy(userId);
+
+ String oldConfigurationName = null;
+ String newConfigurationName = null;
if(newpolicyName.contains("Config_")){
- String oldConfigurationName = configEntity.getConfigurationName();
+ oldConfigurationName = configEntity.getConfigurationName();
configEntity.setConfigurationName(configEntity.getConfigurationName().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension));
controller.updateData(configEntity);
- String newConfigurationName = configEntity.getConfigurationName();
+ newConfigurationName = configEntity.getConfigurationName();
File file = new File(PolicyController.getConfigHome() + File.separator + oldConfigurationName);
if(file.exists()){
File renamefile = new File(PolicyController.getConfigHome() + File.separator + newConfigurationName);
file.renameTo(renamefile);
}
}else if(newpolicyName.contains("Action_")){
- String oldConfigurationName = actionEntity.getActionBodyName();
+ oldConfigurationName = actionEntity.getActionBodyName();
actionEntity.setActionBody(actionEntity.getActionBody().replace(oldScope +"."+oldPolicyNameWithoutExtension, newScope+"."+newPolicyNameWithoutExtension));
controller.updateData(actionEntity);
- String newConfigurationName = actionEntity.getActionBodyName();
+ newConfigurationName = actionEntity.getActionBodyName();
File file = new File(PolicyController.getActionHome() + File.separator + oldConfigurationName);
if(file.exists()){
File renamefile = new File(PolicyController.getActionHome() + File.separator + newConfigurationName);
}
controller.updateData(entity);
+ PolicyRestController restController = new PolicyRestController();
+ restController.notifyOtherPAPSToUpdateConfigurations("rename", newConfigurationName, oldConfigurationName);
PolicyVersion versionEntity = (PolicyVersion) controller.getEntityItem(PolicyVersion.class, "policyName", oldpolicyName);
versionEntity.setPolicyName(policyName);
versionEntity.setModifiedBy(userId);
}
}
- 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;
+ private JSONObject cloneRecord(String newpolicyName, String oldScope, String inRemoveoldPolicyExtension, String newScope, String inRemovenewPolicyExtension, PolicyEntity entity, String userId) throws ServletException{
+ String queryEntityName;
PolicyController controller = getPolicyControllerInstance();
PolicyEntity cloneEntity = new PolicyEntity();
cloneEntity.setPolicyName(newpolicyName);
+ String removeoldPolicyExtension = inRemoveoldPolicyExtension;
+ String removenewPolicyExtension = inRemovenewPolicyExtension;
removeoldPolicyExtension = removeoldPolicyExtension.replace(".xml", "");
removenewPolicyExtension = removenewPolicyExtension.replace(".xml", "");
cloneEntity.setPolicyData(entity.getPolicyData().replace(oldScope+"."+removeoldPolicyExtension, newScope+"."+removenewPolicyExtension));
cloneEntity.setScope(entity.getScope());
String oldConfigRemoveExtension = removeoldPolicyExtension.replace(".xml", "");
String newConfigRemoveExtension = removenewPolicyExtension.replace(".xml", "");
+ String newConfigurationName = null;
if(newpolicyName.contains("Config_")){
ConfigurationDataEntity configurationDataEntity = new ConfigurationDataEntity();
configurationDataEntity.setConfigurationName(entity.getConfigurationData().getConfigurationName().replace(oldScope+"."+oldConfigRemoveExtension, newScope+"."+newConfigRemoveExtension));
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);
+ newConfigurationName = configEntiy.getConfigurationName();
+ try (FileWriter 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);
}
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) {
+ newConfigurationName = actionEntiy.getActionBodyName();
+ try (FileWriter fw = new FileWriter(PolicyController.getActionHome() + File.separator + newConfigurationName);
+ BufferedWriter bw = new BufferedWriter(fw)){
+ bw.write(actionEntiy.getActionBody());
+ } 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);
cloneEntity.setModifiedBy(userId);
controller.saveData(cloneEntity);
+ //Notify others paps regarding clone policy.
+ PolicyRestController restController = new PolicyRestController();
+ restController.notifyOtherPAPSToUpdateConfigurations("clonePolicy", newConfigurationName, null);
return success();
}
String userId = UserUtils.getUserSession(request).getOrgUserId();
String oldPath = params.getString("path");
String newPath = params.getString("newPath");
- oldPath = oldPath.substring(oldPath.indexOf("/")+1);
- newPath = newPath.substring(newPath.indexOf("/")+1);
+ oldPath = oldPath.substring(oldPath.indexOf('/')+1);
+ newPath = newPath.substring(newPath.indexOf('/')+1);
String policyVersionName = newPath.replace(".xml", "");
- String version = policyVersionName.substring(policyVersionName.indexOf(".")+1);
- String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf(".")).replace("/", File.separator);
+ String version = policyVersionName.substring(policyVersionName.indexOf('.')+1);
+ String policyName = policyVersionName.substring(0, policyVersionName.lastIndexOf('.')).replace("/", File.separator);
String newpolicyName = newPath.replace("/", ".");
}else if(newPolicyCheck.contains("Decision_")){
newPolicyCheck = newPolicyCheck.replace(".Decision_", ":Decision_");
}
+ if(!newPolicyCheck.contains(":")){
+ return error("Policy Clone Failed. The Name contains special characters.");
+ }
String[] newPolicySplit = newPolicyCheck.split(":");
+ String checkValidation = newPolicySplit[1].replace(".xml", "");
+ checkValidation = checkValidation.substring(checkValidation.indexOf('_') + 1, checkValidation.lastIndexOf("."));
+ if(!PolicyUtils.policySpecialCharValidator(checkValidation).contains("success")){
+ return error("Policy Clone Failed. The Name contains special characters.");
+ }
+
String oldPolicyCheck = orignalPolicyName;
if(oldPolicyCheck.contains("Config_")){
oldPolicyCheck = oldPolicyCheck.replace(".Config_", ":Config_");
if(params.has("deleteVersion")){
deleteVersion = params.getString("deleteVersion");
}
- path = path.substring(path.indexOf("/")+1);
+ path = path.substring(path.indexOf('/')+1);
String policyNamewithExtension = path.replace("/", File.separator);
String policyVersionName = policyNamewithExtension.replace(".xml", "");
- String query = "";
+ String query;
SimpleBindings policyParams = new SimpleBindings();
if(path.endsWith(".xml")){
- policyNamewithoutExtension = policyVersionName.substring(0, policyVersionName.lastIndexOf("."));
+ policyNamewithoutExtension = policyVersionName.substring(0, policyVersionName.lastIndexOf('.'));
policyNamewithoutExtension = policyNamewithoutExtension.replace(File.separator, ".");
String splitPolicyName = null;
if(policyNamewithoutExtension.contains("Config_")){
boolean pdpCheck = false;
if(path.endsWith(".xml")){
policyNamewithoutExtension = policyNamewithoutExtension.replace(".", File.separator);
- int version = Integer.parseInt(policyVersionName.substring(policyVersionName.indexOf(".")+1));
+ int version = Integer.parseInt(policyVersionName.substring(policyVersionName.indexOf('.')+1));
if("ALL".equals(deleteVersion)){
if(!policyEntityobjects.isEmpty()){
for(Object object : policyEntityobjects){
policyEntity = (PolicyEntity) object;
String groupEntityquery = "from PolicyGroupEntity where policyid ='"+policyEntity.getPolicyId()+"'";
SimpleBindings pgeParams = new SimpleBindings();
- //pgeParams.put("policyIdValue", policyEntity.getPolicyId());
List<Object> groupobject = controller.getDataByQuery(groupEntityquery, pgeParams);
if(!groupobject.isEmpty()){
pdpCheck = true;
if(policyNamewithoutExtension.contains("Config_")){
Files.deleteIfExists(Paths.get(PolicyController.getConfigHome() + File.separator + policyEntity.getConfigurationData().getConfigurationName()));
controller.deleteData(policyEntity.getConfigurationData());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getConfigurationData().getConfigurationName());
}else if(policyNamewithoutExtension.contains("Action_")){
Files.deleteIfExists(Paths.get(PolicyController.getActionHome() + File.separator + policyEntity.getActionBodyEntity().getActionBodyName()));
controller.deleteData(policyEntity.getActionBodyEntity());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getActionBodyEntity().getActionBodyName());
}
}
}
if(pdpCheck){
//Delete from policyVersion table
String getActivePDPPolicyVersion = activePolicyName.replace(".xml", "");
- getActivePDPPolicyVersion = getActivePDPPolicyVersion.substring(getActivePDPPolicyVersion.lastIndexOf(".")+1);
+ getActivePDPPolicyVersion = getActivePDPPolicyVersion.substring(getActivePDPPolicyVersion.lastIndexOf('.')+1);
String policyVersionQuery = "update PolicyVersion set active_version='"+getActivePDPPolicyVersion+"' , highest_version='"+getActivePDPPolicyVersion+"' where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"' and id >0";
if(policyVersionQuery != null){
controller.executeQuery(policyVersionQuery);
if(policyNamewithoutExtension.contains("Config_")){
Files.deleteIfExists(Paths.get(PolicyController.getConfigHome() + File.separator + policyEntity.getConfigurationData().getConfigurationName()));
controller.deleteData(policyEntity.getConfigurationData());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getConfigurationData().getConfigurationName());
}else if(policyNamewithoutExtension.contains("Action_")){
Files.deleteIfExists(Paths.get(PolicyController.getActionHome() + File.separator + policyEntity.getActionBodyEntity().getActionBodyName()));
controller.deleteData(policyEntity.getActionBodyEntity());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getActionBodyEntity().getActionBodyName());
}
if(version > 1){
for(Object object : policyEntityobjects){
policyEntity = (PolicyEntity) object;
String policyEntityName = policyEntity.getPolicyName().replace(".xml", "");
- int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1));
+ int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf('.')+1));
if(policyEntityVersion > highestVersion && policyEntityVersion != version){
highestVersion = policyEntityVersion;
}
entity.setModifiedBy(userId);
controller.watchPolicyFunction(entity, policyNamewithExtension, "DeleteOne");
- String updatequery = "update PolicyVersion set active_version='"+highestVersion+"' , highest_version='"+highestVersion+"' where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"'";
+ String updatequery = "";
+ if(highestVersion != 0){
+ updatequery = "update PolicyVersion set active_version='"+highestVersion+"' , highest_version='"+highestVersion+"' where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"'";
+ }else{
+ updatequery = "delete from PolicyVersion where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"' and id >0";
+ }
controller.executeQuery(updatequery);
}else{
String policyVersionQuery = "delete from PolicyVersion where policy_name ='" +policyNamewithoutExtension.replace("\\", "\\\\")+"' and id >0";
}
}
}else{
- List<String> activePoliciesInPDP = new ArrayList<String>();
+ List<String> activePoliciesInPDP = new ArrayList<>();
if(!policyEntityobjects.isEmpty()){
for(Object object : policyEntityobjects){
policyEntity = (PolicyEntity) object;
if(policyNamewithoutExtension.contains("Config_")){
Files.deleteIfExists(Paths.get(PolicyController.getConfigHome() + File.separator + policyEntity.getConfigurationData().getConfigurationName()));
controller.deleteData(policyEntity.getConfigurationData());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getConfigurationData().getConfigurationName());
}else if(policyNamewithoutExtension.contains("Action_")){
Files.deleteIfExists(Paths.get(PolicyController.getActionHome() + File.separator + policyEntity.getActionBodyEntity().getActionBodyName()));
controller.deleteData(policyEntity.getActionBodyEntity());
+ restController.notifyOtherPAPSToUpdateConfigurations("delete", null, policyEntity.getActionBodyEntity().getActionBodyName());
}
}
}
//Add Active Policies List to PolicyVersionTable
for(int i =0; i < activePoliciesInPDP.size(); i++){
String activePDPPolicyName = activePoliciesInPDP.get(i).replace(".xml", "");
- int activePDPPolicyVersion = Integer.parseInt(activePDPPolicyName.substring(activePDPPolicyName.lastIndexOf(".")+1));
- activePDPPolicyName = activePDPPolicyName.substring(0, activePDPPolicyName.lastIndexOf(".")).replace(".", File.separator);
+ int activePDPPolicyVersion = Integer.parseInt(activePDPPolicyName.substring(activePDPPolicyName.lastIndexOf('.')+1));
+ activePDPPolicyName = activePDPPolicyName.substring(0, activePDPPolicyName.lastIndexOf('.')).replace(".", File.separator);
PolicyVersion insertactivePDPVersion = new PolicyVersion();
insertactivePDPVersion.setPolicyName(activePDPPolicyName);
insertactivePDPVersion.setHigherVersion(activePDPPolicyVersion);
String path = params.getString("path");
LOGGER.debug("editFile path: {}"+ path);
- String domain = path.substring(1, path.lastIndexOf("/"));
+ String domain = path.substring(1, path.lastIndexOf('/'));
domain = domain.replace("/", ".");
path = path.substring(1);
SimpleBindings peParams = new SimpleBindings();
peParams.put("split_1", split[1]);
peParams.put("split_0", split[0]);
- List<Object> queryData = null;
+ List<Object> queryData;
if(PolicyController.isjUnit()){
queryData = controller.getDataByQuery(query, null);
}else{
policyAdapter.setDomainDir(domain);
policyAdapter.setPolicyData(policy);
String policyName = path.replace(".xml", "");
- policyName = policyName.substring(0, policyName.lastIndexOf("."));
- policyAdapter.setPolicyName(policyName.substring(policyName.lastIndexOf(".")+1));
+ policyName = policyName.substring(0, policyName.lastIndexOf('.'));
+ policyAdapter.setPolicyName(policyName.substring(policyName.lastIndexOf('.')+1));
PolicyAdapter setpolicyAdapter = PolicyAdapter.getInstance();
setpolicyAdapter.configure(policyAdapter,entity);
String path = params.getString("path");
try{
if(params.has("subScopename")){
- if(!params.getString("subScopename").equals("")){
+ if(! "".equals(params.getString("subScopename"))) {
name = params.getString("path").replace("/", File.separator) + File.separator +params.getString("subScopename");
}
}else{
}
}
LOGGER.debug("addFolder path: {} name: {}" + path +name);
- if(!name.equals("")){
+ if(! "".equals(name)){
if(name.startsWith(File.separator)){
name = name.substring(1);
}