ExportAndImportDecisionBlackListEntries extends RestrictedBaseController { @RequestMapping(value = {"/policycreation/exportDecisionBlackListEntries"}, method = {RequestMethod.POST}) public void exportBlackList(HttpServletRequest request, HttpServletResponse response) throws IOException { try (HSSFWorkbook workBook = new HSSFWorkbook()) { String requestData = request.getReader().lines().collect(Collectors.joining()); JSONObject root = new JSONObject(requestData); PolicyRestAdapter adapter = new Gson().fromJson(root.get("policyData").toString(), PolicyRestAdapter.class); DecisionPolicyController controller = new DecisionPolicyController(); controller.prePopulateDecisionPolicyData(adapter, null); List<String> blackLists = adapter.getYamlparams().getBlackList(); HSSFSheet sheet = workBook.createSheet("BlackList"); HSSFRow headingRow = sheet.createRow(0); headingRow.createCell(0).setCellValue(ACTION); headingRow.createCell(1).setCellValue(BLACKLISTENTRY); short rowNo = 1; for (Object object : blackLists) { HSSFRow row = sheet.createRow(rowNo); row.createCell(0).setCellValue(1); row.createCell(1).setCellValue(object.toString()); rowNo++; } String tmpFile = System.getProperty("catalina.base") + File.separator + "webapps" + File.separator + "temp"; SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = parseFormat.parse(root.get("date").toString().replaceAll("\"", "")); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); String formatedDate = dateFormat.format(date); String fileName = "BlackList_Scope_" + adapter.getDomainDir() + "_Name_" + adapter.getPolicyName() + "_Version_" + root.get("version").toString() + "_Date_" + formatedDate + ".xls"; String deleteCheckPath = tmpFile + File.separator + fileName; File deleteCheck = new File(deleteCheckPath); if (deleteCheck.exists() && deleteCheck.delete()) { policyLogger.info("Deleted the file from system before exporting a new file."); } File temPath = new File(tmpFile); if (!temPath.exists()) { temPath.mkdir(); } String file = temPath + File.separator + fileName; File filepath = new File(file); FileOutputStream fos = new FileOutputStream(filepath); workBook.write(fos); fos.flush(); response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); response.setContentType(PolicyUtils.APPLICATION_JSON); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); PrintWriter out = response.getWriter(); String successMap = file.substring(file.lastIndexOf("webapps") + 8); String responseString = new Gson().toJson(successMap); JSONObject jsonResposne = new JSONObject("{data: " + responseString + "}"); out.write(jsonResposne.toString()); } catch (Exception e) { policyLogger.error( XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while Exporting BlackList Entries", e); } }  @RequestMapping(value = {"/policycreation/exportDecisionBlackListEntries"}, method = {RequestMethod.POST}) void exportBlackList(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/policycreation/importBlackListForDecisionPolicy"}, method = {RequestMethod.POST}) void importBlackListFile(HttpServletRequest request, HttpServletResponse response);  }
@Test public void testExportBlackList() throws IOException { ClassLoader classLoader = getClass().getClassLoader(); jsonString = IOUtils.toString(classLoader.getResourceAsStream("DecisionPolicyData.txt")); try (BufferedReader reader = new BufferedReader(new StringReader(jsonString))) { Mockito.when(request.getReader()).thenReturn(reader); ExportAndImportDecisionBlackListEntries controller = new ExportAndImportDecisionBlackListEntries(); controller.exportBlackList(request, response); assertTrue("".equals(response.getContentAsString())); } catch (Exception e) { fail("Not expecting Exception while Exporting BlackListEntries."); } }