use of com.day.cq.replication.Agent in project acs-aem-commons by Adobe-Consulting-Services.
the class DispatcherFlushFilterTest method setUp.
@Before
public void setUp() throws Exception {
agent = mock(Agent.class);
agentConfig = mock(AgentConfig.class);
when(agent.getId()).thenReturn("mock-agent");
when(agent.getConfiguration()).thenReturn(agentConfig);
Map<String, Object> tmp = new HashMap<String, Object>();
tmp.put(AgentConfig.PROTOCOL_HTTP_HEADERS, new String[] { "CQ-Action:{action}", "CQ-Handle:{path}", "CQ-Path: {path}" });
hierarchicalProperties = new ValueMapDecorator(tmp);
tmp = new HashMap<String, Object>();
tmp.put(AgentConfig.PROTOCOL_HTTP_HEADERS, new String[] { "CQ-Action:{action}", "CQ-Handle:{path}", "CQ-Path: {path}", "CQ-Action-Scope: ResourceOnly" });
resourceOnlyProperties = new ValueMapDecorator(tmp);
tmp = new HashMap<String, Object>();
tmp.put(AgentConfig.PROTOCOL_HTTP_HEADERS, new String[] { "Foo-Action:{action}", "Foo-Handle:{path}", "Foo-Path: {path}" });
invalidProperties = new ValueMapDecorator(tmp);
}
use of com.day.cq.replication.Agent in project acs-aem-commons by Adobe-Consulting-Services.
the class DispatcherFlusherImplTest method testGetFlushAgents.
@Test
public void testGetFlushAgents() throws Exception {
final Agent agent1 = mock(Agent.class);
final Agent agent2 = mock(Agent.class);
final AgentConfig agentConfig1 = mock(AgentConfig.class);
final AgentConfig agentConfig2 = mock(AgentConfig.class);
@SuppressWarnings("unchecked") final Map<String, Agent> agents = mock(Map.class);
final Collection<Agent> agentValues = Arrays.asList(new Agent[] { agent1, agent2 });
when(agentManager.getAgents()).thenReturn(agents);
when(agents.values()).thenReturn(agentValues);
when(agent1.getId()).thenReturn("Agent 1");
when(agent1.isEnabled()).thenReturn(true);
when(agent1.getConfiguration()).thenReturn(agentConfig1);
when(agent2.getId()).thenReturn("Agent 2");
when(agent2.isEnabled()).thenReturn(true);
when(agent2.getConfiguration()).thenReturn(agentConfig2);
when(agentConfig1.getSerializationType()).thenReturn("flush");
when(agentConfig2.getSerializationType()).thenReturn("notflush");
when(agentConfig1.getTransportURI()).thenReturn("http://localhost/dispatcher/invalidate.cache");
when(agentConfig2.getTransportURI()).thenReturn("ftp://localhost/dispatcher/invalidate.cache");
Map<String, Object> tmp = new HashMap<String, Object>();
tmp.put(AgentConfig.PROTOCOL_HTTP_HEADERS, new String[] { "CQ-Action:{action}", "CQ-Handle:{path}", "CQ-Path: {path}" });
when(agentConfig1.getProperties()).thenReturn(new ValueMapDecorator(tmp));
when(agentConfig2.getProperties()).thenReturn(new ValueMapDecorator(tmp));
final Agent[] actual = dispatcherFlusher.getFlushAgents();
assertEquals(1, actual.length);
assertEquals("Agent 1", actual[0].getId());
}
use of com.day.cq.replication.Agent 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);
}
}
use of com.day.cq.replication.Agent in project acs-aem-commons by Adobe-Consulting-Services.
the class AgentHostsImpl method getHosts.
@Override
public final List<String> getHosts(final AgentFilter agentFilter) {
final List<String> hosts = new ArrayList<String>();
final Map<String, Agent> agents = agentManager.getAgents();
for (final Agent agent : agents.values()) {
if (!agentFilter.isIncluded(agent)) {
continue;
}
try {
final URI uri = new URI(agent.getConfiguration().getTransportURI());
String tmp = StringUtils.defaultIfEmpty(uri.getScheme(), DEFAULT_SCHEME) + "://" + uri.getHost();
if (uri.getPort() > 0) {
tmp += ":" + uri.getPort();
}
hosts.add(tmp);
} catch (URISyntaxException e) {
log.error("Unable to extract a scheme/host/port from Agent transport URI [ {} ]", agent.getConfiguration().getTransportURI());
}
}
return hosts;
}
Aggregations