Registering a Post Processor

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Registering a Post Processor

richardlhr
Hi,

I have have written a post processor and I which to register it so Slice can use it.
How do I do this?

I looked at the MapperModule class. The getMapper(...) method calls mapperBuilder.addDefaultSliceProcessors().build() which adds all the processors and post processor and then built the Mapper.
Would it be fine to create my own MapperModule that would retrieve the current MapperBuilder, add additional processors/post processor and call build again?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Registering a Post Processor

maciej.majchrzak
Administrator
Yes, it will be fine to create your own mapper module. The MapperModule (or CQMapperModule from CQ/AEM addon) is a very simple module providing only one type of objects - Mapper. You need to remember to install the module you'll create instead of CQMapperModule. In order to do this, you'd need to go to your activator and replace the:
CQModulesFactory.createModules();
with a method like this:
private List<Module> createModules() {
	List<Module> modules = new ArrayList<Module>();
	modules.add(new CQModule());
	modules.add(new MyMapperModule());
	modules.add(new DamModule());
	modules.add(new CurrentPageModule());
	modules.add(new RequestedPageModule());
	modules.add(new TemplateModule());
	return modules;
}

I know that this is not particularly handy method of extending mapper, therefor we're going to change this approach in Slice 4.3 - mapper will allow you to use multibindings to register your own processors (https://cognifide.atlassian.net/browse/SLICE-122). The release is planned in Q4 2015.
Reply | Threaded
Open this post in threaded view
|

Re: Registering a Post Processor

richardlhr
Thanks. Yes this feels like a hack :) I'm sure 4.3 will make sure much easier.
Reply | Threaded
Open this post in threaded view
|

Re: Registering a Post Processor

richardlhr
My post processor is properly called which means it is properly registered.
However, it seems that I can't inject anything this in it.

I have:
@Inject
SlingHttpServletRequest request

And it is always null when processValue is called.

I know Injection works for processor, is it supported for postprocessor?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Registering a Post Processor

maciej.majchrzak
Administrator
Yes, it's supported. But if it works or not depends on how you create the postprocessor object. In your MapperModule's getMapper method you can't instantiate the postprocessor by "new MyPostProcessor()". Instead, you need to inject it to the method. Your method should look like this:
@Provides
@ContextScoped
public Mapper getMapper(MapperBuilder mapperBuilder, MyPostProcessor mypostprocessor) {
	return mapperBuilder.addDefaultSliceProcessors().addFieldProcessor(mypostprocessor).build();
}
Hope it helps :) Maciej