This example demonstrates how to install and configure a custom interceptor, StaxConverterInterceptor, which transforms XML tag names from <foo> to <bar> using the Java Streaming API for XML (StAX). This approach allows for efficient and lightweight streaming XML transformations directly within the API Gateway.
Follow the steps below to build and deploy the custom StAX interceptor:
- Navigate to the
examples/xml/stax-interceptor/directory:cd <membrane-root>/examples/stax-interceptor
- Compile and package the interceptor using Maven:
mvn package
Upon successful packaging, the interceptor .jar file needs to be copied to Membrane's lib directory. This is automated using the maven-resources-plugin in the pom.xml file. During the package phase, the plugin copies the compiled .jar to the lib directory, ensuring it is available at runtime.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resource-stax</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>../../../lib</outputDirectory>
<resources>
<resource>
<directory>./target/</directory>
<includes>
<include>stax-converter-1.0-SNAPSHOT.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>Run the Membrane service proxy:
# For Unix/Mac
./membrane.sh
# For Windows
membrane.cmd- Open a second terminal.
- Review the
request.xmlfile, which contains the following XML:<bar xmlns="predic8.de/sample"> <foo>42</foo> </bar>
- Send the XML to the API:
curl -d @request.xml http://localhost:2000 -H "Content-Type: application/xml" - Observe the output, where the
<foo>element has been replaced by<bar>:<bar xmlns="predic8.de/sample"> <bar>42</bar> </bar>
- The interceptor is packaged as a
.jarfile using Maven. - The compiled
.jaris automatically copied to thelibdirectory of Membrane API Gateway during thepackagephase.
See: apis.yaml
- Request Interception: When a request is received, the interceptor inspects the XML payload.
- Tag Transformation: The interceptor uses the StAX API to parse and modify the XML, replacing
<foo>with<bar>. - Response Echoing: The
echointerceptor returns the modified request, allowing users to verify the transformation in the response.
By leveraging the StAX API, this interceptor performs efficient XML transformations, ensuring minimal performance overhead while maintaining flexibility in request processing.