This conversation has been locked due to inactivity. Please create a new post.
This conversation has been locked due to inactivity. Please create a new post.
Hi all,
I am writing a custom sling rewriter which implements Transformer Class. Usually, we will use it to parse the html and rewrite the needed tag elements. In my case, I have to get some dynamic values by accessing JCR and need to rewrite them as URLs. I am not able to Refer OSGi service or ResourceResolverFactory inside transformer class. It gives me either 500 error or null pointer exception.
When I checked on samples in the web, they did a static rewrite. Could anyone help me to confirm whether shall I use OSGi services inside Custom Sling rewriter class.
Regards,
Hema
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Hemalatha,
Create a custom implementation for TransformerFactory and reference the desired OSGI service there.
In the createTransformer() method of TransformerFactory, invoke your custom Transformer class (Passing the referenced OSGI service via Parameterized constructor)
Below sample skeleton for reference. (Haven't tried for actual implementation, below is just the sample structure)
CustomTransformerFactory.class
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service = TransformerFactory.class, property = { "pipeline.mode=global",
"pipeline.paths=/content/learnings", "pipeline-type=linkrewriter" }, immediate = true)
public class CustomTransformerFactory implements TransformerFactory {
@Reference //if this is not working try bind/unbind reference
private ResourceResolverFactory rescFactory;
@Override
public Transformer createTransformer() {
return new CustomTransformer(rescFactory);
}
}
CustomTransformer.class
public class CustomTransformer implements Transformer {
private ResourceResolverFactory rescFactory; // This instance variable is initialized in the below constructor(as invoked from Factory) can then be used in the overridden methods of Transformer.
public CustomTransformer(ResourceResolverFactory rescFactory) {
this.rescFactory = rescFactory;
}
}
Hi @Hemalatha,
Create a custom implementation for TransformerFactory and reference the desired OSGI service there.
In the createTransformer() method of TransformerFactory, invoke your custom Transformer class (Passing the referenced OSGI service via Parameterized constructor)
Below sample skeleton for reference. (Haven't tried for actual implementation, below is just the sample structure)
CustomTransformerFactory.class
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@Component(service = TransformerFactory.class, property = { "pipeline.mode=global",
"pipeline.paths=/content/learnings", "pipeline-type=linkrewriter" }, immediate = true)
public class CustomTransformerFactory implements TransformerFactory {
@Reference //if this is not working try bind/unbind reference
private ResourceResolverFactory rescFactory;
@Override
public Transformer createTransformer() {
return new CustomTransformer(rescFactory);
}
}
CustomTransformer.class
public class CustomTransformer implements Transformer {
private ResourceResolverFactory rescFactory; // This instance variable is initialized in the below constructor(as invoked from Factory) can then be used in the overridden methods of Transformer.
public CustomTransformer(ResourceResolverFactory rescFactory) {
this.rescFactory = rescFactory;
}
}
This is my class, which is similar as you mentioned, seems the issue is that the existing code base is still using SCR annotations. Even If I declare OSGi R7 dependencies and tried to use it in my class. It's not getting listed in the OSGi components list if I use OSGI annotation instead of SCR annotations. (If I rewrite using SCR annotation, it's showing as OSGi component but I am not able to refer any OSGi service inside that class)
@component(property = { "pipeline.type=dmrewriter" }, service = { TransformerFactory.class })
public class DMUrlTransformerFactory implements TransformerFactory {
public Transformer createTransformer() {
return new DMRewriterTransformer();
}
}
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Likes
Replies