Starterstore: Interrupting GenericCommandChainComponent chain nicely after redirect

We are using starterstore 2.4.0 in our project for search, PDP and other features.

We are also using Search & Merchandising as our primary search results provider.

As part of the Search & Merchandising functionality, we have implemented “search redirection” so that when users type certain keywords on the search box and hit enter, S&M instead of returning results, returns a redirection URL to which we should redirect users to.

This is the list of commands for the search page:

psmtInitContextCommand, personalizationCallCommand, psmtSearchCommand, psmtProductEnrichmentCommand, pageableCommand, psmtTrackingCommand

Now, the question is: is there an “elegant” way to interrupt that command chain for the cases where we detect that S&M is returning a redirect URL as a consequence of the user’s search input?

psmtSearchCommand is the one that goes to S&M and returns the redirect Urls for these special keywords mentioned before.

Although we detect the S&M response contains that redirection url and perform the redirection accordingly using

{code:java}
exchangeState.getContext().getResponse().sendRedirect(redirectedUrl);
{code}

we noticed that the rest of the commands in the chain get executed anyway. This is clearly a waste of resources since the intention is to send the user to a different page, so nothing that gets executed after the redirection is necessary from our perspective.

Throwing an exception from the code interrupts the chain, but that’s far from elegant.

Is there any other way to accomplish the interruption and prevent the rest of the commands from being executed?

Kind Regards
Juan Manuel

Hi,
if the command inherits - directly or indirectly - from AbstractStarterStoreCommand, then it’s not possible to interrupt the chain. The main reason is that the execute methods always return false, hence it never interrupts the chain [1].

As an option, you may think about changing the standard behaviour of the AbstractStarterStoreCommand, maybe proposing your own implementation of the execute method returning the proper boolean value.

Please consider that in most of the cases diverging from the ‘standard’ implementation is not the best practice, since it could produce un-expected side effects in future iterations; on the other hand, I don’t see any big change that could break such customizations.

HTH

[1] https://commons.apache.org/proper/commons-chain/apidocs/org/apache/commons/chain/Command.html#execute(org.apache.commons.chain.Context)

1 Like