Search in sources :

Example 1 with ReplicationException

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());
            }
        }
    }
}
Also used : Status(twitter4j.Status) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Page(com.day.cq.wcm.api.Page) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) PageManager(com.day.cq.wcm.api.PageManager) PersistenceException(org.apache.sling.api.resource.PersistenceException) ReplicationException(com.day.cq.replication.ReplicationException) TwitterException(twitter4j.TwitterException)

Example 2 with ReplicationException

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);
    }
}
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)

Example 3 with ReplicationException

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()));
    }
}
Also used : ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) Script(com.cognifide.cq.cqsm.api.scripts.Script) ModifiableScript(com.cognifide.cq.cqsm.api.scripts.ModifiableScript) ModifiableScriptWrapper(com.cognifide.cq.cqsm.core.scripts.ModifiableScriptWrapper) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) ReplicationException(com.day.cq.replication.ReplicationException) ExecutionException(com.cognifide.cq.cqsm.api.exceptions.ExecutionException)

Example 4 with ReplicationException

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);
    }
}
Also used : ReplicationException(com.day.cq.replication.ReplicationException) WCMException(com.day.cq.wcm.api.WCMException)

Example 5 with ReplicationException

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;
}
Also used : ReplicationResult(com.adobe.acs.commons.replication.ReplicationResult) ReplicationOptions(com.day.cq.replication.ReplicationOptions) AgentIdFilter(com.day.cq.replication.AgentIdFilter) Version(javax.jcr.version.Version) ReplicateVersion(com.adobe.acs.commons.replication.ReplicateVersion) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) ReplicationException(com.day.cq.replication.ReplicationException) Session(javax.jcr.Session)

Aggregations

ReplicationException (com.day.cq.replication.ReplicationException)5 ArrayList (java.util.ArrayList)3 Resource (org.apache.sling.api.resource.Resource)3 Page (com.day.cq.wcm.api.Page)2 PageManager (com.day.cq.wcm.api.PageManager)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 ReplicateVersion (com.adobe.acs.commons.replication.ReplicateVersion)1 ReplicationResult (com.adobe.acs.commons.replication.ReplicationResult)1 ExecutionException (com.cognifide.cq.cqsm.api.exceptions.ExecutionException)1 ModifiableScript (com.cognifide.cq.cqsm.api.scripts.ModifiableScript)1 Script (com.cognifide.cq.cqsm.api.scripts.Script)1 ModifiableScriptWrapper (com.cognifide.cq.cqsm.core.scripts.ModifiableScriptWrapper)1 Agent (com.day.cq.replication.Agent)1 AgentIdFilter (com.day.cq.replication.AgentIdFilter)1 ReplicationActionType (com.day.cq.replication.ReplicationActionType)1 ReplicationOptions (com.day.cq.replication.ReplicationOptions)1 ReplicationResult (com.day.cq.replication.ReplicationResult)1 WCMException (com.day.cq.wcm.api.WCMException)1