过滤器
过滤器是mentawai框架的组件。你可以在应用的某个action中进行配置,也可以对所有action进行配置。
在应用管理器中创建
// 应用于指定action上的过滤器
@Override
public void loadActions() {
action("/Hello", HelloAction.class, "sayHi")
.filter(new SomeFilter())
.on(SUCCESS, redir("/index.jsp"));
}
// 一个作用于所有action的全局过滤器
@Override
public void loadFilters() {
filter(new SomeGlobalFilter());
}
基本使用示例:
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() { }
}
一个简单的授权过滤器
tips:你不用编写授权过滤器,因为这是框架的特性之一
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() { }
}
在action执行之前或之后做修改
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()); // 在action执行之前
long now = System.currentTimeMillis();
String result = chain.invoke();
long totalTime = System.currentTimeMillis() - now;
output.setValue("timeAfter", new Date()); // action执行之后
output.setValue("totalTime", totalTime);
return result;
}
@Override
public void destroy() { }
}
在action结果执行之后修改
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() { }
}