use of com.day.cq.replication.ReplicationException in project acs-aem-commons by Adobe-Consulting-Services.
the class TwitterFeedUpdaterImpl method updateTwitterFeedComponents.
@Override
@SuppressWarnings("squid:S3776")
public void updateTwitterFeedComponents(ResourceResolver resourceResolver) {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
List<Resource> twitterResources = findTwitterResources(resourceResolver);
for (Resource twitterResource : twitterResources) {
Page page = pageManager.getContainingPage(twitterResource);
if (page != null) {
Twitter client = page.adaptTo(Twitter.class);
if (client != null) {
try {
ValueMap properties = twitterResource.getValueMap();
String username = properties.get("username", String.class);
if (!StringUtils.isEmpty(username)) {
log.info("Loading Twitter timeline for user {} for component {}.", username, twitterResource.getPath());
List<Status> statuses = client.getUserTimeline(username);
if (statuses != null) {
List<String> tweetsList = new ArrayList<>(statuses.size());
List<String> jsonList = new ArrayList<>(statuses.size());
for (Status status : statuses) {
tweetsList.add(processTweet(status));
jsonList.add(DataObjectFactory.getRawJSON(status));
}
if (!tweetsList.isEmpty()) {
ModifiableValueMap map = twitterResource.adaptTo(ModifiableValueMap.class);
map.put("tweets", tweetsList.toArray(new String[tweetsList.size()]));
map.put("tweetsJson", jsonList.toArray(new String[jsonList.size()]));
twitterResource.getResourceResolver().commit();
handleReplication(twitterResource);
}
}
}
} catch (PersistenceException e) {
log.error("Exception while updating twitter feed on resource:" + twitterResource.getPath(), e);
} catch (ReplicationException e) {
log.error("Exception while replicating twitter feed on resource:" + twitterResource.getPath(), e);
} catch (TwitterException e) {
log.error("Exception while loading twitter feed on resource:" + twitterResource.getPath(), e);
}
} else {
log.warn("Twitter component found on {}, but page cannot be adapted to Twitter API. Check Cloud SErvice configuration", page.getPath());
}
}
}
}
use of com.day.cq.replication.ReplicationException 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.ReplicationException in project APM by Cognifide.
the class ScriptReplicationServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resolver = request.getResourceResolver();
final String searchPath = request.getParameter("fileName");
final String run = request.getParameter("run");
if (StringUtils.isEmpty(searchPath)) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
ServletUtils.writeMessage(response, "error", "File name parameter is required");
return;
}
final Script script = scriptFinder.find(searchPath, resolver);
if (script == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
ServletUtils.writeMessage(response, "error", String.format("Script cannot be found: %s", searchPath));
return;
}
final String scriptPath = script.getPath();
try {
final ModifiableScript modifiableScript = new ModifiableScriptWrapper(resolver, script);
if (PUBLISH_RUN.equals(run)) {
modifiableScript.setPublishRun(true);
}
scriptReplicator.replicate(script, resolver);
ServletUtils.writeMessage(response, "success", String.format("Script '%s' replicated successfully", scriptPath));
} catch (PersistenceException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeMessage(response, "error", String.format("Script '%s' cannot be processed because of" + " repository error: %s", scriptPath, e.getMessage()));
} catch (ExecutionException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeMessage(response, "error", String.format("Script '%s' cannot be processed: %s", scriptPath, e.getMessage()));
} catch (ReplicationException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ServletUtils.writeMessage(response, "error", String.format("Script '%s' cannot be replicated: %s", scriptPath, e.getMessage()));
}
}
use of com.day.cq.replication.ReplicationException in project acs-aem-commons by Adobe-Consulting-Services.
the class MockPageManager method move.
@Override
public Resource move(Resource resource, String destination, String beforeName, boolean shallow, boolean resolveConflict, String[] adjustRefs, String[] publishRefs) throws WCMException {
try {
if (rr == null) {
throw new RuntimeException("Resource resolver was null");
}
if (replicator == null) {
throw new RuntimeException("Replicator was not changed out -- will not work properly");
}
replicator.replicate(null, ReplicationActionType.DEACTIVATE, resource.getPath());
replicator.replicate(null, ReplicationActionType.ACTIVATE, destination + "/" + resource.getName());
return resource;
} catch (ReplicationException ex) {
throw new WCMException(ex);
}
}
use of com.day.cq.replication.ReplicationException 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