Question

My Request comes as below

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:hel="http://example.org/HelloService">
   <soapenv:Header>
<sampler>Test</sampler>     
   </soapenv:Header>
   <soapenv:Body>
  <hel:LastName>
     <lastName>Test</lastName>
  </hel:LastName>
   </soapenv:Body>
</soapenv:Envelope>

My flow which exposes my CXF:proxy-service is as below

<flow name="WS_In">
    <http:inbound-endpoint address="http://localhost:8080/HelloService"
        exchange-pattern="request-response">            
        <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
            namespace="http://example.org/HelloService" service="ProxyService" >
            <cxf:inInterceptors>
            <spring:bean id="inLogger"  
                class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
            <spring:bean id="msgInt" class="com.example.components.MessageInterceptor"/>            
            </cxf:inInterceptors>                           
        </cxf:proxy-service>
    </http:inbound-endpoint>
    <component>
        <spring-object bean="proxyService"></spring-object>         
    </component>        
    <echo-component></echo-component>   
</flow>

But I am not able to get the elements in the soap header. I tried using interceprots with method like below

@Override
public void handleMessage(SoapMessage message) throws Fault {
    System.out.println("The headers is " + message.getHeaders() );

}

But this is printing an emply Collection.

Please suggest how I can get this.

Was it helpful?

Solution

Try adding a custom-processor on your flow before the proxy-service

<flow name="WS_In">
<http:inbound-endpoint address="http://localhost:8080/HelloService"
    exchange-pattern="request-response">
    <custom-processor class="your.package.SoapHeaderObserver" />   
    <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
        namespace="http://example.org/HelloService" service="ProxyService" >
        <cxf:inInterceptors>
        <spring:bean id="inLogger"  
            class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
        <spring:bean id="msgInt" class="com.example.components.MessageInterceptor"/>            
        </cxf:inInterceptors>                           
    </cxf:proxy-service>
</http:inbound-endpoint>
<component>
    <spring-object bean="proxyService"></spring-object>         
</component>        
<echo-component></echo-component>   

public class SoapHeaderObserver { 
   doSomething(SoapMessage message) {
     //try to get header here
   } 
}

Also, your custom-processor can implement MessageProcessor or Callable. Take a look here on How to implement a Mule Message Observer?

And also, the framework have a lot of processors that you might use, instead of building your own.

OTHER TIPS

I got the solution be adding a message processor before my proxy-service.

Given below is my processor.

public class SOAPHeaderExtractor implements MessageProcessor {

    @Override
    public MuleEvent process(MuleEvent event) {     
        try
        {
            MuleMessage inputMessage =  event.getMessage(); 
            SOAPMessage soapMsg = MessageFactory.newInstance().createMessage(null, 
                    new java.io.ByteArrayInputStream(inputMessage.getPayloadAsString().getBytes()));
             SOAPHeader header = soapMsg.getSOAPHeader();            
             System.out.println(header.getElementsByTagName("sampler").item(0).getTextContent() );
        }
        catch(Exception e){
            e.printStackTrace();            
        }       
        return event ;
    }
}

And the change to my flow.

<http:inbound-endpoint address="http://localhost:8080/HelloService"
        exchange-pattern="request-response">
        <custom-processor class="com.example.processors.SOAPHeaderExtractor" /> 
        <cxf:proxy-service wsdlLocation="classpath:helloservice.wsdl"
            namespace="http://example.org/HelloService" service="ProxyService" >
            <cxf:inInterceptors  >
            <spring:bean id="inLogger" class="org.apache.cxf.interceptor.LoggingInInterceptor" />   
            </cxf:inInterceptors>   
        </cxf:proxy-service>        
</http:inbound-endpoint>

Please do suggst if there is any better way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top