SOAPHandler handleMessage返回自定义错误

时间:2015-04-01 09:47:43

标签: web-services soap jax-ws soapheader soaphandler

我有一个自定义SOAPHandler,handleMessage()如下:

public boolean handleMessage(SOAPMessageContext context) {
    // TODO Auto-generated method stub
    /* Step-1: Extract credentials from the SOAP header */
    Boolean isOutbound = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    log.info("In TransportHandler.handleMessage(...) " + isOutbound);

    if (!isOutbound) {

        SOAPMessage soapMsg = context.getMessage();
        try {
            SOAPUtil soapUtil = new SOAPUtil();
            Credentials credentials = soapUtil.retrieveCredentials(soapMsg);

            /* Validate credentials against the directory */
            SecurityUtil securityUtil = new SecurityUtil();
            securityUtil.authenticateClient(credentials.getUserName(),
                    credentials.getPassword());

        } catch (SecurityFault_Exception e) {
            // TODO Auto-generated catch block
            log.error(
                    "SecurityFault_Exception in TransportHandler.handleMessage(...)",
                    e);
            SOAPFault securityFault = null;
            try {
                securityFault = soapMsg.getSOAPBody().addFault();
                securityFault.setFaultString(e.getMessage());
                throw new SOAPFaultException(securityFault);
            } catch (SOAPException e1) {
                // TODO Auto-generated catch block
                log.error(
                        "SOAPException while handing SecurityFault_Exception in TransportHandler.handleMessage(...)",
                        e1);
            }

        } catch (RequestMessageFormatFault_Exception e) {
            // TODO Auto-generated catch block
            log.error(
                    "RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
                    e);
            SOAPFault requestFormatFault = null;
            try {
                requestFormatFault = soapMsg.getSOAPBody().addFault();
                requestFormatFault.setFaultString(e.getFaultInfo().getCustomMessage());
                throw new SOAPFaultException(requestFormatFault);
            } catch (SOAPException e1) {
                // TODO Auto-generated catch block
                log.error(
                        "SOAPException while handing RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
                        e1);
            }
        }

    }

    log.info("Returning from TransportHandler.handleMessage(...)");

    return true;
}

例如,如果某人没有添加安全标头,则以下是响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server</faultcode>
         <faultstring>Security header not present in the SOAP request</faultstring>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

现在在handleMessage(...)中,我已经为坏标头包装了自定义错误,如下所示:

catch (RequestMessageFormatFault_Exception e) {
                // TODO Auto-generated catch block
                log.error(
                        "RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
                        e);
                SOAPFault requestFormatFault = null;
                try {
                    requestFormatFault = soapMsg.getSOAPBody().addFault();
                    requestFormatFault.setFaultString(e.getFaultInfo().getCustomMessage());
                    throw new SOAPFaultException(requestFormatFault);
                } catch (SOAPException e1) {
                    // TODO Auto-generated catch block
                    log.error(
                            "SOAPException while handing RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
                            e1);
                }
            }

根据我们的组织。标准,我在wsdl中声明了以下错误(部分显示在这里):

.
.
.
<message name="RequestMessageFormatFault">
        <part name="RequestMessageFormatFault" element="sch:RequestMessageFormatFault" />
    </message>

    <message name="SecurityFault">
        <part name="SecurityFault" element="sch:SecurityFault" />
    </message>
    .
    .
    .
<portType name="TransportInformationDelegate">
        <operation name="GetTransportInformation">
            .
            .
            <fault name="RequestMessageFormatFault" message="tns:RequestMessageFormatFault"/>
            <fault name="SecurityFault" message="tns:SecurityFault"/>
        </operation>

    </portType>

<binding name="TransportInformationPortBinding" type="tns:TransportInformationDelegate">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <operation name="GetTransportInformation">
            .
            .
            .
            .
            <fault name="RequestMessageFormatFault">
                <soap:fault name="RequestMessageFormatFault" use="literal"/>
            </fault>
            <fault name="SecurityFault">
                <soap:fault name="SecurityFault" use="literal"/>
            </fault>
        </operation>

如何确保响应有此错误?我徒劳地尝试SOAPFault.addDetail(...)

0 个答案:

没有答案