2012-08-30 50 views
2

給定一個Camel路由,該路由應該提取XML消息的某些內部部分,然後從中創建一條新消息,然後將其傳遞。Apache Camel/xpath操作結果檢測

from(SUB_EXTRACT_XML) 
    .setExchangePattern(ExchangePattern.InOut) 
    .setBody().xpath("//mmsg:MyMessage/mmsg:AnyPayload/*", namespaces) 
    .setBody().simple("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n${in.body}") 
    .to(...) 

對於(「嵌入」 XML消息的內部架構中爲xs定義:任何)這樣的正確的輸入信息,也因爲消息的工作是什麼,我希望它是:

<mmsg:MyMessage> 
    <mmsg:RandomTags/> 
    ... 
    <mmsg:AnyPayload> <!-- xs:any in xsd --> 
    <some><xml/><here/></some> 
    </mmsg:AnyPayload> 
</mmsg:MyMessage> 

鑑於存在一些問題,XML消息,如彩信網關:AnyPayload標籤丟失,從而使XPATH不能做自己的工作:

<mmsg:MyMessage> 
    <mmsg:RandomTags/> 
    ... 
    <some><xml/><here/></some> 
</mmsg:MyMessage> 

的XPATH將無法提取數據和整個XML m消息(包括mmsg:MyMessage)被傳遞,這不是預期的。我寧願在這個階段拋出一些例外。

問:

有沒有一種方法來檢查,如果XPath表達式居然發現元素refered到後來的路線,或者如果它未能提取特定元素(S)?

我知道我可以在之前對消息進行一些模式驗證並拒絕垃圾消息,但是有什麼方法可以查看XPath表達式是否失敗?

+0

你使用什麼版本的駱駝? –

+0

2.9.2在具體情況下。如果有幫助,我們可以升級到2.10.1。 –

+0

是啊給2.9.3一個嘗試。 –

回答

0

一種解決方案是使用choice() DSL在這樣的路線:

from(SUB_EXTRACT_XML) 
    .setExchangePattern(ExchangePattern.InOut) 
    .choice() 
     .when(xpath("//mmsg:MyMessage/mmsg:AnyPayload", namespaces)) 
      .setHeader("Status", "OK") // just for another example how to transmit some variable between two routes 
      .setBody().simple("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n${in.body}") 
      .endChoice() 
     .otherwise() 
      .log(LoggingLevel.ERROR, "LoggerName", "Error message; Stop the processing") 
      .stop() 
     .endChoice() 
    .end() 
    // Just to show the headers are following the route... 
    .to("DIRECT_GO_FORWARD"); 



from("DIRECT_GO_FORWARD") 
    .setExchangePattern(ExchangePattern.InOut) 
    .choice() 
     .when(header("Status").isEqualTo("OK")) 
      .bean(new SampleProcessor()) 

     ... 
    .end() 
    ... 
    .to("..."); 

第二條路線是隻是爲了顯示你可以使用標頭在第一路設置(與體內過多) 。