Skip to content

Instantly share code, notes, and snippets.

@sreeni-b
Last active August 16, 2020 10:19
Show Gist options
  • Save sreeni-b/38507a7a25fe8a0997527b9656f52129 to your computer and use it in GitHub Desktop.
Save sreeni-b/38507a7a25fe8a0997527b9656f52129 to your computer and use it in GitHub Desktop.
package com.aemks.core.models;
import com.day.cq.wcm.api.Page;
import org.apache.commons.io.FilenameUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import javax.annotation.PostConstruct;
@Model(
adaptables = SlingHttpServletRequest.class,
adapters = { HeaderModel.class },
resourceType = HeaderModel.RESOURCE_TYPE,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HeaderModel {
public static final String RESOURCE_TYPE="aemks/components/header";
@Self
private SlingHttpServletRequest request;
@ScriptVariable
private Page currentPage;
private Page pageFromSuffix;
@SlingObject
private ResourceResolver resourceResolver;
@PostConstruct
protected void init() {
if (request.getRequestPathInfo().getResourcePath().startsWith("/conf") || request.getRequestPathInfo()
.getResourcePath().startsWith("/content/experience-fragments")) {
String suffix = request.getRequestPathInfo().getSuffix();
if(suffix!=null){
suffix = FilenameUtils.removeExtension(suffix);
Resource currentPageResource = resourceResolver.getResource(suffix);
if(currentPageResource!=null){
// Use the page from suffix passed from dispatcher
pageFromSuffix = currentPageResource.adaptTo(Page.class);
}
}else{
// Use OOTB currentPage in author or publish
pageFromSuffix = currentPage;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment