use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.
the class ReplicateVersionServlet method doPost.
@Override
public final void doPost(SlingHttpServletRequest req, SlingHttpServletResponse res) throws ServletException, IOException {
log.debug("Entering ReplicatePageVersionServlet.doPost(..)");
String[] rootPaths = req.getParameterValues("rootPaths");
Date date = getDate(req.getParameter("datetimecal"));
String[] agents = req.getParameterValues("cmbAgent");
JsonObject obj = validate(rootPaths, agents, date);
if (!obj.has(KEY_ERROR)) {
log.debug("Initiating version replication");
List<ReplicationResult> response = replicateVersion.replicate(req.getResourceResolver(), rootPaths, agents, date);
if (log.isDebugEnabled()) {
for (final ReplicationResult replicationResult : response) {
log.debug("Replication result: {} -- {}", replicationResult.getPath(), replicationResult.getStatus());
}
}
JsonArray arr = convertResponseToJson(response);
obj = new JsonObject();
obj.add(KEY_RESULT, arr);
} else {
log.debug("Did not attempt to replicate version due to issue with input params");
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
obj.addProperty(KEY_STATUS, KEY_ERROR);
}
res.setContentType("application/json");
res.getWriter().print(obj.toString());
}
use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.
the class ReplicateVersionServlet method convertResponseToJson.
private JsonArray convertResponseToJson(List<ReplicationResult> list) {
JsonArray arr = new JsonArray();
for (ReplicationResult result : list) {
JsonObject resultObject = new JsonObject();
resultObject.addProperty(KEY_PATH, result.getPath());
resultObject.addProperty(KEY_STATUS, result.getStatus().name());
resultObject.addProperty(KEY_VERSION, result.getVersion());
arr.add(resultObject);
}
return arr;
}
use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.
the class ReplicateVersionImplTest method testReplicate.
@Test
public final void testReplicate() throws Exception {
final String[] agents = new String[] { "agent1" };
final String[] rootPaths = new String[] { "/content/geometrixx/en" };
final Date date = getDate("2013-12-21T00:00:00");
final Date vDate = getDate("2013-12-01T00:00:00");
final Date vDate1 = getDate("2013-12-22T00:00:00");
final Calendar cal = GregorianCalendar.getInstance();
cal.setTime(vDate);
final Calendar cal1 = GregorianCalendar.getInstance();
cal1.setTime(vDate1);
final ResourceResolver resourceResolver = mock(ResourceResolver.class);
final Session session = mock(Session.class);
final Workspace wk = mock(Workspace.class);
final VersionManager vm = mock(VersionManager.class);
final VersionHistory vh = mock(VersionHistory.class);
final VersionIterator vi = mock(VersionIterator.class);
final Version v = mock(Version.class);
final Version v1 = mock(Version.class);
when(v.getCreated()).thenReturn(cal);
when(v1.getCreated()).thenReturn(cal1);
when(v.getName()).thenReturn("version1");
when(vi.nextVersion()).thenReturn(v, v1);
when(vi.hasNext()).thenReturn(true, true, false);
when(session.getWorkspace()).thenReturn(wk);
when(wk.getVersionManager()).thenReturn(vm);
when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
when(vm.getVersionHistory(rootPaths[0])).thenReturn(vh);
when(vh.getAllVersions()).thenReturn(vi);
final Resource res = mock(Resource.class);
when(res.getPath()).thenReturn(rootPaths[0]);
final Node node = mock(Node.class);
when(resourceResolver.getResource(rootPaths[0])).thenReturn(res);
when(res.adaptTo(Node.class)).thenReturn(node);
when(session.getNode(rootPaths[0])).thenReturn(node);
when(node.hasNode(NameConstants.NN_CONTENT)).thenReturn(true);
final Node node1 = mock(Node.class);
when(node1.getPath()).thenReturn(rootPaths[0]);
when(node.getNode(NameConstants.NN_CONTENT)).thenReturn(node1);
when(node1.isNodeType(JcrConstants.MIX_VERSIONABLE)).thenReturn(true);
when(node.isNodeType("nt:hierarchyNode")).thenReturn(true);
final Resource res1 = mock(Resource.class);
final Resource res2 = mock(Resource.class);
@SuppressWarnings("unchecked") final Iterator<Resource> itr = mock(Iterator.class);
when(resourceResolver.listChildren(res)).thenReturn(itr);
when(itr.hasNext()).thenReturn(true, true, false);
when(itr.next()).thenReturn(res1, res2);
when(res1.adaptTo(Node.class)).thenReturn(node1);
when(res2.adaptTo(Node.class)).thenReturn(node1);
when(node1.isNodeType("nt:hierarchyNode")).thenReturn(false);
List<ReplicationResult> list = rpvs.replicate(resourceResolver, rootPaths, agents, date);
Assert.assertEquals("/content/geometrixx/en", list.get(0).getPath());
Assert.assertEquals(Status.replicated, list.get(0).getStatus());
Assert.assertEquals("version1", list.get(0).getVersion());
}
use of com.adobe.acs.commons.replication.ReplicationResult in project acs-aem-commons by Adobe-Consulting-Services.
the class ReplicateVersionImpl method replicateResource.
private List<ReplicationResult> replicateResource(ResourceResolver resolver, List<Resource> resources, String[] agents, Date date) {
List<ReplicationResult> results = new ArrayList<ReplicationResult>();
ReplicationOptions opts = new ReplicationOptions();
AgentIdFilter agentFilter = new AgentIdFilter(agents);
opts.setFilter(agentFilter);
Session session = resolver.adaptTo(Session.class);
for (Resource resource : resources) {
String path = resource.getPath();
try {
Version v = getAppropriateVersion(resource, date, session);
if (v == null) {
results.add(new ReplicationResult(path, Status.not_replicated));
continue;
}
String versionName = v.getName();
opts.setRevision(versionName);
replicator.replicate(session, ReplicationActionType.ACTIVATE, path, opts);
results.add(new ReplicationResult(path, Status.replicated, versionName));
} catch (RepositoryException e) {
results.add(new ReplicationResult(path, Status.error));
log.error("Exception while replicating version of " + path, e);
} catch (ReplicationException e) {
results.add(new ReplicationResult(path, Status.error));
log.error("Exception while replicating version of " + path, e);
}
}
return results;
}
Aggregations