简体   繁体   中英

In mule enricher how to acess the MuleMessage

I want to retrive a request context from http request without changing the payload, so I use enricher, but seems the enricher cannot be MuleMessage? I got the following error:

org.mule.model.resolvers.EntryPointNotFoundException: Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "com.ict.cos.transformer.RequestContextBuilder@53dc8cb8" does not implement required interface "interface org.mule.api.lifecycle.Callable"
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
ReflectionEntryPointResolver: Could not find entry point on: "com.ict.cos.transformer.RequestContextBuilder" with arguments: "{class org.apache.commons.httpclient.ContentLengthInputStream}"
AnnotatedEntryPointResolver: Component: com.ict.cos.transformer.RequestContextBuilder@53dc8cb8 doesn't have any annotated methods, skipping.
]

My Config:

<enricher source="#[message]" target="#[flowVars['requestContext']]" doc:name="Message Enricher">
    <component doc:name="Build request context">
        <spring-object bean="requestContextBuilder"/>
    </component>
</enricher>

The requestContextBuilder:

public class RequestContextBuilder {
    public RequestContext build(MuleMessage message) {
        RequestContext requestContext = new RequestContext();
        requestContext.setMethod(RESTMethod.get((String) message.getInboundProperty("http.method")));
        requestContext.setAPI((String) message.getInboundProperty("http.request.path"));
        requestContext.setQueryParams((String) message.getInboundProperty("http.query.string"));
        return requestContext;
    }
}

I don't think you can pass in the Message so I would multiple enrichers for each header value you want to be added to the enrichment.

by default the payload will be passed in as the source to the enricher

eg

<flow name="orderProcessingFlow">
<inbound-endpoint ref="orderEndpoint"/>
<enricher>
<authorizenet:authorize cardNumber="/order/cc/number" />
<enrich target="#[header:paymentValidated]" source="/authorizenet/authorization/@valid" />
<enrich target="#[header:paymentAuthCode]" source="/authorizenet/authorization/code"/>
</enricher>
<outbound-endpoint ref="orderStep2"/>
</flow>

Hope that helps!

如异常所示,尝试更改组件以使其实现org.mule.api.lifecycle.Callable,您将收到MuleEvent。

Little changes in code will do the needful.

First modify the component class to implement Mule's Callable interface

public class RequestContextBuilder implements Callable {

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception { 

        MuleMessage message = eventcontext.getMessage();
        RequestContext requestContext = new RequestContext();
        requestContext.setMethod(RESTMethod.get((String) message.getInboundProperty("http.method")));
        requestContext.setAPI((String) message.getInboundProperty("http.request.path"));
        requestContext.setQueryParams((String) message.getInboundProperty("http.query.string"));
        return requestContext;
    }    
}

Then modify the enricher to set payload (here it is requestContext) as the flowVariable.

<enricher source="#[payload]" target="#[flowVars['requestContext']]" doc:name="Message Enricher">
    <component doc:name="Build request context">
        <spring-object bean="requestContextBuilder"/>
    </component>
</enricher>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM