Skip to content

Instantly share code, notes, and snippets.

@sreeni-b
Created July 2, 2020 10:28
Show Gist options
  • Save sreeni-b/5311ee54e1df3c5ae86e230d5ff906d4 to your computer and use it in GitHub Desktop.
Save sreeni-b/5311ee54e1df3c5ae86e230d5ff906d4 to your computer and use it in GitHub Desktop.
Master Link Transformer
package com.aemks.core.transformers;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import java.io.IOException;
public class ReferenceLinkTransformer extends AbstractSAXPipe implements Transformer {
private static final String ANCHOR_TAG = "a";
private static final String HREF_ATTRIBUTE = "href";
private Page currentPage;
private String blueprintPath;
private String liveCopyPath;
private boolean isLiveCopy;
private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceLinkTransformer.class);
@Override
public void init(ProcessingContext processingContext,
ProcessingComponentConfiguration processingComponentConfiguration) throws IOException {
Resource resource = processingContext.getRequest().getResource();
setBlueprintAndLiveCopyPaths(resource);
}
/**
* @param resource
* This method checks whether the current page is a live copy.
* If the page or root is live copy, the blueprint path and live copy path are retrieved
* and set.
*/
private void setBlueprintAndLiveCopyPaths(Resource resource) {
LiveRelationshipManager liveRelationshipManager =resource.getResourceResolver().adaptTo(LiveRelationshipManager.class);
LiveRelationship liveRelationship = null;
PageManager pageManager = resource.getResourceResolver().adaptTo(PageManager.class);
if(pageManager==null){
return;
}
currentPage = pageManager.getContainingPage(resource);
if(currentPage==null){
return;
}
Resource pageResource = currentPage.adaptTo(Resource.class);
try {
isLiveCopy = liveRelationshipManager.getLiveRelationship(pageResource,false)!=null ? true : false;
if(isLiveCopy){
liveRelationship = liveRelationshipManager.getLiveRelationship(pageResource,false);
if(liveRelationship.getLiveCopy()==null){
return;
}
blueprintPath = liveRelationship.getLiveCopy().getBlueprintPath();
liveCopyPath = liveRelationship.getLiveCopy().getPath();
return;
}
} catch (WCMException e) {
LOGGER.error("An error ocurred while getting live copy",e);
}
}
/**
* @param uri
* @param localName
* @param qName
* @param atts
* @throws SAXException
* This method checks for anchor tags and replaces master link with live copy link.
*/
@Override public void startElement(String uri, String localName, String qName, Attributes atts)
throws SAXException {
final AttributesImpl attributes = new AttributesImpl(atts);
if(ANCHOR_TAG.equalsIgnoreCase(localName) && currentPage!=null && isLiveCopy){
String href = attributes.getValue(HREF_ATTRIBUTE);
if(href!=null && href.startsWith(blueprintPath)){
replaceMasterLink(attributes,href);
}
}
contentHandler.startElement(uri, localName, qName, attributes);
}
private void replaceMasterLink(AttributesImpl attributes, String href) {
for (int attributeIndex = 0; attributeIndex < attributes.getLength(); attributeIndex++) {
if (HREF_ATTRIBUTE.equalsIgnoreCase(attributes.getQName(attributeIndex))) {
href= href.replace(blueprintPath, liveCopyPath);
attributes.setValue(attributeIndex,href);
break;
}
}
}
@Override public void dispose() {
// No operation required.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment