use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class WCMUsePojoBaseTest method getResourceBackedBindings.
/**
* <p>
* Creates a {@link Bindings} map initialised with the following default bindings available to Sightly use objects based on {@link
* WCMUsePojo}:
* </p>
* <ul>
* <li>{@link SlingBindings#RESOURCE}</li>
* <li>{@link SlingBindings#REQUEST}</li>
* <li>{@link SlingBindings#RESPONSE}</li>
* <li>{@link WCMBindings#PROPERTIES}</li>
* <li>{@link WCMBindings#WCM_MODE}</li>
* <li>{@link WCMBindings#PAGE_MANAGER}</li>
* <li>{@link WCMBindings#RESOURCE_PAGE}</li>
* <li>{@link WCMBindings#CURRENT_PAGE}</li>
* <li>{@link WCMBindings#PAGE_PROPERTIES}</li>
* </ul>
*
* @param resourcePath the path to a resource already loaded in the testing context
* @return the bindings map
*/
protected Bindings getResourceBackedBindings(String resourcePath) {
Bindings bindings = getDefaultSlingBindings();
Resource resource = context.resourceResolver().getResource(resourcePath);
if (resource != null) {
ValueMap properties = resource.adaptTo(ValueMap.class);
bindings.put(SlingBindings.RESOURCE, resource);
bindings.put(WCMBindings.PROPERTIES, properties);
bindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(context.request()));
PageManager pageManager = context.pageManager();
bindings.put(WCMBindings.PAGE_MANAGER, pageManager);
context.request().setResource(resource);
Page resourcePage = pageManager.getContainingPage(resource);
if (resourcePage != null) {
bindings.put(WCMBindings.RESOURCE_PAGE, resourcePage);
bindings.put(WCMBindings.CURRENT_PAGE, resourcePage);
bindings.put(WCMBindings.PAGE_PROPERTIES, properties);
}
} else {
throw new IllegalArgumentException("Cannot find a resource at " + resourcePath);
}
return bindings;
}
use of com.day.cq.wcm.api.PageManager in project CQ-Actions by Cognifide.
the class HandleMessageJob method process.
@Override
public JobResult process(Job job) {
final String path = (String) job.getProperty(SlingConstants.PROPERTY_PATH);
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getAdministrativeResourceResolver(null);
final PageManager pm = resolver.adaptTo(PageManager.class);
final Page page = pm.getPage(path);
final String actionType;
if (page != null && page.getContentResource() != null) {
actionType = page.getContentResource().getResourceType();
consumeMessage(actionType, page.getContentResource());
} else {
LOG.debug("Empty resource type for action page: " + path);
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
} finally {
if (resolver != null) {
resolver.close();
}
}
return JobResult.OK;
}
use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.
the class AemObjectInjector method getResourcePage.
private Page getResourcePage(Object adaptable) {
PageManager pageManager = getPageManager(adaptable);
Resource resource = getResource(adaptable);
if (pageManager != null && resource != null) {
return pageManager.getContainingPage(resource);
}
return null;
}
use of com.day.cq.wcm.api.PageManager in project acs-aem-commons by Adobe-Consulting-Services.
the class WorkflowPackageManagerImpl method create.
/**
* {@inheritDoc}
*/
public final Page create(final ResourceResolver resourceResolver, String bucketSegment, final String name, final String... paths) throws WCMException, RepositoryException {
final Session session = resourceResolver.adaptTo(Session.class);
final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
String bucketPath = "/etc/workflow/packages";
if (StringUtils.isNotBlank(bucketSegment)) {
bucketPath += "/" + bucketSegment;
}
final Node shardNode = JcrUtils.getOrCreateByPath(bucketPath, NT_SLING_FOLDER, NT_SLING_FOLDER, session, false);
final Page page = pageManager.create(shardNode.getPath(), JcrUtil.createValidName(name), WORKFLOW_PACKAGE_TEMPLATE, name, false);
final Resource contentResource = page.getContentResource();
Node node = JcrUtil.createPath(contentResource.getPath() + "/" + NN_VLT_DEFINITION, NT_VLT_DEFINITION, session);
node = JcrUtil.createPath(node.getPath() + "/filter", JcrConstants.NT_UNSTRUCTURED, session);
JcrUtil.setProperty(node, SLING_RESOURCE_TYPE, FILTER_RESOURCE_TYPE);
int i = 0;
Node resourceNode = null;
for (final String path : paths) {
if (path != null) {
resourceNode = JcrUtil.createPath(node.getPath() + "/resource_" + i++, JcrConstants.NT_UNSTRUCTURED, session);
JcrUtil.setProperty(resourceNode, "root", path);
JcrUtil.setProperty(resourceNode, "rules", this.getIncludeRules(path));
JcrUtil.setProperty(resourceNode, SLING_RESOURCE_TYPE, FILTER_RESOURCE_RESOURCE_TYPE);
}
}
session.save();
return page;
}
use of com.day.cq.wcm.api.PageManager 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());
}
}
}
}
Aggregations