use of com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServlet method getContentPolicy.
/**
* Returns the content policy bound to the given component.
*
* @param imageResource the resource identifying the accessed image component
* @return the content policy. May be {@code nulll} in case no content policy can be found.
*/
private ContentPolicy getContentPolicy(@NotNull Resource imageResource) {
ResourceResolver resourceResolver = imageResource.getResourceResolver();
ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
if (policyManager != null) {
ComponentManager componentManager = resourceResolver.adaptTo(ComponentManager.class);
if (componentManager != null) {
com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(imageResource);
if (component != null && component.getProperties() != null) {
String delegatingResourceType = component.getProperties().get(AbstractImageDelegatingModel.IMAGE_DELEGATE, String.class);
if (StringUtils.isNotEmpty(delegatingResourceType)) {
imageResource = new CoreResourceWrapper(imageResource, delegatingResourceType);
}
}
}
return policyManager.getPolicy(imageResource);
} else {
LOGGER.warn("Could not get policy manager from resource resolver!");
}
return null;
}
use of com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class PageListItemImpl method getTeaserResource.
@Override
@JsonIgnore
public Resource getTeaserResource() {
if (teaserResource == null && component != null) {
String delegateResourceType = component.getProperties().get(PN_TEASER_DELEGATE, String.class);
if (StringUtils.isEmpty(delegateResourceType)) {
LOGGER.error("In order for list rendering delegation to work correctly you need to set up the teaserDelegate property on" + " the {} component; its value has to point to the resource type of a teaser component.", component.getPath());
} else {
Resource resourceToBeWrapped = ComponentUtils.getFeaturedImage(page);
if (resourceToBeWrapped != null) {
// use the page featured image and inherit properties from the page item
overriddenProperties.put(JcrConstants.JCR_TITLE, this.getTitle());
if (showDescription) {
overriddenProperties.put(JcrConstants.JCR_DESCRIPTION, this.getDescription());
}
if (linkItems) {
overriddenProperties.put(ImageResource.PN_LINK_URL, this.getPath());
}
} else {
// use the page content node and inherit properties from the page item
resourceToBeWrapped = page.getContentResource();
if (resourceToBeWrapped == null) {
return null;
}
if (!showDescription) {
hiddenProperties.add(JcrConstants.JCR_DESCRIPTION);
}
if (linkItems) {
overriddenProperties.put(ImageResource.PN_LINK_URL, this.getPath());
}
}
teaserResource = new CoreResourceWrapper(resourceToBeWrapped, delegateResourceType, hiddenProperties, overriddenProperties);
}
}
return teaserResource;
}
use of com.adobe.cq.wcm.core.components.internal.resource.CoreResourceWrapper in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class Utils method getWrappedImageResourceWithInheritance.
/**
* Wraps an image resource with the properties and child resources of the inherited featured image of either
* the linked page or the page containing the resource.
*
* @param resource The image resource
* @param linkHandler The link handler
* @param currentStyle The style of the image resource
* @param currentPage The page containing the image resource
* @return The wrapped image resource augmented with inherited properties and child resource if inheritance is enabled, the plain image resource otherwise.
*/
public static Resource getWrappedImageResourceWithInheritance(Resource resource, LinkHandler linkHandler, Style currentStyle, Page currentPage) {
if (resource == null) {
LOGGER.error("The resource is not defined");
return null;
}
if (linkHandler == null) {
LOGGER.error("The link handler is not defined");
return null;
}
ValueMap properties = resource.getValueMap();
String fileReference = properties.get(DownloadResource.PN_REFERENCE, String.class);
Resource fileResource = resource.getChild(DownloadResource.NN_FILE);
boolean imageFromPageImage = properties.get(PN_IMAGE_FROM_PAGE_IMAGE, StringUtils.isEmpty(fileReference) && fileResource == null);
boolean altValueFromPageImage = properties.get(PN_ALT_VALUE_FROM_PAGE_IMAGE, imageFromPageImage);
if (imageFromPageImage) {
Resource inheritedResource = null;
String linkURL = properties.get(ImageResource.PN_LINK_URL, String.class);
boolean actionsEnabled = (currentStyle != null) ? !currentStyle.get(Teaser.PN_ACTIONS_DISABLED, !properties.get(Teaser.PN_ACTIONS_ENABLED, true)) : properties.get(Teaser.PN_ACTIONS_ENABLED, true);
Resource firstAction = Optional.of(resource).map(res -> res.getChild(Teaser.NN_ACTIONS)).map(actions -> actions.getChildren().iterator().next()).orElse(null);
if (StringUtils.isNotEmpty(linkURL)) {
// the inherited resource is the featured image of the linked page
Optional<Link> link = linkHandler.getLink(resource);
inheritedResource = link.map(link1 -> (Page) link1.getReference()).map(ComponentUtils::getFeaturedImage).orElse(null);
} else if (actionsEnabled && firstAction != null) {
// the inherited resource is the featured image of the first action's page (the resource is assumed to be a teaser)
inheritedResource = Optional.of(linkHandler.getLink(firstAction, Teaser.PN_ACTION_LINK)).map(link1 -> {
if (link1.isPresent()) {
Page linkedPage = (Page) link1.get().getReference();
return Optional.ofNullable(linkedPage).map(ComponentUtils::getFeaturedImage).orElse(null);
}
return null;
}).orElse(null);
} else {
// the inherited resource is the featured image of the current page
inheritedResource = Optional.ofNullable(currentPage).map(page -> {
Template template = page.getTemplate();
// make sure the resource is part of the currentPage or of its template
if (StringUtils.startsWith(resource.getPath(), currentPage.getPath()) || (template != null && StringUtils.startsWith(resource.getPath(), template.getPath()))) {
return ComponentUtils.getFeaturedImage(currentPage);
}
return null;
}).orElse(null);
}
Map<String, String> overriddenProperties = new HashMap<>();
Map<String, Resource> overriddenChildren = new HashMap<>();
String inheritedFileReference = null;
Resource inheritedFileResource = null;
String inheritedAlt = null;
String inheritedAltValueFromDAM = null;
if (inheritedResource != null) {
// Define the inherited properties
ValueMap inheritedProperties = inheritedResource.getValueMap();
inheritedFileReference = inheritedProperties.get(DownloadResource.PN_REFERENCE, String.class);
inheritedFileResource = inheritedResource.getChild(DownloadResource.NN_FILE);
inheritedAlt = inheritedProperties.get(ImageResource.PN_ALT, String.class);
inheritedAltValueFromDAM = inheritedProperties.get(PN_ALT_VALUE_FROM_DAM, String.class);
}
overriddenProperties.put(DownloadResource.PN_REFERENCE, inheritedFileReference);
overriddenChildren.put(DownloadResource.NN_FILE, inheritedFileResource);
// don't inherit the image title from the page image
overriddenProperties.put(PN_TITLE_VALUE_FROM_DAM, "false");
if (altValueFromPageImage) {
overriddenProperties.put(ImageResource.PN_ALT, inheritedAlt);
overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, inheritedAltValueFromDAM);
} else {
overriddenProperties.put(PN_ALT_VALUE_FROM_DAM, "false");
}
return new CoreResourceWrapper(resource, resource.getResourceType(), null, overriddenProperties, overriddenChildren);
}
return resource;
}
Aggregations