This section describes how to create triggers. Using the Exalead CloudView Eclipse plugin, select File > New > Other > CloudView Mashup Components > <Type of> Trigger
A Feed Trigger is an entry point to alter the behavior of a Feed. It is called by the Mashup API before and after the feed execution, allowing for query manipulation, context modification and results manipulation. Therefore, Feed Triggers can do the following:
• Decide to override the query to be issued to the actual ‘execute’ method based on query expansion (beforeQuery method)
• Decide to replay the feed execution because the result obtained is not satisfying, for example, if there are no results (afterQuery method that returns Result.EVAL_AGAIN)
Example:
You can use a Feed Trigger on any Feed in your configuration to customize query processing or feeds behavior for different purposes such as:
• Query rewriting
• Query computing from previously retrieved results
• Enabling / Disabling feeds
Design Triggers
Design triggers are called by the Mashup UI and include:
• Pre-request triggers which can be used to decide whether the user should be redirected to another page or not. The back end is not yet called, so no special load is triggered on the system. For example, redirect the user to the page called /imagesearch if the query starts withimage(s).
• Page and widget triggers which have the possibility to alter the behavior of the display by making decisions to draw things or even change the configuration (each user request gets a fresh copy of the original configuration, so any changes at query time are safe). For example: decide whether a widget should be displayed or not.
• Application triggers which are executed on all application pages.
@CustomComponent(displayName = "Removes entry (Test)") public class RemoveEntry implements MashupWidgetTrigger {
@Override public boolean beforeRendering(DataWidgetWrapper dww, TriggerContext triggerContext) { for (String feedName : dww.getResultFeeds().keySet()) { java.util.Iterator<Entry> it = dww.getResultFeeds().get(feedName).getEntries() .iterator(); while (it.hasNext()) { Entry entry = it.next(); if (entry.getMeta("source").equals("hotel")) { it.remove(); } } } return true; // return false to hide the widget }
@Override public void afterRendering(DataWidgetWrapper dww, TriggerContext triggerContext) { } }
Application triggers
The code is exactly the same as the one shown above. The only difference is the way you call them in Mashup Builder, that is to say, either on a page or on an application.
Implementing a page trigger
public class MyPageTrigger implements MashupTrigger<MashupPage> { /* Implementation */ }
Implementing a widget trigger
public class MyWidgetTrigger implements MashupTrigger<Widget> { /* Implementation */ }
Mashup API interface
Feed triggers
/** * Feed Trigger interface: A Feed Trigger is an entry point to alter the * behavior of a Feed. It is called before and after the feed execution, * allowing for query manipulation, context modification and result * manipulation. IMPORTANT: Feed Triggers are shared across the request and thus * MUST be both stateless and threadsafe. If you need to keep states * between the beforeQuery call and the afterQuery call, you can store the * variables in the QueryContext: * beforeQuery: context.setVariable("myComputedScore", 42); * afterQuery: context.getVariable("myComputedScore"); */ public interface FeedTrigger { /** * Before Feed Execution call. Return Result.STOP to skip the evaluation of * the feed, Result.CONTINUE for the normal behavior. * * @param feed * @param context * @throws AccessException */ public Result beforeQuery(Feed feed, QueryContext context) throws AccessException; /** * After Feed Execution call. Return Result.CONTINUE for the normal behavior, * or Result.EVAL_AGAIN to re-execute the feed after overriding a few * parameters. * * @param feed * @param context * @throws AccessException */ public Result afterQuery(Feed feed, QueryContext context, ResultFeed resultFeed) throws AccessException;
public enum Result { CONTINUE, STOP, EVAL_AGAIN } /** * A Trigger that implements BeforeSubfeedAware will receive an additional event after its execute() method, * but before the sub feeds processing. */ public static interface BeforeSubfeedAware {
public Result beforeSubfeeds(Feed feed, QueryContext context, ResultFeed resultFeed) throws AccessException;
} }
Feed trigger example
The following Java snippet is an example of basic query rewriting, replacing all occurrences of the word "rain" by the word "sun".
public class SampleTrigger implements FeedTrigger {
public Result beforeQuery(Feed feed, QueryContext context) throws AccessException { // Fetches the original q parameter String originalQuery = feed.getEvaluatedParameter(context, "q"); System.out.println("Original query: " + originalQuery);
// Computes the new query String newQuery = originalQuery.replace("rain", "sun"); System.out.println("New query:" + newQuery);
// Forces the "q" parameter to the new query feed.overrideParameter(context, "q", newQuery); return Result.CONTINUE; }
public Result afterQuery(Feed feed, QueryContext context, ResultFeed resultFeed) throws AccessException { return Result.CONTINUE; }
} // To get your custom code running into your CloudView 360 instance, compile it into // a .jar file and drop it in the javabin directory of your CloudView 360 kit. // Finally, to plug your Trigger in the Access.xml configuration file, // just add a <Trigger> tag to the targeted feed: