DeleteHandler { public void doApiDeleteFromPap(HttpServletRequest request, HttpServletResponse response) throws IOException { String json = null; java.util.Scanner scanner = new java.util.Scanner(request.getInputStream()); scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); PolicyLogger.info("JSON request from API to Delete Policy from the PAP: " + json); StdPAPPolicy policy = PolicyUtils.jsonStringToObject(json, StdPAPPolicy.class); String policyName = policy.getPolicyName(); boolean policyVersionDeleted = false; String removeXmlExtension; int currentVersion; String removeVersionExtension; String splitPolicyName = null; String[] split = null; String status = ERROR; PolicyEntity policyEntity = null; JPAUtils jpaUtils = null; try { jpaUtils = JPAUtils.getJPAUtilsInstance(); } catch (Exception e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "doAPIDeleteFromPAP", " Could not create JPAUtils instance on the PAP"); response.addHeader(ERROR, "jpautils"); response.addHeader(OPERATION, DELETE); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } if (jpaUtils.dbLockdownIgnoreErrors()) { PolicyLogger.warn("Policies are locked down"); response.addHeader(OPERATION, DELETE); response.addHeader("lockdown", "true"); response.setStatus(HttpServletResponse.SC_ACCEPTED); return; } String policyEntityQuery = null; try { if (policyName.endsWith(".xml")) { removeXmlExtension = policyName.replace(".xml", ""); currentVersion = Integer.parseInt(removeXmlExtension.substring(removeXmlExtension.lastIndexOf('.') + 1)); removeVersionExtension = removeXmlExtension.substring(0, removeXmlExtension.lastIndexOf('.')); boolean queryCheck = true; if ("All Versions".equalsIgnoreCase(policy.getDeleteCondition())) { if (policyName.contains(CONFIG)) { splitPolicyName = removeVersionExtension.replace(".Config_", ":Config_"); } else if (policyName.contains(ACTION)) { splitPolicyName = removeVersionExtension.replace(".Action_", ":Action_"); } else if (policyName.contains("Decision_")) { splitPolicyName = removeVersionExtension.replace(".Decision_", ":Decision_"); } if (splitPolicyName != null) { split = splitPolicyName.split(":"); } else { PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy. Please, provide the valid policyname."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } policyEntityQuery = "SELECT p FROM PolicyEntity p WHERE p.policyName LIKE :pName and p.scope=:pScope"; } else if ("Current Version".equalsIgnoreCase(policy.getDeleteCondition())) { if (policyName.contains(CONFIG)) { splitPolicyName = policyName.replace(".Config_", ":Config_"); } else if (policyName.contains(ACTION)) { splitPolicyName = policyName.replace(".Action_", ":Action_"); } else if (policyName.contains("Decision_")) { splitPolicyName = policyName.replace(".Decision_", ":Decision_"); } split = splitPolicyName.split(":"); queryCheck = false; policyEntityQuery = "SELECT p FROM PolicyEntity p WHERE p.policyName=:pName and p.scope=:pScope"; } SimpleBindings params = new SimpleBindings(); if (queryCheck) { params.put("pName", "%" + split[1] + "%"); } else { params.put("pName", split[1]); } params.put("pScope", split[0]); List<?> peResult = commonClassDao.getDataByQuery(policyEntityQuery, params); if (!peResult.isEmpty()) { String getPolicyVersion = "Select p from PolicyVersion p where p.policyName=:pname"; SimpleBindings pvParams = new SimpleBindings(); pvParams.put("pname", removeVersionExtension.replace(".", File.separator)); List<?> pvResult = commonClassDao.getDataByQuery(getPolicyVersion, pvParams); PolicyVersion polVersion = (PolicyVersion) pvResult.get(0); int newVersion = 0; if ("All Versions".equalsIgnoreCase(policy.getDeleteCondition())) { boolean groupCheck = checkPolicyGroupEntity(peResult); if (!groupCheck) { for (Object peData : peResult) { policyEntity = (PolicyEntity) peData; status = deletePolicyEntityData(policyEntity); } } else { status = POLICY_IN_PDP; } switch (status) { case ERROR: PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; case POLICY_IN_PDP: PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; default: try { policyVersionDeleted = true; commonClassDao.delete(polVersion); } catch (Exception e) { LOGGER.error(e.getMessage(), e); policyVersionDeleted = false; } break; } } else if ("Current Version".equalsIgnoreCase(policy.getDeleteCondition())) { boolean groupCheck = checkPolicyGroupEntity(peResult); if (!groupCheck) { policyEntity = (PolicyEntity) peResult.get(0); status = deletePolicyEntityData(policyEntity); } else { status = POLICY_IN_PDP; } if (ERROR.equals(status)) { PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Exception Occured while deleting the Entity from Database."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } else if (POLICY_IN_PDP.equals(status)) { PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); response.addHeader(ERROR, POLICY_IN_PDP); response.setStatus(HttpServletResponse.SC_CONFLICT); return; } else { if (currentVersion > 1) { if (!peResult.isEmpty()) { for (Object object : peResult) { policyEntity = (PolicyEntity) object; String policyEntityName = policyEntity.getPolicyName().replace(".xml", ""); int policyEntityVersion = Integer.parseInt( policyEntityName.substring(policyEntityName.lastIndexOf('.') + 1)); if (policyEntityVersion > newVersion) { newVersion = policyEntityVersion - 1; } } } polVersion.setActiveVersion(newVersion); polVersion.setHigherVersion(newVersion); try { policyVersionDeleted = true; commonClassDao.save(polVersion); } catch (Exception e) { LOGGER.error(e.getMessage(), e); policyVersionDeleted = false; } } else { try { policyVersionDeleted = true; commonClassDao.delete(polVersion); } catch (Exception e) { LOGGER.error(e.getMessage(), e); policyVersionDeleted = false; } } } } } else { PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy for an unknown reason. Check the file system and other logs" + " for further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } } } catch (Exception e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "XACMLPapServlet", " ERROR"); response.addHeader(ERROR, "deleteDB"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } if (policyVersionDeleted) { response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", SUCCESS); response.addHeader(OPERATION, DELETE); } else { PolicyLogger.error(MessageCodes.ERROR_UNKNOWN + "Failed to delete the policy for an unknown reason. Check the file system and other logs for " + "further information."); response.addHeader(ERROR, UNKNOWN); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } @Autowired  DeleteHandler(CommonClassDao commonClassDao);  DeleteHandler(); void doApiDeleteFromPap(HttpServletRequest request, HttpServletResponse response); static String deletePolicyEntityData(PolicyEntity policyEntity); static boolean checkPolicyGroupEntity(List<?> peResult); void doApiDeleteFromPdp(HttpServletRequest request, HttpServletResponse response,
            OnapLoggingContext loggingContext); OnapPDPGroup getDeletedGroup(); boolean preSafetyCheck(PDPPolicy policy); static DeleteHandler getInstance();  }
