Filters are the building blocks of the Mentawai framework. You can configure them in the application manager by action or for all actions (global filter).
Set up in the application manager:
// a filter that will be applied to a specific action: @Override public void loadActions() { action("/Hello", HelloAction.class, "sayHi") .filter(new SomeFilter()) .on(SUCCESS, redir("/index.jsp")); } // a global filter that will be applied to all actions: @Override public void loadFilters() { filter(new SomeGlobalFilter()); }
Basic example:
import org.mentawai.core.*; import java.util.*; public class CacheFilter implements Filter { private static final String KEY = "cache"; @Override public String filter(InvocationChain chain) throws Exception { Action action = chain.getAction(); Context application = action.getApplication(); Map<String, Object> cache = (Map<String, Object>) application.getAttribute(KEY); if (cache != null) { Input input = action.getInput(); input.setValue(KEY, cache); } return chain.invoke(); // next filter or the action } @Override public void destroy() { } }
A simple authentication filter:
TIP: You don't have to worry about coding any authentication filter because Mentawai comes with a fully featured one.
import org.mentawai.core.*; public class AuthenticationFilter implements Filter { public static final String LOGIN = "login"; @Override public String filter(InvocationChain chain) throws Exception { Action action = chain.getAction(); Context session = action.getSession(); if (session.hasAttribute("user")) { return chain.invoke(); } return LOGIN; } @Override public void destroy() { } }
Modifying the action before and after its execution:
import org.mentawai.core.*; import java.util.Date; public class TimerFilter implements Filter { @Override public String filter(InvocationChain chain) throws Exception { Action action = chain.getAction(); Output output = action.getOutput(); output.setValue("timeBefore", new Date()); // before the action execution... long now = System.currentTimeMillis(); String result = chain.invoke(); long totalTime = System.currentTimeMillis() - now; output.setValue("timeAfter", new Date()); // after the action execution... output.setValue("totalTime", totalTime); return result; } @Override public void destroy() { } }
Modifying the action after the consequence has being executed:
package examples.helloworld.filter; import org.mentawai.core.*; import java.util.Date; public class TimerFilter implements AfterConsequenceFilter { @Override public String filter(InvocationChain chain) throws Exception { Action action = chain.getAction(); Output output = action.getOutput(); output.setValue("timeBefore", new Date().toString()); // before the action execution... long now = System.currentTimeMillis(); String result = chain.invoke(); long totalTime = System.currentTimeMillis() - now; output.setValue("timeAfter", new Date().toString()); // after the action execution... output.setValue("totalTime", totalTime); // save the initial time for the consequence here... Input input = action.getInput(); input.setValue("initialTime", System.currentTimeMillis()); return result; } @Override public void afterConsequence(Action action, Consequence c, boolean conseqExecuted, boolean actionExecuted, String result) { // Note: You should understand that after the consequence is executed it is too // late to include anything in the response to the client. if (!conseqExecuted) { System.out.println("There was an error executing the consequence!"); } else { Input input = action.getInput(); Long initialTime = (Long) input.getValue("initialTime"); long totalTime = System.currentTimeMillis() - initialTime.longValue(); System.out.println("The total time for the consequence was: " + totalTime); } } @Override public void destroy() { } }