Wednesday, July 13, 2011

Mule - Regex filter

Reg-ex filter applies the regular expression to the message payload. It is a best practice to ensure that the message payload is of type String.

mule-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
    xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
    xsi:schemaLocation="
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
       http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
       http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.mulesource.org/schema/mule/smtp/2.2 http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd">

    <stdio:connector name="stdioInConnector"
        outputMessage="Data : " promptMessage="Enter : " />

    <model>

        <service name="StdioService">
            <inbound>
                <stdio:inbound-endpoint system="IN"
                    connector-ref="stdioInConnector" />
            </inbound>
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="processInputIN" />
                </pass-through-router>
            </outbound>
        </service>

        <service name="ProcessInput">
            <inbound>
                <vm:inbound-endpoint path="processInputIN" />
            </inbound>
            <component class="org.mybusiness.components.ProcessInputComponent" />
            <outbound>
                <filtering-router>
                    <vm:outbound-endpoint path="computeLengthIN" />
                    <and-filter>
                        <regex-filter pattern="vijay(.*)" />
                        <payload-type-filter expectedType="java.lang.String"/>
                    </and-filter>
                </filtering-router>
                <logging-catch-all-strategy />
            </outbound>
        </service>

        <service name="ComputeLength">
            <inbound>
                <vm:inbound-endpoint path="computeLengthIN" />
            </inbound>
            <component
                class="org.mybusiness.components.ComputeLengthComponent" />
        </service>
    </model>
</mule>

Component Classes


package org.mybusiness.components;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ProcessInputComponent {
Log LOG = LogFactory.getLog(ProcessInputComponent.class);

public Object display(String data) throws Exception {
LOG.info("Input Message : " + data);
if (data.trim().length() == 0) {
return new Exception("Invalid Input");
}
return data;
}
}

package org.mybusiness.components;

public class ComputeLengthComponent {

public String compute(String input) {

int noOfAs = 0;

for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'a' || input.charAt(i) == 'A') {
noOfAs++;
}
}

System.out.println("Number of As : " + noOfAs);
return ("Number of As : " + noOfAs);
}
}

No comments:

Post a Comment