Go to new doc!

+49 228 5552576-0


info@predic8.com

Interceptor Configuration

Interceptors are the building blocks of the Membrane Router. This article shows the different ways how you can set up and configure an interceptor with the Membrane Router. To find out how they work refer to Interceptors.

The Configuration File monitor-beans.xml

The interceptors are configured in the monitor-beans.xml file. Its a XML document that contains a spring configuration. The router uses the Spring Framework to build up the object hierarchy it depends on. The object hierarchy is defined in the spring configuration provided by the monitor-beans.xml file. The interceptors are part of that object hierarchy. So to configure interceptors you have to know something about spring configurations.

Spring Configuration Files

There are more to know about spring configuration files than it is described here. For a complete reference visit the Spring homepage. For this tutorial we will only cover some aspects so that you are able to configure interceptors for the router.

Spring configuration files are XML documents. They describe an object hierarchy. The XML document structure of the configuration file reflects the structure of the object hierarchy.

In the configuration file objects are represented by bean definitions. They consist of a bean element, that has an attribute class that contains the full classname of the represented object. To set properties of the object one can use one or more nested property elements. The following listing shows a bean definition for the StatisticsCSVInterceptor.

				
				<bean class="com.predic8.membrane.core.interceptor.statistics.StatisticsCSVInterceptor">
				  <property name="fileName" value="C:\temp\exc-statistics.csv" /> 
				</bean> 
				
			

The StatisticsCSVInterceptors full classname is com.predic8.membrane.core.interceptor.statistics.StatisticsCSVInterceptor. Further it has a property called fileName. Its value is set to C:\temp\exc-statistics.csv by using the nested element property. The StaticsitcCSVInterceptors logs the metadata of an exchange as semicolon seperated values to a file specified by fileName.

Configuration Scopes

An interceptor can be engaged with

  1. A Transport. The interceptor will than be called every time a request is routed through the transport you registered them with.
  2. A Rule. The interceptor will only by invoked if the request is matched by the rule that references the interceptor. This gives you fine grained control over the invokation of interceptors.

Register Interceptor By Transport

To register an interceptor by transport open the monitor-beans.xml file. Locate the bean definition for the transport. It should have an id attribute with the value transport and can look like this:

					
					<bean id="transport" class="com.predic8.membrane.core.transport.http.HttpTransport">
					...
					</bean>
					
				
The transport bean has a property called interceptors. The property element will have a nested element named list. To register an interceptor simply append its bean definition as a child to the list element. To register the StatisticsCSVInterceptor the transport bean definition can look like this:
					
					<bean id="transport" class="com.predic8.membrane.core.transport.http.HttpTransport">
						...
						<property name="interceptors">
							<list>
							...
							<bean 	class="com.predic8.membrane.core.interceptor.statistics.StatisticsCSVInterceptor">
								<property name="fileName" value="C:\temp\exc-statistics.csv" /> 
							</bean>
							...
							</list>
						</property>
					</bean>
					
				

Global Bean Definitions

To define an interceptor as a global bean open the monitor-beans.xml. Create a bean definition as a child of the beans element. Don't forget to set an id attribute and a property called displayName. The id attribute is used to reference the bean later. The displayName is used by the Monitor GUI as a user friendly name for the interceptor. To define a global bean for the StatisticsCSVInterceptor the bean definition can look like this:

					
					<beans ...>
						...
						<bean 	id="statisticsCSVInterceptor"
							class="com.predic8.membrane.core.interceptor.statistics.StatisticsCSVInterceptor">
							<property name="fileName" value="C:\temp\exc-statistics.csv" />
							<property name="displayName" value="Statistics CSV Logger" />
						</bean>
					</beans>
					
				

Referencing Interceptors

If you have defined a interceptor as a global bean you can reference it from rules. The simplest way to reference an interceptor is to do it by the Monitor GUI. If you use the Membrane Router you will have to modify the rules.xml configuration file.

Monitor GUI

To assign an interceptor to a rule choose the rule from the rules list and click the Edit button.

List view of configured rules.

Figure1: List view of configured rules

In the rules dialog switch to the interceptors tab. All interceptors that are configured for the rule are listed under the interceptors tab. Klick the Add button to assign a new interceptor.

List view of configured rules.

Figure2: List of assigned interceptors

The following dialog shows alle interceptors that are registered globally. Choose the Statistics CSV Logger interceptor and klick the Ok button.

List view of configured rules.

Figure3: List of all available interceptors

The Statistics CSV Logger will be listed in the interceptors tab. Klick the Ok button to close the rules dialog.

List view of configured rules.

Figure4: Assigned Statistics CSV Logger interceptor

Now you have assigned a Statistics CSV Logger interceptor to the rule you choosed befor.

Rules.xml

If you are only working with the Membrane Router and you want to configure an interceptor for a rule, you have to work with the rules.xml file.

The rules.xml file is a XML document. Its root element is configuration. This element contains an element called rules that contains all rule configurations. For each rule there exists a child in the rules element. Each child describes a rule and can contain an element called interceptors. To assign an interceptor to a rule simply add a child called interceptor to the interceptors element. The interceptor element should have an id attribute thats value matches the id attribute of the global bean definition of the interceptor you want to assign.

To assign the StatisticsCSVInterceptor we have defined befor. A rules element can look like this:

					
<rules>
	<forwarding-rule ...>
		...
		<interceptors>
			<interceptor id="statisticsCSVInterceptor"/>
		</interceptors>
	</forwarding-rule>
	...
</rules>