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;
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")){
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 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{
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{
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));
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);
if(oldPath.endsWith(".xml")){
JSONObject result = policyRename(oldPath, newPath, userId);
if(!(Boolean)(result.getJSONObject("result").get("success"))){
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 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++){
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));
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;
+ String queryEntityName;
PolicyController controller = getPolicyControllerInstance();
PolicyEntity cloneEntity = new PolicyEntity();
cloneEntity.setPolicyName(newpolicyName);
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("/", ".");
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(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);
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;
}
//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);
}