use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class PageImageThumbnail method initModel.
@PostConstruct
protected void initModel() {
configPath = request.getRequestPathInfo().getResourcePath();
componentPath = request.getRequestPathInfo().getSuffix();
if (StringUtils.isBlank(componentPath)) {
RequestParameter itemParam = request.getRequestParameter("item");
if (itemParam == null) {
log.error("Suffix and 'item' param are blank");
return;
}
componentPath = itemParam.getString();
}
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
if (pageManager == null) {
log.error("pagemanager is null");
return;
}
Resource component = resourceResolver.getResource(componentPath);
if (component == null) {
log.error("the component at {} does not exist", componentPath);
return;
}
Page currentPage = pageManager.getContainingPage(component);
if (currentPage != null) {
currentPagePath = currentPage.getPath();
}
Page targetPage = null;
RequestParameter pageLinkParam = request.getRequestParameter("pageLink");
if (pageLinkParam != null) {
// retrieve the page link from the request parameter
String pageLink = pageLinkParam.getString();
targetPage = pageManager.getPage(pageLink);
} else {
// retrieve the page link from the component model
Teaser teaserModel = modelFactory.getModelFromWrappedRequest(request, component, Teaser.class);
Link link = null;
if (teaserModel != null) {
link = teaserModel.getLink();
} else {
Image imageModel = modelFactory.getModelFromWrappedRequest(request, component, Image.class);
if (imageModel != null) {
link = imageModel.getImageLink();
}
}
if (link != null) {
targetPage = (Page) link.getReference();
} else {
targetPage = currentPage;
}
}
if (targetPage == null) {
log.info("A target page cannot be found for the link defined in the request parameter or on the server at {}.", component.getPath());
return;
}
Resource featuredImage = ComponentUtils.getFeaturedImage(targetPage);
if (featuredImage == null) {
log.info("No featured image defined for the page at {}", targetPage.getPath());
return;
}
Image imageModel = modelFactory.getModelFromWrappedRequest(request, featuredImage, Image.class);
if (imageModel == null) {
log.info("the image model of {} is null", featuredImage.getPath());
return;
}
this.alt = imageModel.getAlt();
this.src = imageModel.getSrc();
}
use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class InheritedField method initModel.
@PostConstruct
private void initModel() {
path = slingRequest.getRequestPathInfo().getSuffix();
if (StringUtils.isBlank(path)) {
RequestParameter itemParam = slingRequest.getRequestParameter("item");
if (itemParam == null) {
log.error("Suffix and 'item' param are blank");
return;
}
path = itemParam.getString();
}
if (StringUtils.isBlank(prop)) {
log.error("'prop' value is null");
return;
}
ResourceResolver rr = slingRequest.getResourceResolver();
PageManager pm = rr.adaptTo(PageManager.class);
if (pm == null) {
log.error("pagemanager is null");
return;
}
containingPage = pm.getPage(path);
if (containingPage == null) {
log.error("page is null");
return;
}
inheritedValue = Utils.getInheritedValue(containingPage.getParent(), prop);
Resource contentResource = containingPage.getContentResource();
if (contentResource == null) {
return;
}
ValueMap props = contentResource.adaptTo(ValueMap.class);
if (props == null) {
return;
}
override = props.get(getProp() + OVERRIDE_SUFFIX, OVERRIDE_DEFAULT);
specifiedValue = props.get(getProp(), String.class);
}
use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ClientLibrariesImplTest method testGetCategories.
@Test
void testGetCategories() {
PageManager pageManager = context.pageManager();
Page page = pageManager.getPage(ROOT_PAGE);
Map<String, Object> attributes = new HashMap<>();
attributes.put(ClientLibraries.OPTION_RESOURCE_TYPES, Utils.getPageResourceTypes(page, context.request(), mock(ModelFactory.class)));
ClientLibrariesImpl clientlibs = Objects.requireNonNull((ClientLibrariesImpl) getClientLibrariesUnderTest(ROOT_PAGE, attributes));
Set<String> categories = new HashSet<>();
categories.add(TEASER_CATEGORY);
categories.add(ACCORDION_CATEGORY);
categories.add(CAROUSEL_CATEGORY);
assertEquals(categories, clientlibs.getCategoriesFromComponents());
}
use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AdaptiveImageServlet method doGet.
@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
Timer.Context requestDuration = metrics.startDurationRecording();
try {
metrics.markServletInvocation();
RequestPathInfo requestPathInfo = request.getRequestPathInfo();
List<String> selectorList = selectorToList(requestPathInfo.getSelectorString());
String suffix = requestPathInfo.getSuffix();
String imageName = StringUtils.isNotEmpty(suffix) ? FilenameUtils.getName(suffix) : "";
if (StringUtils.isNotEmpty(suffix)) {
String suffixExtension = FilenameUtils.getExtension(suffix);
if (StringUtils.isNotEmpty(suffixExtension)) {
if (!suffixExtension.equals(requestPathInfo.getExtension())) {
LOGGER.error("The suffix part defines a different extension than the request: {}.", suffix);
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
} else {
LOGGER.error("Invalid suffix: {}.", suffix);
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
Resource component = request.getResource();
ResourceResolver resourceResolver = request.getResourceResolver();
if (!component.isResourceType(IMAGE_RESOURCE_TYPE)) {
// image coming from template; need to switch resource
Resource componentCandidate = null;
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
if (pageManager != null) {
Page page = pageManager.getContainingPage(component);
if (page != null) {
Template template = page.getTemplate();
if (template != null) {
if (StringUtils.isNotEmpty(suffix)) {
long lastModifiedSuffix = getRequestLastModifiedSuffix(suffix);
String relativeTemplatePath = lastModifiedSuffix == 0 ? // no timestamp info, but extension is valid; get resource name
suffix.substring(0, suffix.lastIndexOf('.')) : // timestamp info, get parent path from suffix
suffix.substring(0, suffix.lastIndexOf("/" + String.valueOf(lastModifiedSuffix)));
String imagePath = ResourceUtil.normalize(template.getPath() + relativeTemplatePath);
if (StringUtils.isNotEmpty(imagePath) && !template.getPath().equals(imagePath)) {
componentCandidate = resourceResolver.getResource(imagePath);
}
}
}
}
}
if (componentCandidate == null) {
LOGGER.error("Unable to retrieve an image from this page's template.");
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
component = componentCandidate;
}
LinkHandler linkHandler = request.adaptTo(LinkHandler.class);
Style currentStyle = WCMUtils.getStyle(request);
Page currentPage = Optional.ofNullable(resourceResolver.adaptTo(PageManager.class)).map(pageManager -> pageManager.getContainingPage(request.getResource())).orElse(null);
Resource wrappedImageResourceWithInheritance = getWrappedImageResourceWithInheritance(component, linkHandler, currentStyle, currentPage);
ImageComponent imageComponent = new ImageComponent(wrappedImageResourceWithInheritance);
if (imageComponent.source == Source.NOCONTENT || imageComponent.source == Source.NONEXISTING) {
LOGGER.error("Either the image from {} does not have a valid file reference" + " or the containing page does not have a valid featured image", component.getPath());
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
ValueMap componentProperties = component.getValueMap();
long lastModifiedEpoch = 0;
Calendar lastModifiedDate = componentProperties.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
if (lastModifiedDate == null) {
lastModifiedDate = componentProperties.get(NameConstants.PN_PAGE_LAST_MOD, Calendar.class);
}
if (lastModifiedDate != null) {
lastModifiedEpoch = lastModifiedDate.getTimeInMillis();
}
Asset asset = null;
if (imageComponent.source == Source.ASSET) {
asset = imageComponent.imageResource.adaptTo(Asset.class);
if (asset == null) {
LOGGER.error("Unable to adapt resource {} used by image {} to an asset.", imageComponent.imageResource.getPath(), component.getPath());
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
long assetLastModifiedEpoch = asset.getLastModified();
if (assetLastModifiedEpoch > lastModifiedEpoch) {
lastModifiedEpoch = assetLastModifiedEpoch;
}
}
long requestLastModifiedSuffix = getRequestLastModifiedSuffix(suffix);
if (requestLastModifiedSuffix >= 0 && requestLastModifiedSuffix != lastModifiedEpoch) {
String redirectLocation = getRedirectLocation(request, lastModifiedEpoch);
if (StringUtils.isNotEmpty(redirectLocation)) {
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", redirectLocation);
return;
} else {
LOGGER.error("Unable to determine correct redirect location.");
metrics.markImageErrors();
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
if (!handleIfModifiedSinceHeader(request, response, lastModifiedEpoch)) {
Map<String, Integer> transformationMap = getTransformationMap(selectorList, component);
Integer jpegQualityInPercentage = transformationMap.get(SELECTOR_QUALITY_KEY);
double quality = jpegQualityInPercentage / 100.0d;
int resizeWidth = transformationMap.get(SELECTOR_WIDTH_KEY);
String imageType = getImageType(requestPathInfo.getExtension());
if (imageComponent.source == Source.FILE) {
transformAndStreamFile(response, componentProperties, resizeWidth, quality, imageComponent.imageResource, imageType, imageName);
} else if (imageComponent.source == Source.ASSET) {
transformAndStreamAsset(response, componentProperties, resizeWidth, quality, asset, imageType, imageName);
}
metrics.markImageStreamed();
}
} catch (IllegalArgumentException e) {
LOGGER.error("Invalid image request", e.getMessage());
metrics.markImageErrors();
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} finally {
requestDuration.stop();
}
}
use of com.day.cq.wcm.api.PageManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class CaConfigReferenceProvider method findReferences.
@Override
public List<Reference> findReferences(Resource resource) {
if (!enabled) {
return Collections.emptyList();
}
List<Reference> references = new ArrayList<>();
// If the resource is not part of a page: stop the processing
PageManager pageManager = resource.getResourceResolver().adaptTo(PageManager.class);
if (pageManager == null) {
return references;
}
Page page = pageManager.getContainingPage(resource);
if (page == null) {
return references;
}
for (String config : configurationManager.getConfigurationNames()) {
addCaConfigReference(config, resource, references);
}
return references;
}
Aggregations