AEM error - Usage of org.apache.sling.api.resource.ResourceResolver as a field is not thread safe.
hi folks,
I had to write a link rewriter but I get the above warning with it.
So the jist is, I stored the resource Resolver in the init() and use it in the startElement()
I don’t know any other way of doing a link rewriter except this :-)
Any suggestions welcome.
thanks
FIoa
@Component(
label = "External Link Transformer Factory",
description = "Rewrites external links by ...")
@Service(value = TransformerFactory.class)
public class ExternalLinkTransformerFactory implements TransformerFactory {
@Property(value = "extlink", propertyPrivate = true)
private static final String PROPERTY_PIPELINE_TYPE = "pipeline.type";
@Override
public Transformer createTransformer() {
return new ExternalLinkTransformer();
}
private ResourceResolver resourceResolver;
private class ExternalLinkTransformer implements Transformer {
private static final String TAG_ANCHOR = "a";
private static final String ATTR_HREF = "href";
private ContentHandler contentHandler;
@Override
public void init(ProcessingContext processingContext,
ProcessingComponentConfiguration processingComponentConfiguration)
throws IOException {
resourceResolver = processingContext.getRequest().getResourceResolver();
}
@Override
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
contentHandler.startElement(uri, localName, qName, rebuildAttributes(localName, attributes));
}
private Attributes rebuildAttributes(String elementName, Attributes currentAttrs) {
if(!TAG_ANCHOR.equalsIgnoreCase(elementName)) {
return currentAttrs;
}
String url = currentAttrs.getValue(ATTR_HREF);
try {
final AttributesImpl newAttrs = new AttributesImpl(currentAttrs);
if ( resourceResolver.isLive() == true ) {
String modifiedHref = resourceResolver.map(pathname);
modifiedHref = modifiedHref + other stuff...
newAttrs.setValue(currentAttrs.getIndex(ATTR_HREF), modifiedHref);
return newAttrs;
}
}