You can use the Assembler eventing framework as an extension point for navigation cartridges in cases where extending an existing cartridge handler is insufficient.
If you are making modifications to the navigation cartridges, you can trigger processing logic based on Assembler events instead of subclassing the core cartridge handlers.
Using an event listener instead of extending a cartridge handler introduces the following considerations:
Related links
The Assembler provides an empty implementation of the
AssemblerEventListener
,
AssemblerEventAdapter
. You can extend this implementation
to create a listener that triggers on an Assembler event.
Create a new Java class that extends the
AssemblerEventAdapter
.For example:
public class ResultsListListener extends AssemblerEventAdapter { }
Override the methods that correspond to the events for which you wish to trigger custom processing logic:
public class ResultsListListener extends AssemblerEventAdapter { @Override public void cartridgePreprocessStarting(AssemblerEvent event){ ... } @Override public void cartridgeProcessComplete(AssemblerEvent event){ ... } }
For a list of Assembler events, see the Assembler event framework reference or refer to the Assembler API Reference (Javadoc).
Add conditional logic to restrict processing to a specific cartridge handler:
public class ResultsListListener extends AssemblerEventAdapter { ... @Override public void cartridgeProcessComplete(AssemblerEvent event){ if(event.getContentItem() != null && "ResultsList".equals(event.getContentItem().getType()){ ... } } }
The example below prefixes the
max_price
property on a record with a dollar sign:public class ResultsListListener extends AssemblerEventAdapter { ... @Override public void cartridgeProcessComplete(AssemblerEvent event){ if(event.getContentItem() != null && "ResultsList".equals(event.getContentItem().getType()){ ResultsList resultsList = (ResultsList) event.getContentItem(); for(Record record : resultsList.getRecords()){ Attribute price = record.getAttributes().get("product.max_price"); if(price != null){ for(int i = 0 ; i < price.size(); i++){ price.set(i, "$" + price.get(i).toString()); } } } } } }
After creating a new listener, you must register it by including it in
the list of listeners for the
assemblerFactory
object.
You must register all event listeners with the
AssemblerFactory
object.
The
AssemblerFactory
takes event listeners as constructor
arguments. These listeners are instantiated with each
Assembler
object created by the factory class.
Optionally, you may also choose to use the
Assembler.addAssemblerEventListener()
method to add a
listener for a single assembly request.
Example 5. Example
The example below uses the
ResultsListListener
defined in the previous topic,
registered in the Discover Electronics reference application.
The reference application uses the Assembler context file to configure
global application properties. The configuration bean for the
AssemblerFactory
includes a list of listeners as
constructor arguments:
<bean id="assemblerFactory" class="com.endeca.infront.assembler.spring.SpringAssemblerFactory"
scope="singleton">
<constructor-arg>
...
</constructor-arg>
<constructor-arg>
<!-- List of listeners registered in the assembler -->
<list>
<bean class="com.endeca.infront.ResultsListListener" />
<bean class="com.endeca.infront.logger.SLF4JAssemblerEventLogger" />
<bean class="com.endeca.infront.assembler.event.request.ContentItemAugmentAdapter">
<constructor-arg ref="springUtility"/>
</bean>
...
</list>
</constructor-arg>
</bean>