Search in sources :

Example 1 with ReplicationActionType

use of com.day.cq.replication.ReplicationActionType in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlusherImplTest method testFlush_2.

@Test
public void testFlush_2() throws Exception {
    final ResourceResolver resourceResolver = mock(ResourceResolver.class);
    final Session session = mock(Session.class);
    when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
    final ReplicationActionType actionType = ReplicationActionType.DELETE;
    final boolean synchronous = false;
    final String path1 = "/content/foo";
    final String path2 = "/content/bar";
    dispatcherFlusher.flush(resourceResolver, actionType, synchronous, path1, path2);
    verify(replicator, times(1)).replicate(eq(session), eq(actionType), eq(path1), any(ReplicationOptions.class));
    verify(replicator, times(1)).replicate(eq(session), eq(actionType), eq(path2), any(ReplicationOptions.class));
    verifyNoMoreInteractions(replicator);
}
Also used : ReplicationOptions(com.day.cq.replication.ReplicationOptions) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ReplicationActionType(com.day.cq.replication.ReplicationActionType) Session(javax.jcr.Session) Test(org.junit.Test)

Example 2 with ReplicationActionType

use of com.day.cq.replication.ReplicationActionType in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlushRulesImplTest method testConfigureReplicationActionType_DEACTIVATE.

@Test
public void testConfigureReplicationActionType_DEACTIVATE() throws Exception {
    final ReplicationActionType expected = ReplicationActionType.DEACTIVATE;
    final ReplicationActionType actual = dispatcherFlushRules.configureReplicationActionType("DEACTIVATE");
    assertEquals(expected, actual);
}
Also used : ReplicationActionType(com.day.cq.replication.ReplicationActionType) Test(org.junit.Test)

Example 3 with ReplicationActionType

use of com.day.cq.replication.ReplicationActionType in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlushRulesImplTest method testConfigureReplicationActionType_ACTIVATE.

@Test
public void testConfigureReplicationActionType_ACTIVATE() throws Exception {
    final ReplicationActionType expected = ReplicationActionType.ACTIVATE;
    final ReplicationActionType actual = dispatcherFlushRules.configureReplicationActionType("ACTIVATE");
    assertEquals(expected, actual);
}
Also used : ReplicationActionType(com.day.cq.replication.ReplicationActionType) Test(org.junit.Test)

Example 4 with ReplicationActionType

use of com.day.cq.replication.ReplicationActionType in project acs-aem-commons by Adobe-Consulting-Services.

the class PageRelocator method performNecessaryReplication.

private void performNecessaryReplication(ResourceResolver rr, String path) throws ReplicationException {
    ReplicationActionType action;
    if (path.startsWith(sourcePath)) {
        action = ReplicationActionType.DEACTIVATE;
    } else {
        action = ReplicationActionType.ACTIVATE;
    }
    long start = System.currentTimeMillis();
    if (!dryRun) {
        replicator.replicate(rr.adaptTo(Session.class), action, path);
    }
    long end = System.currentTimeMillis();
    if (path.startsWith(sourcePath)) {
        note(path, Report.deactivate_time, end - start);
    } else {
        note(reversePathLookup(path), Report.activate_time, end - start);
    }
}
Also used : ReplicationActionType(com.day.cq.replication.ReplicationActionType) Session(javax.jcr.Session)

Example 5 with ReplicationActionType

use of com.day.cq.replication.ReplicationActionType in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlusherServlet method doPost.

@Override
@SuppressWarnings("squid:S3776")
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final Resource resource = request.getResource();
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    final Page currentPage = pageManager.getContainingPage(resource);
    final ValueMap properties = resource.getValueMap();
    /* Properties */
    final String[] paths = properties.get("paths", new String[0]);
    final ReplicationActionType replicationActionType = ReplicationActionType.valueOf(properties.get("replicationActionType", ReplicationActionType.ACTIVATE.name()));
    final List<FlushResult> overallResults = new ArrayList<FlushResult>();
    boolean caughtException = false;
    ResourceResolver flushingResourceResolver = null;
    try {
        if (paths.length > 0) {
            if (flushWithAdminResourceResolver) {
                // Use the admin resource resolver for replication to ensure all
                // replication permission checks are OK
                // Make sure to close this resource resolver
                flushingResourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
            } else {
                // Use the HTTP Request's resource resolver; don't close this resource resolver
                flushingResourceResolver = resourceResolver;
            }
            final Map<Agent, ReplicationResult> results = dispatcherFlusher.flush(flushingResourceResolver, replicationActionType, true, paths);
            for (final Map.Entry<Agent, ReplicationResult> entry : results.entrySet()) {
                final Agent agent = entry.getKey();
                final ReplicationResult result = entry.getValue();
                overallResults.add(new FlushResult(agent, result));
            }
        }
    } catch (ReplicationException e) {
        log.error("Replication exception occurred during Dispatcher Flush request.", e);
        caughtException = true;
    } catch (LoginException e) {
        log.error("Could not obtain an Admin Resource Resolver during Dispatcher Flush request.", e);
        caughtException = true;
    } finally {
        if (flushWithAdminResourceResolver && flushingResourceResolver != null) {
            // Close the admin resource resolver if opened by this servlet
            flushingResourceResolver.close();
        }
    }
    if (request.getRequestPathInfo().getExtension().equals("json")) {
        response.setContentType("application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        try {
            writer.object();
            for (final FlushResult result : overallResults) {
                writer.key(result.agentId);
                writer.value(result.success);
            }
            writer.endObject();
        } catch (JSONException e) {
            throw new ServletException("Unable to output JSON data", e);
        }
    } else {
        String suffix;
        if (caughtException) {
            suffix = "replication-error";
        } else {
            suffix = StringUtils.join(overallResults, '/');
        }
        response.sendRedirect(request.getContextPath() + currentPage.getPath() + ".html/" + suffix);
    }
}
Also used : JSONWriter(org.apache.sling.commons.json.io.JSONWriter) Agent(com.day.cq.replication.Agent) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) Page(com.day.cq.wcm.api.Page) ServletException(javax.servlet.ServletException) ReplicationResult(com.day.cq.replication.ReplicationResult) PageManager(com.day.cq.wcm.api.PageManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) ReplicationException(com.day.cq.replication.ReplicationException) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) ReplicationActionType(com.day.cq.replication.ReplicationActionType)

Aggregations

ReplicationActionType (com.day.cq.replication.ReplicationActionType)10 Test (org.junit.Test)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Session (javax.jcr.Session)2 LoginException (org.apache.sling.api.resource.LoginException)2 WorkflowException (com.adobe.granite.workflow.WorkflowException)1 Asset (com.day.cq.dam.api.Asset)1 Agent (com.day.cq.replication.Agent)1 ReplicationException (com.day.cq.replication.ReplicationException)1 ReplicationOptions (com.day.cq.replication.ReplicationOptions)1 ReplicationResult (com.day.cq.replication.ReplicationResult)1 Page (com.day.cq.wcm.api.Page)1 PageManager (com.day.cq.wcm.api.PageManager)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 RepositoryException (javax.jcr.RepositoryException)1