Apache Camel Java and XPath

一个人想着一个人 提交于 2019-12-13 04:57:36

问题


So I have a web service with several namespaces that I would like to route through a bean to do some checking of user credentials. Its been a long time since I used XPATH so I might just be having a PICNIC(Problem In Chair Not In Computer Moment) error.

The web service message will always have the following structure/pattern :

<Operation>
    <header with the head name space where the user credentials are stored>
    <record control>
    <objXX>
</Operation>

Here is a example message(SOAP UI):

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:list="http://www.insol.irix.com.au/IRIX_V1/Debtors/List" xmlns:head="http://www.insol.irix.com.au/IRIX_V1/Headers" xmlns:rec="http://www.insol.irix.com.au/IRIX_V1/RecordControl">
 <soapenv:Header/>
   <soapenv:Body>
      <list:ListDebtorReq>
         <head:MsgReqHdr>
            <head:MsgGUID>${=java.util.UUID.randomUUID()}</head:MsgGUID>
        <head:MsgDateTime>${=javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(GregorianCalendar.getInstance())}</head:MsgDateTime>
        <head:ConsumerSystemIDInfo>
           <head:ConsumerSystemID>ConsumerSystemID</head:ConsumerSystemID>
           <head:ConsumerSystemUserID>AgentX</head:ConsumerSystemUserID>
        </head:ConsumerSystemIDInfo>
        <head:SecCredInfo>
           <head:IRIXUserID>Some User ID</head:IRIXUserID>
           <head:IRIXPassword>Some Password</head:IRIXPassword>
        </head:SecCredInfo>
        <head:CryptoInfo>
           <head:DigitalSignatureInfo>
              <head:DigitalSignatureValue>verrantque per auras</head:DigitalSignatureValue>
              <head:DigitalSignatureAlgorithm>SHA-256</head:DigitalSignatureAlgorithm>
           </head:DigitalSignatureInfo>
        </head:CryptoInfo>
     </head:MsgReqHdr>
     <!--Optional:-->
     <rec:RecCntrl>
        <rec:StartRecordNumber>1</rec:StartRecordNumber>
        <!--Optional:-->
        <rec:NumberOfRecords>3</rec:NumberOfRecords>
     </rec:RecCntrl>
  </list:ListDebtorReq>
  </soapenv:Body>
</soapenv:Envelope>

So essentially I want to be able to create a bean that will be able to query the MsgReq header for all the user name and password data. To simplify things I am just trying to query the MsgGUID and work my way from there. However I cant seem to get the xpath right. Since I am using several namespaces I have included them in the camel context file just to make sure they are available.

Here is my camel-context:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:camel="http://camel.apache.org/schema/spring"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://camel.apache.org/schema/spring 
     http://camel.apache.org/schema/spring/camel-spring.xsd">
  <import resource="classpath:META-INF/spring/camel-cxf.xml" />
  <bean id="SecurityCheckBean" class="au.com.irix.insol.Security.IRIXSecurity"/>
  <camelContext xmlns="http://camel.apache.org/schema/spring"
   xmlns:list="http://www.insol.irix.com.au/IRIX_V1/Debtors/List" 
   xmlns:head="http://www.insol.irix.com.au/IRIX_V1/Headers" 
   xmlns:rec="http://www.insol.irix.com.au/IRIX_V1/RecordControl">
   <route>
    <from uri="cxf:bean:DebtorsService?dataFormat=PAYLOAD"/>    
    <bean ref="SecurityCheckBean"/>
   </route>
</camelContext>

</beans>

As you can see I am running the incoming message of the web service producer to the SecurityCheckBean. My SecurityCheckBean is super simple at the moment see code below.

  public class IRIXSecurity {



    public void CheckCredentials(


            @XPath("//head:MsgGUID") String msgGUID,
            @Body String body){


        System.out.println(body);
        System.out.println("Check Credentials Invoked");
        System.out.println(msgGUID);



    }
}

However when I send a send a request via soap UI I get the following exception:

Invalid xpath: //head:MsgGUID. Reason: javax.xml.xpath.XPathExpressionException: net.sf.saxon.trans.XPathException: Prefix head has not been declared

So how do I go about retrieving this information? Why even though I have declared the name spaces in my camel-context.xml they are reported as missing?

Just for interest sake I have tried several variations of the XPATH such as:

@XPath("//MsgGUID")
@XPath("//MsgReqHdr/head:MsgGUID")
@XPath("//head:MsgReqHdr/head:MsgGUID")

Every time I either get an exception as listed above or a NULL value...


回答1:


Right got it to work. When dealing with the namespaces in a bean the following syntax must be used to include the namespaces.

public class IRIXSecurity {



    public void CheckCredentials(
            //@Body ListDebtorReqType msgBody, @Headers Map hdr,

            @XPath(value="//header:MsgGUID",namespaces = @NamespacePrefix(
                    prefix = "header",
                    uri = "http://www.insol.irix.com.au/IRIX_V1/Headers")) String msgGUID,
            @Body Document xml)
    {


        System.out.println("Check Credentials Invoked");
        System.out.println(msgGUID);
        //exchange.getOut().setBody(debtorRsType);





    }
}


来源:https://stackoverflow.com/questions/16765255/apache-camel-java-and-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!