@Test public void testMultipleReferencesReferenceToPages() throws Exception { Map<String, Object> map = new HashMap<String, Object>(); String path = "\"/content/geometrixx/en\",\"/content/geometrixx/en/toolbar\""; String path1 = "/content/geometrixx/en/toolbar"; map.put("path", path); // map.put("path1", path1); ValueMap vm = new ValueMapDecorator(map); when(resource.getValueMap()).thenReturn(vm); when(resource.getResourceResolver()).thenReturn(resolver); when(resolver.getResource(path)).thenReturn(res); when(resolver.getResource(path1)).thenReturn(res1); when(res.adaptTo(Page.class)).thenReturn(referredpage); when(res1.adaptTo(Page.class)).thenReturn(referredpage1); when(referredpage.getName()).thenReturn("geometrixx"); when(referredpage1.getName()).thenReturn("geometrixx1"); when(referredpage1.getPath()).thenReturn(path1); when(manager.getContainingPage(path1)).thenReturn(referredpage1); Calendar cal = GregorianCalendar.getInstance(); when(referredpage.getLastModified()).thenReturn(cal); when(referredpage1.getLastModified()).thenReturn(cal); List<Reference> actual = instance.findReferences(resource); assertNotNull(actual); assertEquals(2, actual.size()); boolean geometrixxFound = false; boolean geometrixxOneFound = false; for (Reference ref : actual) { if (ref.getName().equals("geometrixx (Page)")) { geometrixxFound = true; } else if (ref.getName().equals("geometrixx1 (Page)")) { geometrixxOneFound = true; } } assertTrue(geometrixxFound); assertTrue(geometrixxOneFound); }
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); TidyJSONWriter writer = new TidyJSONWriter(response.getWriter()); writer.setTidy("true".equals(request.getParameter(TIDY))); ResourceResolver resolver = request.getResourceResolver(); Session session = resolver.adaptTo(Session.class); String paths[] = request.getParameterValues(PATH); List<Reference> allReferences = new ArrayList<Reference>(); if (paths != null) { // search all refs that may be contained in one of the passed paths for (String path : paths) { if (path.length() > 0) { // get content node Resource r = resolver.getResource(path + "/" + JcrConstants.JCR_CONTENT); if (r == null) { r = resolver.getResource(path); } if (r == null) { continue; } for (ReferenceProvider referenceProvider : referenceProviders) { allReferences.addAll(referenceProvider.findReferences(r)); } } } } try { writer.object(); writer.key("assets"); writer.array(); for (Reference reference : allReferences) { boolean published = false; boolean outdated = false; ReplicationStatus replStatus = null; final Resource resource = reference.getResource(); boolean canReplicate = canReplicate(reference.getResource().getPath(), session); long lastPublished = 0; if (resource != null) { replStatus = resource.adaptTo(ReplicationStatus.class); if (replStatus != null) { published = replStatus.isDelivered() || replStatus.isActivated(); if (published) { lastPublished = replStatus.getLastPublished().getTimeInMillis(); outdated = lastPublished < reference.getLastModified(); } } log.debug( "Considering reference at {} . Published: {}, outdated: {} ( lastPublished: {}, lastModified: {} )", new Object[] { reference.getResource().getPath(), published, outdated, new Date(lastPublished), new Date(reference.getLastModified()) }); } if (!published || outdated) { writer.object(); writer.key("type").value(reference.getType()); writer.key("path").value(reference.getResource().getPath()); writer.key("name").value(reference.getName()); if (replStatus != null) { writer.key("published").value(published); } writer.key("outdated").value(outdated); writer.key("status").value(outdated ? "outdated" : "not available"); writer.key("disabled").value(!canReplicate); writer.endObject(); } } writer.endArray(); writer.endObject(); } catch (Exception e) { throw new ServletException(e); } }