X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fadmin%2FPolicyRestController.java;h=2a52335e56f3d35473c58348512710edb3916ff8;hb=87ef46b7f3004e78b89c5a5c60d1160b332abbb2;hp=2eba697cf928d2a8c941744b0879612feea60e07;hpb=3555353c1baedc080f310e9e737a46f7cc7aa89e;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java index 2eba697cf..2a52335e5 100644 --- a/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java +++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/admin/PolicyRestController.java @@ -3,6 +3,7 @@ * ONAP Policy Engine * ================================================================================ * Copyright (C) 2017-2018 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. @@ -289,12 +290,7 @@ public class PolicyRestController extends RestrictedBaseController{ connection.setRequestProperty("Content-Type",PolicyController.getContenttype()); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - JsonNode root = null; - try { - root = mapper.readTree(request.getReader()); - }catch (Exception e1) { - policyLogger.error("Exception Occured while calling PAP"+e1); - } + JsonNode root = getJsonNode(request, mapper); ObjectMapper mapper1 = new ObjectMapper(); mapper1.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); @@ -302,15 +298,12 @@ public class PolicyRestController extends RestrictedBaseController{ Object obj = mapper1.treeToValue(root, Object.class); String json = mapper1.writeValueAsString(obj); - Object content = new ByteArrayInputStream(json.getBytes()); - - if (content instanceof InputStream) { - // send current configuration - try (OutputStream os = connection.getOutputStream()) { - int count = IOUtils.copy((InputStream) content, os); - if (policyLogger.isDebugEnabled()) { - policyLogger.debug("copied to output, bytes=" + count); - } + // send current configuration + try(InputStream content = new ByteArrayInputStream(json.getBytes()); + OutputStream os = connection.getOutputStream()) { + int count = IOUtils.copy(content, os); + if (policyLogger.isDebugEnabled()) { + policyLogger.debug("copied to output, bytes=" + count); } } }else{ @@ -330,30 +323,7 @@ public class PolicyRestController extends RestrictedBaseController{ } } } - - connection.connect(); - - int responseCode = connection.getResponseCode(); - if(responseCode == 200){ - // get the response content into a String - String responseJson = 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"); - responseJson = scanner.hasNext() ? scanner.next() : ""; - } catch (Exception e){ - //Reason for rethrowing the exception is if any exception occurs during reading of inputsteam - //then the exception handling is done by the outer block without returning the response immediately - //Also finally block is existing only in outer block and not here so all exception handling is - //done in only one place - policyLogger.error("Exception Occured"+e); - throw e; - } - - policyLogger.info("JSON response from PAP: " + responseJson); - return responseJson; - } - + return doConnect(connection); } catch (Exception e) { policyLogger.error("Exception Occured"+e); }finally{ @@ -377,6 +347,41 @@ public class PolicyRestController extends RestrictedBaseController{ return null; } + private JsonNode getJsonNode(HttpServletRequest request, ObjectMapper mapper) { + JsonNode root = null; + try { + root = mapper.readTree(request.getReader()); + }catch (Exception e1) { + policyLogger.error("Exception Occured while calling PAP"+e1); + } + return root; + } + + private String doConnect(final HttpURLConnection connection) throws IOException{ + connection.connect(); + int responseCode = connection.getResponseCode(); + if(responseCode == 200){ + // get the response content into a String + String responseJson = 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"); + responseJson = scanner.hasNext() ? scanner.next() : ""; + } catch (Exception e){ + //Reason for rethrowing the exception is if any exception occurs during reading of inputsteam + //then the exception handling is done by the outer block without returning the response immediately + //Also finally block is existing only in outer block and not here so all exception handling is + //done in only one place + policyLogger.error("Exception Occured"+e); + throw e; + } + + policyLogger.info("JSON response from PAP: " + responseJson); + return responseJson; + } + return null; + } + @RequestMapping(value={"/getDictionary/*"}, method={RequestMethod.GET}) public void getDictionaryController(HttpServletRequest request, HttpServletResponse response){ String uri = request.getRequestURI().replace("/getDictionary", "");