$BlogRSDURL$>
applicationContext-rules.xml
, into [appfuse]\web\WEB-INF\
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="ruleServiceProvider"
class="org.springmodules.jsr94.factory.DefaultRuleServiceProviderFactoryBean">
<property name="provider">http://drools.org/</value></property>
<property name="providerClass"><value>org.drools.jsr94.rules.RuleServiceProviderImpl</value>
</property>
</bean>
<bean id="ruleRuntime"
class="org.springmodules.jsr94.factory.RuleRuntimeFactoryBean">
<property name="serviceProvider"><ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleAdministrator"
class="org.springmodules.jsr94.factory.RuleAdministratorFactoryBean">
<ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleSource" class="org.springmodules.jsr94.rulesource.DefaultRuleSource">
<property name="ruleRuntime"><ref local="ruleRuntime"/></property>
<property name="ruleAdministrator"><ref local="ruleAdministrator"/></property>
<property name="source"><value>/WEB-INF/authorizedUsers.drl</value></property>
<property name="bindUri"><value>authorizedUsers</value></property>
</bean>
<bean id="rulesService" class="org.appfuse.service.impl.RulesServiceDrools">
<property name="ruleSource"><ref local="ruleSource"/></property>
</bean>
</beans>
authorizedUsers.drl
, is the ruleset, and RulesServiceDrools
is the Drools rule implementation we have in this sample setup.authorizedUsers.drl
, in [appfuse]\web\WEB-INF\
:
<?xml version="1.0" encoding="UTF-8"?>
<rule-set
name="Get authorized users"
description="Rules to retrieve authorized users"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<rule name="Enabled users">
<parameter identifier="user">
<class>org.appfuse.model.User</class>
</parameter>
<java:condition> user.isEnabled() != true </java:condition>
<java:consequence> drools.retractObject(user); </java:consequence>
</rule>
<rule name="Valid credentials">
<parameter identifier="user">
<class>org.appfuse.model.User</class>
</parameter>
<java:condition>
user.isCredentialsExpired() == true || user.getRoles().size() == 0
</java:condition>
<java:consequence>
drools.retractObject(user);
</java:consequence>
</rule>
</rule-set>
if (!user.isEnabled() && (user.isCredentialsExpired()
|| user.getRoles().size() == 0))
userList.remove(user);
RulesService
, is simple enough:
public interface RulesService {
public List getAuthorizedUsers(List users);
}
RulesServiceDrools
:
public class RulesServiceDrools extends Jsr94RuleSupport implements RulesService {
public final static String ACTIVE_USERS_URI="authorizedUsers";
public List getAuthorizedUsers(List users) {
return executeStateless(ACTIVE_USERS_URI, users);
}
}
public List getAuthorizedUsers(List users) {
List outputList = getTemplate().executeStateless(ACTIVE_USERS_URI,null,
new StatelessRuleSessionCallback() {
public Object execute(StatelessRuleSession session)
throws InvalidRuleSessionException, RemoteException {
return session.executeRules(users);
}
}
);
return outputList;
}
public abstract class AuthorizedUsers extends BasePage implements PageRenderListener {
public abstract UserManager getUserManager();
public abstract void setUserManager(UserManager manager);
public abstract RulesService getRulesService();
public abstract void setRulesService(RulesService svr);
public void pageBeginRender(PageEvent event) {
List authorizedUsers = getRulesService().getAuthorizedUsers(getUserManager().getUsers(null));
...
}
}
public final class LoginServlet extends HttpServlet {
...
public void execute(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
...
request.getSession().setAttribute(Constants.PREFERRED_LOCALE, request.getParameter("locale"));
...
}
}
public class ActionFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain)
throws IOException, ServletException {
...
if ((username != null) && (user == null)) {
...
String preferredLocale = session.getAttribute(Constants.PREFERRED_LOCALE).toString();
if (preferredLocale != null) {
if (preferredLocale "") {
if (user.getPreferredLocale() null) {
user.setPreferredLocale(Constants.DEFAULT_LOCALE);
}
} else {
user.setPreferredLocale(preferredLocale);
}
}
}
session.setAttribute(Constants.PREFERRED_LOCALE, user.getPreferredLocale());
}
}
public class LocaleFilter implements Filter {
private static final transient Log log = LogFactory.getLog(LocaleFilter.class);
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpSession session = servletRequest.getSession(false);
if (log.isDebugEnabled()) {
log.debug(servletRequest.getRequestURL());
}
Locale preferredLocale = null;
if (session!=null){
if (session.getAttribute(Constants.PREFERRED_LOCALE) != null) {
String localeKey = session.getAttribute(Constants.PREFERRED_LOCALE).toString();
if (localeKey != null) {
if (localeKey.equals("")) {
String[] keys = Constants.DEFAULT_LOCALE.split("-");
preferredLocale = new Locale(keys[0],keys[1]);
} else {
String[] keys = localeKey.split("-");
preferredLocale = new Locale(keys[0],keys[1]);
}
}
}
}
if (null != preferredLocale
&& !(request instanceof LocaleResponseWrapper)) {
request = new LocaleResponseWrapper(servletRequest, preferredLocale);
}
chain.doFilter(request, response);
}
}
public class LocaleResponseWrapper extends HttpServletRequestWrapper {
private final Locale preferredLocale;
private Enumeration locales;
public LocaleResponseWrapper(HttpServletRequest request, Locale sessionLocale) {
super(request);
preferredLocale = sessionLocale;
}
public Locale getLocale() {
if (null != preferredLocale) {
return preferredLocale;
} else {
return super.getLocale();
}
}
public Enumeration getLocales() {
if (null != preferredLocale) {
return setLocales();
} else {
return super.getLocales();
}
}
private Enumeration setLocales() {
if (null == locales) {
List l = Collections.list(super.getLocales());
l.add(0, preferredLocale);
locales = Collections.enumeration(l);
}
return locales;
}
}
<filter>
<filter-name>localeFilter</filter-name>
<display-name>Locale Filter</display-name>
<filter-class>org.appfuse.webapp.filter.LocaleFilter</filter-class>
</filter>
<fmt:setLocale value="${preferredLocale}" />
#
# DWR - https://dwr.dev.java.net/ (Direct Web Remoting)
#
dwr.version=1.0
dwr.dir=${lib.dir}/dwr-${dwr.version}
dwr.jar=${dwr.dir}/dwr.jar
<!-- Web -->
<path id="web.compile.classpath">
...
<pathelement location="${dwr.jar}"/>
</path>
<target name="package-web" depends="compile-web,jsp-2" description="Package WAR">
...
<war destfile="${webapp.dist}/${webapp.war}"
webxml="${webapp.target}/WEB-INF/web.xml" compress="true">
...
<lib file="${dwr.jar}"/>
</war>
</target>
<!-- dwr servlet -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<!-- dwr mapping -->
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<excludes>
<pattern>/dwr/*</pattern>
</excludes>
<dwr>
<allow>
<convert converter="bean" match="com.octasoft.fp.*"/>
<create creator="spring" javascript="userManager">
<param name="beanName" value="userManager"/>
<include method="getUser"/>
<include method="getUsers"/>
</create>
</allow>
</dwr>
<script src="/appfuse/dwr/engine.js" type="text/javascript"></script>
<script src="/appfuse/dwr/util.js" type="text/javascript"></script>
<script src="/appfuse/dwr/interface/userManager.js" type="text/javascript"></script>
<script>
DWREngine.setErrorHandler(doNothing);
var nickname = "";
function doNothing() {
return false;
}
function getNickname() {
if (nickname == "") {
userManager.getUser(popUser,document.loginForm.j_username.value);
document.loginForm.j_password.focus();
return false;
} else {
return validateForm( document.loginForm, false, true, true, false, 1)
}
}
var popUser = function(user) {
if (user == undefined) {
return false;
} else {
DWRUtil.setValue("nick", "Hello, " + user.nickName);
nickname = user.nickName;
}
}
</script>
<form method="post" name="loginForm" id="loginForm"
action="<c:url value="/authorize"/>" onsubmit="return getNickname();">
<div id="nick"></div>
<input name="j_username" type="text" id="j_username"/>
<input name="j_password" type="password" id="j_password"/>
</form>