我正在编写CXF OutFaultInterceptor,以在Mule ESB版本3.8.1中实现SOAP Web服务时引发自定义SOAP故障消息
问题出在每个SOAP错误上,我从服务器获取HTTP状态代码500。对于错误的请求,我想根据错误类型(例如400)更改状态代码。
该怎么做?
我尝试将MESSAGE.RESPONSE_CODE设置为400,将PHASE设置为PRE_STREAM和MARSHAL。
但是它不起作用。下面是示例代码:
public class SampleCxfFaultInterceptor extends AbstractSoapInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(EservicesCxfFaultInterceptor.class);
public SampleCxfFaultInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(SoapMessage soapMessage) throws Fault {
Fault fault = (Fault) soapMessage.getContent(Exception.class);
if (fault.getCause() instanceof org.mule.api.transformer.TransformerMessagingException) {
LOGGER.error("INTERNAL ERROR OCCURED WHILE PROCESSING REQUEST);
soapMessage.remove(Message.RESPONSE_CODE);
soapMessage.put(Message.RESPONSE_CODE,new Integer(400));
Element detail = fault.getOrCreateDetail();
Element errorDetail = detail.getOwnerDocument().createElement("errorDetail");
Element errorCode = errorDetail.getOwnerDocument().createElement("errorCode");
Element message = errorDetail.getOwnerDocument().createElement("message");
errorCode.setTextContent("500");
message.setTextContent("INTERNAL_ERROR");
errorDetail.appendChild(errorCode);
errorDetail.appendChild(message);
detail.appendChild(errorDetail);
}
}
private Throwable getOriginalCause(Throwable t) {
if (t instanceof ComponentException && t.getCause() != null) {
return t.getCause();
} else {
return t;
}
}
预期结果将获得HTTP状态代码为400。
但是我得到的是这样的东西:HTTP / 1.1 500
答案 0 :(得分:1)
确保还重写handleFault
方法,并直接在HttpServletResponse对象上设置状态:
...
private void setHttpResponseStatus(final SoapMessage message, final int status)
{
// skip if inbound
if (!MessageUtils.isOutbound(message))
{
return;
}
// outbound
final Exchange exchange = message.getExchange();
final Message outMessage = Optional.ofNullable(exchange.getOutMessage()).orElse(exchange.getOutFaultMessage());
final HttpServletResponse httpResponse = (HttpServletResponse) outMessage.get(AbstractHTTPDestination.HTTP_RESPONSE);
if (httpResponse != null)
{
httpResponse.setStatus(status);
}
/*
* else this is likely SOAP over some non-HTTP transport protocol
*/
/*
* Prevent any further processing by other interceptors
*/
message.getInterceptorChain().abort();
}
@Override
public void handleMessage(final SoapMessage message) throws Fault
{
// example setting HTTP status to 400
setHttpResponseStatus(message, 400);
}
@Override
public void handleFault(final SoapMessage message)
{
// example setting HTTP status to 400
setHttpResponseStatus(message, 400);
}