Search code examples
javaregexspringapache-camellookbehind

Can someone check this regex statement please?


I just need to verify that this regex statement will do what I want.

Given the following json string

{"a":"1","Provider":"WebHook","b":"2"}

I need to ensure that the following regex

(?<=\bProvider":")\w+

Will always return the word following the string Provider":"

In this case, the word following string Provider":" is WebHook, but it could be any word. I have control over this word, so it will never contain non-ascii characters.

I will be using this expression in Apache Camel, which uses the java regex engine.

Can anyone spot any pitfalls in my strategy.


Solution

  • Ok, thanks for the advice guys. I bit the bullet and did things the right way, instead of parsing the regex. Here's my solution:

    I built a service which uses the org.codehaus.jackson.map.ObjectMapper. This class takes the exchange body as an argument. It looks like this.

    public class ProviderTypeWrangler {
    
        public String getProvider(String json) {
    
            try {
    
                ObjectMapper mapper = new ObjectMapper();
                Integration integration;
                integration = mapper.readValue(json, Integration.class);
                return integration.getProvider();
            } catch (JsonParseException e) {
                return "";
            } catch (JsonMappingException e) {
                return "";
            } catch (IOException e) {
                return "";
            }
    
        }
    
    }
    

    I then used the routing slip pattern to provide access to this service. If anyone thinks another EIP would be appropriate, I'd be open for suggestions. Anyway, here's an example of that.

    public class WufooIntegrationRoutingSlip {
    
        @RoutingSlip
        public String slip(String body) {
            String answer = "activemq:noProducerDefined";
            ProviderTypeWrangler wrangler = new ProviderTypeWrangler();
            String producer = wrangler.getProvider(body);
            Logger mylogger = Logger.getLogger("log4j.logger.org.apache.camel");
            if (!producer.equals("")) {
                mylogger.info("Body:"+body);
                answer = "activemq:"+producer;
            }
            return answer;
        }
    
    }
    

    Then, in my camel.xml file I exposed this routing slip as a bean

    <bean id="integrationBean" class="com.wufoo.camel.WufooIntegrationRoutingSlip"/>
    

    And I used that bean to route the exchange to the correct queue.

    <route errorHandlerRef="dlc" autoStartup="true" id="IntegrationQueue" xmlns:ns2="http://camel.apache.org/schema/web" xmlns="http://camel.apache.org/schema/spring">
        <description>Send all integrations here.  Logic will parse to individual queue based on Provider.</description>
        <from uri="activemq:integration"/> 
        <bean ref="integrationBean"/>
    </route>
    

    I learned a lot about camel and spring along the way, so thanks to the commenters for pushing me in the right direction.