Creating your own RegEx Plug-in

Following on from our article on how to create your own CSV Connectivity Plug-ins, in this article we will be discussing how you can create a slightly more complex plug-in – a RegEx plug-in. With this plug-in, you can define a regular expression and a value to replace it with to identify patterns and transform messages accordingly.

This plug-in is written in Java and includes a File Transport to read and write files (for simple testing), but this package also include more comprehensive system tests. We suggest you read the blog post on the CSV Plug-in for an overview of Connectivity Plug-ins before investigating the RegEx Plug-in.

The RegEx codec can translate messages both towards the host (correlator) and towards the transport. It extends the ‘com.softwareag.connectivity.AbstractSimpleCodec’ class, overriding both ‘transformMessageTowardsHost’ and ‘transformMessageTowardsTransport’ methods, performing alteration on the payload of the supplied Message parameter, and returning a new Message object that is passed onto the next plug-in in the chain.

The RegEx pattern and replacement String are read in from the YAML configuration file when the Plug-in chain is being constructed, and provided to the the Codec class during construction. This is an example of the configuration for the RegEx Codec:

- RegexCodec:
    regex: "[0-9]+"
    keys: ["payload"]

The configuration is supplied as a Map to the Codec class constructor and configuration keys can be extracted from it. Also included in the package is a class ‘SafeMapHelper’ that provides type-safe access to a heterogenous Map (where keys and values could be of any type). Here is an example Codec constructor which extracts the ‘regex’ and ‘repl’ String configs using the ‘SafeMapHelper’:

public RegexCodec(Map<String, Object> configMap, String chainId, Logger logger) throws Exception {
    super(configMap, chainId, logger);
    SafeMapHelper config = SafeMapHelper.createWithAutoRemove(configMap, getClass().getSimpleName() + ".config");
    // Config for regular expression and the replacement
    pattern = Pattern.compile(config.getString("regex"), flags);
    replacement = config.getString("repl");
    // raise an error if any unexpected config options were specified

The RegEx replacement is then applied to the payload and the metadata of the Message in the ‘transforMessage**’ methods. Depending on the structure of the payload, various things can happen. The RegEx is applied to primitives (string, boolean, integer, float, decimal) but Maps/dictionaries are traversed and the RegEx is only applied to the values. ‘input’ and ‘output’ keys are added to the output message Map which are then converted to fields in the Test event.

The RegEx can be applied to messages going in both directions (towards host or transport) but the Codec can also be configured to only apply the RegEx in one of these directions.

The Readme file included covers the basics of running a ‘test’ of the plug-in – and we’d encourage you to explore the code for the codec, the transport and the YAML file together (alongside the simple test EPL file). There’s more than one test included – looking over the inputs and outputs and perhaps making some modifications to those is a great way to get a feel of how things work as a whole. As with the CSV plugin, you’re free to explore the code and modify it as you wish for your projects – and, once again, we’d encourage you to do so!

As always, please post on our forums with any questions you may have. Thanks and happy downloading!

File Transport on GitHub
RegEx Codec on GitHub

Utilities and samples shown here are not official parts of the Software AG products. These utilities and samples are not eligible for technical support through Software AG Customer Care. Software AG makes no guarantees pertaining to the functionality, scalability, robustness, or degree of testing of these utilities and samples. Customers are strongly advised to consider these utilities and samples as “working examples” from which they should build and test their own solutions