Tutorial: Build and deploy your own Interceptor using Maven and OSGi
Warning: This tutorial only works with Membrane ESB up to version 3.5.X. The 4.X branch still has some work left.
Lets assume you have just finished the previous tutorial, that explained how to extend ServiceMix with Membrane using a custom proxies.xml file.
For this tutorial, you need an installation of Eclipse, for example the Java EE edition.
1. Install the m2e Plugin
In Eclipse, select Help, Install Software from the menu. Choose a releases repository and install m2e - Maven Integration for Eclipse. Follow the dialog and restart Eclipse.
2. Create the New Project
Select File, New, Project... from the menu. Select Maven Project and Next. Then check Create simple project (skip archetype selection) and choose Next. Choose a Group Id and Archetype Id for your new project: In this example, we choose com.predic8 and com.predic8.membrane.tutorial.interceptor. Click Finish.
3. Add Dependencies and Build an OSGi Bundle
Replace the content of pom.xml with the following (except your groupId and artifactId in lines 6 and 7): (Use the left icon shown when hovering for Copy&Paste.)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>com.predic8.membrane.tutorial.interceptor</artifactId> <groupId>com.predic8</groupId> <packaging>bundle</packaging> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.predic8</groupId> <artifactId>membrane-esb-core</artifactId> <version>3.3.7-SNAPSHOT</version> <scope>provided</scope> </dependency> </dependencies> <repositories> <repository> <id>predic8-releases</id> <url>http://repository.membrane-soa.org/content/groups/public</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <configuration> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Bundle-Description>${project.description}</Bundle-Description> <Import-Package>*</Import-Package> </instructions> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> </plugin> </plugins> </pluginManagement> </build> </project>
Save this file, right-click the project and choose Maven, Update Project Configuration. (Note that some build errors will remain until you have completed the next step.)
4. Add your Interceptor Implementation
Add a class MyInterceptor in a package com.predic8.membrane.tutorial.interceptor with the following implementation:
package com.predic8.membrane.tutorial.interceptor; import com.predic8.membrane.core.exchange.Exchange; import com.predic8.membrane.core.http.MimeType; import com.predic8.membrane.core.http.Response; import com.predic8.membrane.core.interceptor.AbstractInterceptor; import com.predic8.membrane.core.interceptor.Outcome; public class MyInterceptor extends AbstractInterceptor { @Override public Outcome handleRequest(Exchange exc) throws Exception { exc.setResponse( Response.ok(). contentType(MimeType.TEXT_HTML_UTF8). body("This is an intercepted response."). build()); return Outcome.ABORT; } }
In this class you can add your customizations.
Any previous build errors should now have been resolved.
5. Add the Proxies and Beans Configuration Files
In src/main/resources, create the folder META-INF/membrane and add the following two files in it:
First, create a file called proxies.xml:
<proxies xmlns="http://membrane-soa.org/schemas/proxies/v1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://membrane-soa.org/schemas/proxies/v1/ http://membrane-soa.org/schemas/proxies/v1/proxies.xsd"> <serviceProxy port="9001" > <adminConsole/> </serviceProxy> <serviceProxy port="2001" > <interceptor refid="myInterceptor"/> </serviceProxy> </proxies>
Then, create a file called monitor-beans.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright 2009 predic8 GmbH, www.predic8.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <spring:beans xmlns="http://membrane-soa.org/router/beans/1/" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://membrane-soa.org/router/beans/1/ http:/www.predic8.com/schema/router/conf/router-conf.xsd"> <router/> <transport coreThreadPoolSize="5" socketTimeout="30000" tcpNoDelay="true" httpClientRetries="5" autoContinue100Expected="true"> <ruleMatching /> <dispatching /> <userFeature /> <httpClient /> </transport> <spring:bean id="transformerFactory" class="com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" /> <spring:bean id="myInterceptor" class="com.predic8.membrane.tutorial.interceptor.MyInterceptor" /> </spring:beans>
6. Install the Bundle into Your Local Maven Repository
Right-click the project and choose Run As and Maven install. The Console view should activate.
Wait until BUILD SUCCESS appears.
7. Install your Bundle in Karaf
Go to the Karaf shell, install and activate your new bundle:
karaf@root> _
8. The Result
Open http://localhost:2001/ in a browser and watch.
Also visit the admin console.