X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FFirewallConfigPolicy.java;h=e747edb9b56e8e667e888cd68bf7fbd3af9a520d;hb=7957dd169d09ff5fcfdfc0384e8ed7d3534d3dc0;hp=0c8784c8151e76f2cdb82581def84b628c21854b;hpb=073cc188efe9abb4c010cf674e34e2cf46ef1c52;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java index 0c8784c81..e747edb9b 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java @@ -62,6 +62,7 @@ import org.onap.policy.rest.jpa.ServiceList; import org.onap.policy.rest.jpa.TermList; import org.onap.policy.rest.jpa.UserInfo; +import com.att.research.xacml.api.pap.PAPException; import com.att.research.xacml.std.IdentifierImpl; import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jackson.JsonLoader; @@ -97,39 +98,28 @@ public class FirewallConfigPolicy extends Policy { // Saving the Configurations file at server location for config policy. protected void saveConfigurations(String policyName, String jsonBody) { String configurationName = policyName; - FileWriter fw = null; - try{ - if(configurationName.endsWith(".xml")){ - configurationName = configurationName.replace(".xml", ""); - } - fw = new FileWriter(CONFIG_HOME + File.separator + configurationName + ".json"); - BufferedWriter bw = new BufferedWriter(fw); - bw.write(jsonBody); - bw.close(); + if(configurationName.endsWith(".xml")){ + configurationName = configurationName.replace(".xml", ""); + } + String fileName = CONFIG_HOME + File.separator + configurationName + ".json"; + try(BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))){ + bw.write(jsonBody); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Configuration is succesfully saved"); } } catch (IOException e) { - LOGGER.error("Exception Occured"+e); - }finally{ - try{ - if(fw != null){ - fw.close(); - } - }catch(Exception e){ - LOGGER.error("Exception Occured"+e); - } + LOGGER.error("Save of configuration to file" +fileName+ "failed",e); } } - - //Utility to read json data from the existing file to a string + + //Utility to read json data from the existing file to a string static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); } @Override - public Map savePolicies() throws Exception { + public Map savePolicies() throws PAPException { Map successMap = new HashMap<>(); if(isPolicyExists()){ successMap.put("EXISTS", "This Policy already exist on the PAP"); @@ -147,7 +137,11 @@ public class FirewallConfigPolicy extends Policy { if (policyAdapter.isEditPolicy()) { dbIsUpdated = updateFirewallDictionaryData(policyAdapter.getJsonBody(), policyAdapter.getPrevJsonBody()); } else { - dbIsUpdated = insertFirewallDicionaryData(policyAdapter.getJsonBody()); + try { + dbIsUpdated = insertFirewallDicionaryData(policyAdapter.getJsonBody()); + } catch (SQLException e) { + throw new PAPException(e); + } } } else { dbIsUpdated = true; @@ -174,7 +168,7 @@ public class FirewallConfigPolicy extends Policy { //This is the method for preparing the policy for saving. We have broken it out //separately because the fully configured policy is used for multiple things @Override - public boolean prepareToSave() throws Exception{ + public boolean prepareToSave() throws PAPException{ if(isPreparedToSave()){ //we have already done this @@ -1271,16 +1265,11 @@ public class FirewallConfigPolicy extends Policy { } private JsonObject stringToJson(String jsonString) { - JsonObject json = null; - if (jsonString != null) { - //Read jsonBody to JsonObject - StringReader in = null; - in = new StringReader(jsonString); - - JsonReader jsonReader = Json.createReader(in); - json = jsonReader.readObject(); - jsonReader.close(); - } + //Read jsonBody to JsonObject + StringReader in = new StringReader(jsonString); + JsonReader jsonReader = Json.createReader(in); + JsonObject json = jsonReader.readObject(); + jsonReader.close(); return json; }