REST サービスからの応答を処理するフローがあります。
<http:outbound-endpoint method="GET"
address="http://www.SomeAddress.com"
exchange-pattern="request-response">
...
<response>
<json:json-to-object-transformer/>
<custom-transformer ... />
</response>
</http:outbound-endpoint>
最近、サービスが更新され、html コンテンツを返すようになりました。応答で content-type を識別できるようにしたいと思います。
<http:outbound-endpoint method="GET"
address="http://www.SomeAddress.com"
exchange-pattern="request-response">
...
<response>
<choice>
<when content-type is html>
<custom-transformer-for-html ... />
</when>
<otherwise>
<json:json-to-object-transformer/>
<custom-transformer ... />
</otherwise>
</choice>
</response>
</http:outbound-endpoint>
さらに良いことに、コンテンツ タイプが認識されない場合は、従来の switch ステートメントと同様に、単純に例外をスローするとよいでしょう。
switch (content-type) {
case (html) : ...
case (json): ...
default: throw exception
}
Choice ステートメントでインバウンド ヘッダーの content-type をチェックできますか? デフォルトチェックでの切り替えは可能ですか?私が見ることができる例はありますか?
- - アップデート - -
以下のDavidsの提案を試しました:
<response>
<choice>
<when expression="#[message.inboundProperties['Content-Type'].contains('text/html')]">
<logger message="when number one invoked" level="WARN"/>
</when>
<when expression="#[message.inboundProperties['Content-Type'].contains('application/json')]">
<logger message="when number two invoked" level="WARN"/>
</when>
<otherwise>
<logger message="otherwise invoked" level="WARN"/>
</otherwise>
</choice>
</response>
しかし、コンパイル時に次のエラーが発生します。
cvc-complex-type.2.4.a: Invalid content was found starting with element 'choice'.
One of '{"http://www.mulesoft.org/schema/mule/core":abstract-transformer,
"http://www.mulesoft.org/schema/mule/core":abstract-filter,
"http://www.mulesoft.org/schema/mule/core":abstract-security-filter,
"http://www.mulesoft.org/schema/mule/core":abstract-intercepting-message-processor,
"http://www.mulesoft.org/schema/mule/core":abstract-observer-message-processor,
"http://www.mulesoft.org/schema/mule/core":processor,
"http://www.mulesoft.org/schema/mule/core":custom-processor}' is expected.
(org.xml.sax.SAXParseException)
org.apache.xerces.util.ErrorHandlerWrapper:-1 (null)