I have a Transformer rewrite some URKs based heavily on the Helpx article, but mine looks at all elements and all attributes and rewrites urls like /content/uc/news/articles/2016/06/n200000.html to /content/uc/news/articles/n200000.html
It works as expected on the rewrite part, but isn't hitting all the elements, namely, elements that are in my template (in customheaderlibs.html -- at a page component with resourceSuperType=wcm/foundation/components/page). I tried outputting all the attributes to the log in startElement(), and I don't even seen an entry for the non-transformed attributes.
Here's my code:
package org.uc.news.core.rewriters; import java.util.Map; import java.util.Set; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Property; import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.rewriter.DefaultTransformer; import org.apache.sling.rewriter.Transformer; import org.apache.sling.rewriter.TransformerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component(metatype = true, label = "UC News - Story Link Rewriter", description = "modifies news paths to remove the year and month after '/articles/'") @Service(value = TransformerFactory.class) public class StoryLinkRewriterFactory implements TransformerFactory { private static final Logger log = LoggerFactory.getLogger(StoryLinkRewriterFactory.class); @Property(value = "article-url", propertyPrivate = true) private static final String PIPELINE_TYPE = "pipeline.type"; @Property(value = "global", propertyPrivate = true) private static final String PIPELINE_MODE = "pipeline.mode"; @Property(boolValue = false, propertyPrivate = false) private static final String PIPELINE_ENABLED = "pipeline.enabled"; Map<String, Object> properties; @Activate protected void activate(Map<String, Object> properties) { this.properties = properties; } @Override public Transformer createTransformer() { if(PropertiesUtil.toBoolean(properties.get(PIPELINE_ENABLED), false)) { return new StoryLinkRewriter(); } return new DefaultTransformer(); } }
Transformer:
package org.uc.news.core.rewriters; import org.apache.felix.scr.annotations.Reference; import org.apache.sling.rewriter.DefaultTransformer; import org.apache.sling.rewriter.Transformer; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; public class StoryLinkRewriter extends DefaultTransformer implements Transformer { private static final Logger log = LoggerFactory.getLogger(StoryLinkRewriter.class); public StoryLinkRewriter() { log.info("started"); } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { AttributesImpl newAttrs = new AttributesImpl(atts); int length = newAttrs.getLength(); for(int i = 0 ; i < length; i++) { if(!newAttrs.getValue(i).trim().equals("")) { log.info(newAttrs.getValue(i).trim()); String stringValue = newAttrs.getValue(i).trim(); String pattern = "(.*)articles/([0-9]{4}/[0-9]{2}/)(.*)"; stringValue = stringValue.replaceAll(pattern, "$1articles/$3"); newAttrs.setValue(i, stringValue); } } super.startElement(uri, localName, qName, newAttrs); } }
And here's some example snippets fro the same request:
<!--should be rewritten but it's not, this path doesn't appear in the log either --> <meta property="og:url" content="http://localhost:4503/content/uc/news/articles/2016/06/n200000.html"/> ... <!-- default image component--path successfully rewritten --> <div class="image parbase cq-analyzable aem-GridColumn aem-GridColumn--default--4"> <img src="/content/uc/news/articles/n200000/_jcr_content/root/responsivegrid/image.img.jpg/1464871896413.jpg" title="Image Title" class="cq-dd-image" data-emptytext="Image"/> <small>Image Description</small> </div>
I'm not sure what I'm doing wrong--perhaps the transformer factory isn't correctly configured?
Solved! Go to Solution.
Views
Replies
Total Likes
Kind of try to avoid Transformers as a rule of Thumb, it's fast, but hard to configure and sometime it can result in blank screen in case of any issue..
Why not use the core Sling/Servlet feature Filters[1], simply set with the high priority for the Servlet, so that request get's intercepted last.
Then once the page is fully generated use lovely Jsoup[2] library to parse the output and change it accordingly to your needs.
[1] https://sling.apache.org/documentation/the-sling-engine/filters.html
Regards,
Peter
Views
Replies
Total Likes
Kind of try to avoid Transformers as a rule of Thumb, it's fast, but hard to configure and sometime it can result in blank screen in case of any issue..
Why not use the core Sling/Servlet feature Filters[1], simply set with the high priority for the Servlet, so that request get's intercepted last.
Then once the page is fully generated use lovely Jsoup[2] library to parse the output and change it accordingly to your needs.
[1] https://sling.apache.org/documentation/the-sling-engine/filters.html
Regards,
Peter
Views
Replies
Total Likes