private void writeReplicationState(JSONWriter out, Resource resource) throws Exception { out.key("replication").object(); ReplicationStatus replicationStatus = resource.adaptTo(ReplicationStatus.class); if (replicationStatus != null) { Calendar published = replicationStatus.getLastPublished(); if (published != null) { out.key("published").value(published.getTimeInMillis()); } if (replicationStatus.getLastReplicationAction() != null) { String action = replicationStatus.getLastReplicationAction().name(); if (action != null && action.length() > 0) { out.key("action").value(action); } } } out.endObject(); }
@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); } }