X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FRESTfulPAPEngine.java;h=0c87ad3a444bfdac95ba71bdaa88036015e949d6;hb=eff265962c081edb751d5d2ed99dc443cb97f3fb;hp=53be0999d9957689a116841eb82db551865354e0;hpb=3555353c1baedc080f310e9e737a46f7cc7aa89e;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java index 53be0999d..0c87ad3a4 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/RESTfulPAPEngine.java @@ -405,19 +405,7 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP if (contentObj != null) { if (contentObj instanceof InputStream) { - try { - // - // Send our current policy configuration - // - try (OutputStream os = connection.getOutputStream()) { - int count = IOUtils.copy((InputStream)contentObj, os); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("copied to output, bytes="+count); - } - } - } catch (Exception e) { - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to write content in '" + method + "'", e); - } + sendCurrPolicyConfig(method, connection, (InputStream) contentObj); } else { // The contentObj is an object to be encoded in JSON ObjectMapper mapper = new ObjectMapper(); @@ -453,16 +441,7 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP return successMap; } else { // get the response content into a String - String json = null; - // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file) - try(java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) { - scanner.useDelimiter("\\A"); - json = scanner.hasNext() ? scanner.next() : ""; - } catch (Exception e){ - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to read inputStream from connection: " + e, e); - throw e; - } - LOGGER.info("JSON response from PAP: " + json); + String json = getJsonString(connection); // convert Object sent as JSON into local object ObjectMapper mapper = new ObjectMapper(); @@ -517,4 +496,34 @@ public class RESTfulPAPEngine extends StdPDPItemSetChangeNotifier implements PAP } } } + + private void sendCurrPolicyConfig(String method, final HttpURLConnection connection, InputStream contentObj) { + try { + // + // Send our current policy configuration + // + try (OutputStream os = connection.getOutputStream()) { + int count = IOUtils.copy(contentObj, os); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("copied to output, bytes="+count); + } + } + } catch (Exception e) { + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to write content in '" + method + "'", e); + } + } + + private String getJsonString(final HttpURLConnection connection) throws IOException { + String json = null; + // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file) + try(java.util.Scanner scanner = new java.util.Scanner(connection.getInputStream())) { + scanner.useDelimiter("\\A"); + json = scanner.hasNext() ? scanner.next() : ""; + } catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to read inputStream from connection: " + e, e); + throw e; + } + LOGGER.info("JSON response from PAP: " + json); + return json; + } }