+ private void checkURI(HttpServletRequest request, String uri, HttpURLConnection connection, FileItem item) throws IOException {
+ String boundary;
+ if(!(uri.endsWith("set_BRMSParamData") || uri.contains(IMPORT_DICTIONARY))){
+ connection.setRequestProperty(CONTENT_TYPE,PolicyController.getContenttype());
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ JsonNode root = getJsonNode(request, mapper);
+
+ ObjectMapper mapper1 = new ObjectMapper();
+ mapper1.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
+
+ Object obj = mapper1.treeToValue(root, Object.class);
+ String json = mapper1.writeValueAsString(obj);
+
+ // 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{
+ if(uri.endsWith("set_BRMSParamData")){
+ connection.setRequestProperty(CONTENT_TYPE,PolicyController.getContenttype());
+ try (OutputStream os = connection.getOutputStream()) {
+ IOUtils.copy((InputStream) request.getInputStream(), os);
+ }
+ }else{
+ boundary = "===" + System.currentTimeMillis() + "===";
+ connection.setRequestProperty(CONTENT_TYPE,"multipart/form-data; boundary=" + boundary);
+ try (OutputStream os = connection.getOutputStream()) {
+ if(item != null){
+ IOUtils.copy((InputStream) item.getInputStream(), os);
+ }
+ }
+ }
+ }
+ }
+
+ 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;
+ }
+