编程语言
首页 > 编程语言> > java – Camel返回没有CXF / Spring-ws的简单SoapFault

java – Camel返回没有CXF / Spring-ws的简单SoapFault

作者:互联网

我创建了一个proxy-camel,它接受SOAP(通过HTTP)和RESTful请求,并将它们转发到正确的Web服务. Camel不知道消息结构,它不知道WSDL或任何东西,它根据http头只知道它是否是SOAP.没有CXF端点.

此外,它做了一些处理.例外情况可能发生在那里,例如,当找不到服务或网址无效时.
有没有一种简单的方法可以直接从这个驼峰返回有效的SOAPFault?
我试着编写一个名为onException的简单处理器.它看起来像这样:

.choice().when().header("SOAP").processRef(ExceptionToSoapProcessor())

应该将任何Exception转换为SOAPFault的处理器如下所示

@Override
public void process(Exchange exchange) throws Exception {
    Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    Integer responseCode = (Integer) exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE);

    QName qName = SoapFault.FAULT_CODE_SERVER;
    if (responseCode != null && responseCode < 500) {
        qName = SoapFault.FAULT_CODE_CLIENT;
    }

    SoapFault fault = new SoapFault(exception.getMessage(), qName);
    Message outMessage = exchange.getOut();
    outMessage.setHeader(Message.RESPONSE_CODE, 500);
    outMessage.setFault(true);
    outMessage.setBody(fault);

    exchange.setException(null);
    exchange.removeProperty(Exchange.EXCEPTION_CAUGHT);
    exchange.setProperty(Exchange.EXCEPTION_HANDLED, true);
}

但现在我不明白我将如何编组它,响应看起来像这样:

org.apache.cxf.binding.soap.SoapFault: Unauthorized

(“未经授权”是实际消息)

PS:之前我使用过dataformat SOAP,但如上所述,我在这个Camel中没有任何ServiceInterface.

解决方法:

我会将错误场景的处理移到onException()块.这样,您可以“声明”某些行为,例如将异常标记为已处理.恕我直言使它更清洁.

只返回SOAP错误不会产生有效的SOAP响应.您必须构建完整的消息结构.我认为没有用于文本流的SOAP消息的类型转换器,因此您必须自己编组SOAP响应.

这是我用来完成工作的代码:

<onException>
    <exception>java.lang.Exception</exception>
    <handled>
        <constant>true</constant>
    </handled>
    <bean beanType="some.package.WSHelper" method="createSOAPFaultServerError" />
</onException>


public static String createSOAPFaultServerError(final Exception cause) {
    String result = null;
    LOG.error("Creating SOAP fault, hiding original cause from client:", cause);
    try {
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPBody body = message.getSOAPBody();
        SOAPFault fault = body.addFault();
        fault.setFaultCode("Server");
        fault.setFaultString("Unexpected server error.");
        Detail detail = fault.addDetail();
        Name entryName = envelope.createName("message");
        DetailEntry entry = detail.addDetailEntry(entryName);
        entry.addTextNode("The server is not able to complete the request. Internal error.");

        result = soapMessage2String(message);
    } catch (Exception e) {
        LOG.error("Error creating SOAP Fault message", e);
    }

    return result;
}

private static String soapMessage2String(final SOAPMessage message) throws SOAPException, IOException {
    String result = null;

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    message.writeTo(outStream);
    result = new String(outStream.toByteArray(), StandardCharsets.UTF_8);

    return result;
}

HTH

标签:java,soap,web-services,apache-camel,soapfault
来源: https://codeday.me/bug/20190528/1170880.html