Created
October 7, 2021 22:38
-
-
Save techforum-repo/c858502041f23787c934321f70e171ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.sling.models.annotations.Model; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.models.annotations.DefaultInjectionStrategy; | |
import org.apache.sling.models.annotations.Exporter; | |
import org.apache.sling.models.annotations.injectorspecific.Self; | |
import org.apache.sling.models.annotations.Via; | |
import org.apache.sling.models.annotations.via.ResourceSuperType; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import lombok.experimental.Delegate; | |
import com.adobe.cq.export.json.ExporterConstants; | |
import com.adobe.cq.wcm.core.components.models.ExperienceFragment; | |
import com.adobe.cq.xf.ExperienceFragmentsConstants; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.models.annotations.injectorspecific.SlingObject; | |
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; | |
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy; | |
import com.day.cq.wcm.api.Page; | |
@Model(adaptables = { Resource.class, | |
SlingHttpServletRequest.class }, adapters = ExperienceFragment.class, resourceType = CustomExperienceFragmentImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) | |
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION) | |
public class CustomExperienceFragmentImpl implements ExperienceFragment { | |
public static final String RESOURCE_TYPE = "mysite/components/experiencefragment"; | |
@Self | |
@Via(type = ResourceSuperType.class) | |
@Delegate(excludes = DelegationExclusion.class) | |
private ExperienceFragment delegate; | |
@Self | |
private SlingHttpServletRequest request; | |
@SlingObject | |
private ResourceResolver resourceResolver; | |
@ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL) | |
private Page currentPage; | |
private static final Logger LOGGER = LoggerFactory.getLogger(CustomExperienceFragmentImpl.class); | |
private static final String CONTENT_ROOT = "/content"; | |
private static final String XF_HEADER_VARIATION_PATH = "/site/header/master"; | |
private static final String XF_FOOTER_VARIATION_PATH = "/site/footer/master"; | |
private static final String CONTENT_ROOT_EWP = "/content/test"; | |
private static final String XF_HEADER_VARIATION = "/header/"; | |
private static final String XF_FOOTER_VARIATION = "/footer/"; | |
/* | |
* Return the site specific headers and footers, delegate to Core Component | |
* ExperienceFragment Model for remaining scenarios | |
*/ | |
@Override | |
public String getLocalizedFragmentVariationPath() { | |
try { | |
if (currentPage != null && currentPage.getAbsoluteParent(2) != null) { | |
final String currentPageRootPath = currentPage.getAbsoluteParent(2).getPath(); | |
String fragmentVariationPath = request.getResource().getValueMap() | |
.get(ExperienceFragment.PN_FRAGMENT_VARIATION_PATH, String.class); | |
if (StringUtils.isNoneEmpty(fragmentVariationPath) && StringUtils.isNoneEmpty(currentPageRootPath) | |
&& !currentPageRootPath.startsWith(CONTENT_ROOT_EWP)) { | |
LOGGER.debug("CurrentPageRootPath: {} FragmentVariationPath: {}", currentPageRootPath, | |
fragmentVariationPath); | |
if (fragmentVariationPath.contains(XF_HEADER_VARIATION)) { | |
String xfVariationPath = StringUtils.join(StringUtils.replace(currentPageRootPath, CONTENT_ROOT, | |
ExperienceFragmentsConstants.CONTENT_PATH, 1), XF_HEADER_VARIATION_PATH); | |
if (resourceExists(xfVariationPath)) { | |
LOGGER.debug("Handling the header variation. XfVariationPath - {} ", xfVariationPath); | |
return xfVariationPath; | |
} | |
} else if (fragmentVariationPath.contains(XF_FOOTER_VARIATION)) { | |
String xfVariationPath = StringUtils.join(StringUtils.replace(currentPageRootPath, CONTENT_ROOT, | |
ExperienceFragmentsConstants.CONTENT_PATH, 1), XF_FOOTER_VARIATION_PATH); | |
if (resourceExists(xfVariationPath)) { | |
LOGGER.debug("Handling the footer variation. XfVariationPath - {} ", xfVariationPath); | |
return xfVariationPath; | |
} | |
} | |
} | |
} | |
} catch (Exception e) { | |
LOGGER.error(e.getMessage()); | |
} | |
return delegate.getLocalizedFragmentVariationPath(); | |
} | |
private boolean resourceExists(final String path) { | |
return (StringUtils.isNotEmpty(path) && this.request.getResourceResolver().getResource(path) != null); | |
} | |
private interface DelegationExclusion { | |
String getLocalizedFragmentVariationPath(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment