2

我有一个自定义 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(...)

4

0 回答 0