SourceForge.net Logo

create services

This page explains how to create a BERSERK filter.

filter's class

 
A filter can manipulate service's parameters and results. It can be a pre or post filter, meaning that it will be ran before or after the service invocation.
For this example we will use a pre-filter that checks if we are in February. If we are, the filter runs sucessfully. Otherwise it throws a OutOfValentines exception.
 
The filter is must implement BERSERK's IFilter interface. This obligates our class to implement the execute() method.
BERSERK will only call back the execute() method which does not mean that the filter's class can't have another methods.
public class IsFebruary implements IFilter
{

	public void execute(ServiceRequest req, ServiceResponse res) throws FilterException, Exception
	{
		Calendar calendar = new GregorianCalendar();
		int currentMonth = calendar.get(Calendar.MONTH);
		int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
		
		if (currentMonth != Calendar.FEBRUARY)
			throw new OutOfValentines("It is not valentine but women like flowers anyway !");
		if (currentDay > 14)
			throw new OutOfValentines("Two late. Prepare for revenge.");
	}
}							
									
Just because it exists, the filter does not do anything. It must be associated with a filter chain to be eventually run.

filter's configuration

 
The differences between using OJB and XML storage are explained in the Tutorials section of this website. Thus, in this page we will use XML storage and make no reference to how things would be if we were using OJB.
 
We will also include this filter in a filter chain expression. See the "how to create filter chains" page.
 
The configuration file should look like this:
<filterDefinitions>
	
<filter>
		<name>IsValentinesTime</name>
		<implementationClass>IsFebruary</implementationClass>
		<description>Checks
		if it is worth to buy flowers for Valentine's Day</description>
		<isTransactional>false</isTransactional>
	</filter>									
</filterDefinitions>
This filter assumes the class is in the application's default package.
 
The name and the implementationClass fields are different to remind that there is no relation between them: the name is the string we must provide in a filter chain's expression, and the implementing class is the full qualified name to the class where the filter's logic resides.
Just because it exists, the filter does not do anything. It must be associated with a filter chain to be run eventually.

filter exceptions

 
Our filter throws an exception, indicating that its result should be false. As stated in this site's filter composing language and exception handling sections, this is achieved by throwing a FilterException, or derived, exception:
public class OutOfValentines extends Exception
{
	
	public OutOfValentines(String message)
	{
		super(message);
	}


}

calling the filter

 
A filter can not be directly invoked. Instead it must be refered in a filter chain's expression. The filter chain should also be associated with a service, in order to the filter to be invoked when the service is executed.