use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ContentFragmentUtilsTest method getGridTypeSetInFragmentPolicy.
@Test
public void getGridTypeSetInFragmentPolicy() {
// GIVEN
ContentPolicyManager contentPolicyManager = Mockito.mock(ContentPolicyManager.class);
ContentPolicy contentPolicy = Mockito.mock(ContentPolicy.class);
ResourceResolver resourceResolver = Mockito.mock(ResourceResolver.class);
Resource resource = Mockito.mock(Resource.class);
ValueMap valueMap = new MockValueMap(resource);
valueMap.put(PN_CFM_GRID_TYPE, "foobar");
Mockito.when(resource.getResourceResolver()).thenReturn(resourceResolver);
Mockito.when(resourceResolver.adaptTo(Mockito.eq(ContentPolicyManager.class))).thenReturn(contentPolicyManager);
Mockito.when(contentPolicyManager.getPolicy(Mockito.eq(resource))).thenReturn(contentPolicy);
Mockito.when(contentPolicy.getProperties()).thenReturn(valueMap);
// WHEN
String defaultGridResourceType = ContentFragmentUtils.getGridResourceType(resource);
// THEN
Assertions.assertEquals(defaultGridResourceType, "foobar");
}
use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServlet method getAllowedRenditionWidths.
/**
* Returns the list of allowed renditions sizes from this component's content policy. If the component doesn't have a content policy,
* then the list will be empty. Rendition widths that are not valid {@link Integer} numbers will be ignored.
*
* @param request the request identifying the component
* @return the list of the allowed widths; the list will be <i>empty</i> if the component doesn't have a content policy
*/
private List<Integer> getAllowedRenditionWidths(@Nonnull SlingHttpServletRequest request) {
List<Integer> list = new ArrayList<>();
ResourceResolver resourceResolver = request.getResourceResolver();
ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
if (policyManager != null) {
ContentPolicy contentPolicy = policyManager.getPolicy(request.getResource());
if (contentPolicy != null) {
String[] allowedRenditionWidths = contentPolicy.getProperties().get(com.adobe.cq.wcm.core.components.models.Image.PN_DESIGN_ALLOWED_RENDITION_WIDTHS, new String[0]);
for (String width : allowedRenditionWidths) {
try {
list.add(Integer.parseInt(width));
} catch (NumberFormatException e) {
LOGGER.warn("One of the configured widths ({}) from the {} content policy is not a valid Integer.", width, contentPolicy.getPath());
return list;
}
}
}
}
return list;
}
use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AllowedTitleSizesDataSourceServlet method getAllowedTypes.
private List<Resource> getAllowedTypes(@Nonnull SlingHttpServletRequest request) {
List<Resource> allowedTypes = new ArrayList<>();
ResourceResolver resolver = request.getResourceResolver();
Resource contentResource = resolver.getResource((String) request.getAttribute(Value.CONTENTPATH_ATTRIBUTE));
ContentPolicyManager policyMgr = resolver.adaptTo(ContentPolicyManager.class);
if (policyMgr != null) {
ContentPolicy policy = policyMgr.getPolicy(contentResource);
if (policy != null) {
ValueMap props = policy.getProperties();
if (props != null) {
String[] titleTypes = props.get("allowedTypes", String[].class);
if (titleTypes != null && titleTypes.length > 0) {
for (String titleType : titleTypes) {
allowedTypes.add(new TitleTypeResource(titleType, resolver));
}
}
}
}
}
return allowedTypes;
}
use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ImageImpl method initModel.
@PostConstruct
private void initModel() {
boolean hasContent = false;
if (StringUtils.isNotEmpty(fileReference)) {
int dotIndex;
if ((dotIndex = fileReference.lastIndexOf(DOT)) != -1) {
extension = fileReference.substring(dotIndex + 1);
}
hasContent = true;
} else {
Resource file = resource.getChild(DownloadResource.NN_FILE);
if (file != null) {
extension = mimeTypeService.getExtension(PropertiesUtil.toString(file.getResourceMetadata().get(ResourceMetadata.CONTENT_TYPE), MIME_TYPE_IMAGE_JPEG));
hasContent = true;
}
}
if (hasContent) {
long lastModifiedDate = 0;
if (!wcmmode.isDisabled()) {
ValueMap properties = resource.getValueMap();
Calendar lastModified = properties.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
if (lastModified == null) {
lastModified = properties.get(NameConstants.PN_PAGE_LAST_MOD, Calendar.class);
}
if (lastModified != null) {
lastModifiedDate = lastModified.getTimeInMillis();
}
}
if (extension.equalsIgnoreCase("tif") || extension.equalsIgnoreCase("tiff")) {
extension = DEFAULT_EXTENSION;
}
ResourceResolver resourceResolver = request.getResourceResolver();
ContentPolicyManager policyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
ContentPolicy contentPolicy = policyManager.getPolicy(resource);
if (contentPolicy != null) {
disableLazyLoading = contentPolicy.getProperties().get(PN_DESIGN_LAZY_LOADING_ENABLED, false);
}
Set<Integer> supportedRenditionWidths = getSupportedRenditionWidths(contentPolicy);
smartImages = new String[supportedRenditionWidths.size()];
smartSizes = new int[supportedRenditionWidths.size()];
int index = 0;
String escapedResourcePath = Text.escapePath(resource.getPath());
for (Integer width : supportedRenditionWidths) {
smartImages[index] = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + width + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
smartSizes[index] = width;
index++;
}
if (smartSizes.length == 0 || smartSizes.length >= 2) {
src = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
} else if (smartSizes.length == 1) {
src = request.getContextPath() + escapedResourcePath + DOT + AdaptiveImageServlet.DEFAULT_SELECTOR + DOT + smartSizes[0] + DOT + extension + (!wcmmode.isDisabled() && lastModifiedDate > 0 ? "/" + lastModifiedDate + DOT + extension : "");
}
if (!isDecorative) {
Page page = pageManager.getPage(linkURL);
if (page != null) {
String vanityURL = page.getVanityUrl();
linkURL = (vanityURL == null ? linkURL + ".html" : vanityURL);
}
} else {
linkURL = null;
alt = null;
}
buildJson();
}
}
use of com.day.cq.wcm.api.policies.ContentPolicyManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ContentFragmentUtils method getGridResourceType.
/**
* Returns an optional grid type configured via {@link ContentPolicy content policy} property
* ({@value #PN_CFM_GRID_TYPE}) or {@value #DEFAULT_GRID_TYPE} as default.
*
* @param fragmentResource the content fragment resource resource to be checked
* @return the configured grid type of a default
*/
public static String getGridResourceType(Resource fragmentResource) {
String gridResourceType = DEFAULT_GRID_TYPE;
if (fragmentResource == null) {
return gridResourceType;
}
ResourceResolver resourceResolver = fragmentResource.getResourceResolver();
ContentPolicyManager contentPolicyManager = resourceResolver.adaptTo(ContentPolicyManager.class);
ContentPolicy fragmentContentPolicy = contentPolicyManager != null ? contentPolicyManager.getPolicy(fragmentResource) : null;
if (fragmentContentPolicy != null) {
ValueMap contentPolicyProperties = fragmentContentPolicy.getProperties();
gridResourceType = contentPolicyProperties.get(PN_CFM_GRID_TYPE, DEFAULT_GRID_TYPE);
}
return gridResourceType;
}
Aggregations