Merge "Fix issues reported by sonar"
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / PDPServices.java
index af7112e..6dae064 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP-PDP-REST
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -88,9 +89,9 @@ public class PDPServices {
         try {
             Request request = JSONRequest.load(jsonString);
             // Assign a rainy day treatment request to parse the decided treatment
-               if (jsonString.contains("BB_ID")) {
-                       rainydayRequest = request;
-               }
+            if (jsonString.contains("BB_ID")) {
+                rainydayRequest = request;
+            }
             // Call the PDP
             LOGGER.info("--- Generating Request: ---\n" + JSONRequest.toString(request));
             response = callPDP(request, requestID);
@@ -128,16 +129,16 @@ public class PDPServices {
                 LOGGER.info("Decision not a Permit. "  + result.getDecision().toString());
                 PDPResponse pdpResponse = new PDPResponse();
                 if (decide) {
-                       String indeterminatePropValue = XACMLProperties.getProperty("decision.indeterminate.response");
-                       if(result.getDecision().equals(Decision.INDETERMINATE)&& indeterminatePropValue != null){
-                               if("PERMIT".equalsIgnoreCase(indeterminatePropValue)){
-                                       pdpResponse.setDecision(PolicyDecision.PERMIT);
-                               }else{
-                                       pdpResponse.setDecision(PolicyDecision.DENY);
-                               }
-                       }else{
-                               pdpResponse.setDecision(PolicyDecision.DENY);
-                       }
+                    String indeterminatePropValue = XACMLProperties.getProperty("decision.indeterminate.response");
+                    if(result.getDecision().equals(Decision.INDETERMINATE)&& indeterminatePropValue != null){
+                        if("PERMIT".equalsIgnoreCase(indeterminatePropValue)){
+                            pdpResponse.setDecision(PolicyDecision.PERMIT);
+                        }else{
+                            pdpResponse.setDecision(PolicyDecision.DENY);
+                        }
+                    }else{
+                        pdpResponse.setDecision(PolicyDecision.DENY);
+                    }
                     for(Advice advice: result.getAssociatedAdvice()){
                         for(AttributeAssignment attribute: advice.getAttributeAssignments()){
                             pdpResponse.setDetails(attribute.getAttributeValue().getValue().toString());
@@ -156,12 +157,12 @@ public class PDPServices {
                     PDPResponse pdpResponse = new PDPResponse();
                     pdpResponse.setDecision(PolicyDecision.PERMIT);
                     
-                       //if this is a Rainy Day treatment decision we need to get the selected treatment
-                       if(rainydayRequest!=null){
-                               pdpResponse.setDetails(getRainyDayTreatment(result));
-                       } else {
+                    //if this is a Rainy Day treatment decision we need to get the selected treatment
+                    if(rainydayRequest!=null){
+                        pdpResponse.setDetails(getRainyDayTreatment(result));
+                    } else {
                         pdpResponse.setDetails("Decision Permit. OK!");
-                       }
+                    }
                     combinedResult.add(pdpResponse);
                     return combinedResult;
                 }
@@ -319,20 +320,20 @@ public class PDPServices {
     }
     
     private String getRainyDayTreatment(Result result) {
-       String treatment = null;
-       if (rainydayRequest!=null&& !result.getAssociatedAdvice().isEmpty()) {
-               // Get the desired treatment for requested errorCode from the Advice
-               for (Advice advice : result.getAssociatedAdvice()) {
-                       Map<String, String> adviseAttributes = new HashMap<>();
-                       for (AttributeAssignment attribute : advice.getAttributeAssignments()) {
-                               adviseAttributes.put(attribute.getAttributeId().stringValue(), attribute.getAttributeValue().getValue().toString());
-                               if ("treatment".equalsIgnoreCase(attribute.getAttributeId().stringValue())){
-                                       treatment = attribute.getAttributeValue().getValue().toString();
-                               }
-                       }   
-               }
-       }
-       return treatment;
+        String treatment = null;
+        if (rainydayRequest!=null&& !result.getAssociatedAdvice().isEmpty()) {
+            // Get the desired treatment for requested errorCode from the Advice
+            for (Advice advice : result.getAssociatedAdvice()) {
+                Map<String, String> adviseAttributes = new HashMap<>();
+                for (AttributeAssignment attribute : advice.getAttributeAssignments()) {
+                    adviseAttributes.put(attribute.getAttributeId().stringValue(), attribute.getAttributeValue().getValue().toString());
+                    if ("treatment".equalsIgnoreCase(attribute.getAttributeId().stringValue())){
+                        treatment = attribute.getAttributeValue().getValue().toString();
+                    }
+                }
+            }
+        }
+        return treatment;
     }
 
     private PDPResponse configCall(String pdpConfigLocation) throws PDPException, IOException{
@@ -340,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);
         }
     }