aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
diff options
context:
space:
mode:
authorKrishnajinka <kris.jinka@samsung.com>2018-07-25 15:05:28 +0900
committerKrishnakumar Jinka <kris.jinka@samsung.com>2018-07-25 14:56:22 +0000
commit87ef46b7f3004e78b89c5a5c60d1160b332abbb2 (patch)
tree28283fd019ced509c95e4260cb3131bcf521b9fe /ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
parent5ad6783ca5a09c0061b661a48210f6d8e22cd7d3 (diff)
Fix issues reported by sonar
Fix issues related to unused method variables, extract constants, not nest more than 3 control stmts, use try with res in policy engine project. Rework for comments Issue-ID: POLICY-1008 Change-Id: If0953de9e802110df14862707f3b525652cdf112 Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
Diffstat (limited to 'ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java')
-rw-r--r--ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java123
1 files changed, 53 insertions, 70 deletions
diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
index 936e497f4..6dae064a7 100644
--- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
+++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PDPServices.java
@@ -341,83 +341,66 @@ public class PDPServices {
if(pdpConfigLocation.contains("/")){
pdpConfigLocation = pdpConfigLocation.replace("/", File.separator);
}
- InputStream inputStream = null;
- JsonReader jsonReader = null;
- try {
- inputStream = new FileInputStream(new File(pdpConfigLocation));
- try {
- if (pdpConfigLocation.endsWith("json")) {
- pdpResponse.setType(PolicyType.JSON);
- jsonReader = Json.createReader(inputStream);
+
+ try(InputStream inputStream = new FileInputStream(new File(pdpConfigLocation))) {
+ if (pdpConfigLocation.endsWith("json")) {
+ pdpResponse.setType(PolicyType.JSON);
+ try(JsonReader jsonReader = Json.createReader(inputStream);) {
pdpResponse.setConfig(jsonReader.readObject().toString());
- jsonReader.close();
- } else if (pdpConfigLocation.endsWith("xml")) {
- pdpResponse.setType(PolicyType.XML);
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
- dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
- DocumentBuilder db = null;
- try {
- db = dbf.newDocumentBuilder();
- Document document = db.parse(inputStream);
- DOMSource domSource = new DOMSource(document);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer;
- transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- pdpResponse.setConfig(writer.toString());
- } catch (Exception e) {
- LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ e);
- throw new PDPException(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ "Unable to parse the XML config", e);
- }
- } else if (pdpConfigLocation.endsWith("properties")) {
- pdpResponse.setType(PolicyType.PROPERTIES);
- Properties configProp = new Properties();
- configProp.load(inputStream);
- Map<String, String> propVal = new HashMap<>();
- for(String name: configProp.stringPropertyNames()) {
- propVal.put(name, configProp.getProperty(name));
- }
- pdpResponse.setProperty(propVal);
- } else if (pdpConfigLocation.endsWith("txt")) {
- pdpResponse.setType(PolicyType.OTHER);
- String other = IOUtils.toString(inputStream);
- IOUtils.closeQuietly(inputStream);
- pdpResponse.setConfig(other);
- } else {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config Not Found");
- pdpResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_NOT_FOUND);
- pdpResponse.setPolicyConfigMessage("Illegal form of Configuration Type Found.");
- inputStream.close();
- return pdpResponse;
}
- LOGGER.info("config Retrieved " + pdpConfigLocation);
- pdpResponse.setStatus("Config Retrieved! ",
- PolicyResponseStatus.NO_ACTION_REQUIRED,
- PolicyConfigStatus.CONFIG_RETRIEVED);
- return pdpResponse;
- } catch (IOException | ParserConfigurationException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
- throw new PDPException(XACMLErrorConstants.ERROR_PROCESS_FLOW +
- "Cannot open a connection to the configURL", e);
- } finally {
- if(jsonReader != null) {
- try {
- jsonReader.close();
- } catch (Exception e) {
- LOGGER.error("Exception Occured while closing the JsonReader"+e);
- }
+ } else if (pdpConfigLocation.endsWith("xml")) {
+ pdpResponse.setType(PolicyType.XML);
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ DocumentBuilder db = null;
+ try {
+ db = dbf.newDocumentBuilder();
+ Document document = db.parse(inputStream);
+ DOMSource domSource = new DOMSource(document);
+ StringWriter writer = new StringWriter();
+ StreamResult result = new StreamResult(writer);
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer transformer;
+ transformer = tf.newTransformer();
+ transformer.transform(domSource, result);
+ pdpResponse.setConfig(writer.toString());
+ } catch (Exception e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ e);
+ throw new PDPException(XACMLErrorConstants.ERROR_SCHEMA_INVALID+ "Unable to parse the XML config", e);
}
+ } else if (pdpConfigLocation.endsWith("properties")) {
+ pdpResponse.setType(PolicyType.PROPERTIES);
+ Properties configProp = new Properties();
+ configProp.load(inputStream);
+ Map<String, String> propVal = new HashMap<>();
+ for(String name: configProp.stringPropertyNames()) {
+ propVal.put(name, configProp.getProperty(name));
+ }
+ pdpResponse.setProperty(propVal);
+ } else if (pdpConfigLocation.endsWith("txt")) {
+ pdpResponse.setType(PolicyType.OTHER);
+ String other = IOUtils.toString(inputStream);
+ IOUtils.closeQuietly(inputStream);
+ pdpResponse.setConfig(other);
+ } else {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config Not Found");
+ pdpResponse.setPolicyConfigStatus(PolicyConfigStatus.CONFIG_NOT_FOUND);
+ pdpResponse.setPolicyConfigMessage("Illegal form of Configuration Type Found.");
+ return pdpResponse;
}
+ LOGGER.info("config Retrieved " + pdpConfigLocation);
+ pdpResponse.setStatus("Config Retrieved! ",
+ PolicyResponseStatus.NO_ACTION_REQUIRED,
+ PolicyConfigStatus.CONFIG_RETRIEVED);
+ return pdpResponse;
} catch (FileNotFoundException e) {
LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
throw new PDPException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error in ConfigURL", e);
- }finally{
- if(inputStream != null){
- inputStream.close();
- }
+ } catch (IOException | ParserConfigurationException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
+ throw new PDPException(XACMLErrorConstants.ERROR_PROCESS_FLOW +
+ "Cannot open a connection to the configURL", e);
}
}