ContainerServlet extends SlingAllMethodsServlet { @Override protected void doPost(SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException { ResourceResolver resolver = request.getResourceResolver(); Resource container = request.getResource(); try { String[] deletedChildrenNames = request.getParameterValues(PARAM_DELETED_CHILDREN); if (deletedChildrenNames != null && deletedChildrenNames.length > 0) { for (String childName: deletedChildrenNames) { Resource child = container.getChild(childName); if (child != null) { resolver.delete(child); } } } resolver.commit(); } catch (PersistenceException e) { LOGGER.error("Could not delete items of the container at {}", container.getPath(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } try { String[] orderedChildrenNames = request.getParameterValues(PARAM_ORDERED_CHILDREN); if (orderedChildrenNames != null && orderedChildrenNames.length > 0) { final Node containerNode = container.adaptTo(Node.class); if (containerNode != null) { for (int i = 0; i < orderedChildrenNames.length; i++) { if (!containerNode.hasNode(orderedChildrenNames[i])) { containerNode.addNode(orderedChildrenNames[i]); } } for (int i = orderedChildrenNames.length - 1; i >= 0; i--) { if (i == orderedChildrenNames.length - 1) { containerNode.orderBefore(orderedChildrenNames[i], null); } else { containerNode.orderBefore(orderedChildrenNames[i], orderedChildrenNames[i+1]); } } resolver.commit(); } } } catch (RepositoryException | PersistenceException e) { LOGGER.error("Could not order items of the container at {}", container.getPath(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }    }
@Test public void testDeleteOneChild() throws ServletException, IOException { context.request().getParameterMap().put(PARAM_DELETED_CHILDREN, new String[]{"item_1"}); servlet.doPost(context.request(), context.response()); assertNull(context.currentResource().getChild("item_1"), "Deleted child 'item_1' still exists"); }
@Test public void testDeleteMultipleChildren() throws ServletException, IOException { context.request().getParameterMap().put(PARAM_DELETED_CHILDREN, new String[]{"item_1","item_3"}); servlet.doPost(context.request(), context.response()); assertNull(context.currentResource().getChild("item_1"), "Deleted child 'item_1' still exists"); assertNotNull(context.currentResource().getChild("item_2"), "Child 'item_2' was deleted but should still exist"); assertNull(context.currentResource().getChild("item_3"), "Deleted child 'item_3' still exists"); }
@Test public void testDeleteUnknownChild() throws ServletException, IOException { context.request().getParameterMap().put(PARAM_DELETED_CHILDREN, new String[]{"item_XXXX"}); servlet.doPost(context.request(), context.response()); }
@Test public void testDeleteEmptyParam() throws ServletException, IOException { context.request().getParameterMap().put(PARAM_DELETED_CHILDREN, new String[]{}); servlet.doPost(context.request(), context.response()); }
@Test public void testOrderChildren() throws ServletException, IOException { String[] reorderedChildren = new String[]{"item_3","item_2","item_1"}; context.request().getParameterMap().put(PARAM_ORDERED_CHILDREN, reorderedChildren); servlet.doPost(context.request(), context.response()); Iterable<Resource> childrenIterator = context.currentResource().getChildren(); Iterator<String> expectedIterator = Arrays.asList(reorderedChildren).iterator(); for (Resource resource : childrenIterator) { assertEquals(resource.getName(), expectedIterator.next(), "Reordering children failed"); } }