@Test public void testDoDeletePap() throws IOException { CommonClassDao dao = Mockito.mock(CommonClassDao.class); DeleteHandler handler = new DeleteHandler(dao); MockHttpServletRequest request = new MockHttpServletRequest(); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); MockHttpServletResponse response = new MockHttpServletResponse(); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Bar_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); List<PolicyVersion> pePVs = new ArrayList<PolicyVersion>(); PolicyVersion pv = new PolicyVersion(); pePVs.add(pv); List<Object> peObjs = new ArrayList<Object>(pePVs); List<PolicyEntity> peEnts = new ArrayList<PolicyEntity>(); PolicyEntity peEnt = new PolicyEntity(); peEnts.add(peEnt); List<Object> peEntObjs = new ArrayList<Object>(peEnts); Mockito.when(dao.getDataByQuery(eq("Select p from PolicyVersion p where p.policyName=:pname"), any())) .thenReturn(peObjs); Mockito.when( dao.getDataByQuery(eq("SELECT p FROM PolicyEntity p WHERE p.policyName=:pName and p.scope=:pScope"), any())) .thenReturn(peEntObjs); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); request.setBodyContent( "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n, \"deleteCondition\": \"All Versions\"}\n"); handler.doApiDeleteFromPap(request, response); assertTrue(response.containsHeader("error")); }