如何在处理 SOAP 消息期间转换 SAAJ 对象?

How to cast SAAJ object during handling SOAP message?

同事们,我有xml需要在发送给客户端之前进行处理和配置:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns2:request xmlns:ns2="http://mayacomp/Generic/Ws">
            <in xmlns="http://mayacomp/Generic/Ws">
                <requestHeader>
                    <requestUid>1234567890</requestUid>
                    <ns2:requestTimestamp>2015-02-11T07:46:00.000</ns2:requestTimestamp>
                </requestHeader>
                <ns2:FullDataView>
                    <ns2:ApplicationData>
                        <ns2:ProductType>0</ns2:ProductType>
                    </ns2:ApplicationData>
                    <ns2:ProductDetails>
                        <ns2:ProductCode>605</ns2:ProductCode>
                    </ns2:ProductDetails>
                    <ns2:Applicant>
                        <ns2:Surname>Клавиатурова</ns2:Surname>
                        <ns2:Address>
                            <ns2:Type>1</ns2:Type>
                        </ns2:Address>
                        <ns2:Address>
                            <ns2:Type>2</ns2:Type>
                        </ns2:Address>
                        <ns2:Email>test@test.ru</ns2:Email>
                    </ns2:Applicant>
                </ns2:FullDataView>
            </in>
        </ns2:request>
    </soapenv:Body>
</soapenv:Envelope>

我使用下一个方法来配置 soap 消息:

public void doWithMessage(WebServiceMessage message) {

                        SaajSoapMessage saajSoapMessage = (SaajSoapMessage)message;

                    try {

                        SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
                        SOAPPart soapPart = soapMessage.getSOAPPart();
                        SOAPEnvelope envelope = soapPart.getEnvelope();
                        log.info("SOAPEnvelope prefix " +  envelope.getPrefix());
                        envelope.removeNamespaceDeclaration(envelope.getPrefix());
                        envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                        envelope.setPrefix("soapenv");

                        /*Remove header*/
                        SOAPHeader header = soapMessage.getSOAPHeader();
                        header.detachNode();


                        SOAPBody body = envelope.getBody();
                        log.info("SOAPBody prefix " +  body.getPrefix());
                        body.removeNamespaceDeclaration(body.getPrefix()) ;
                        body.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                        body.setPrefix("soapenv");


                        /*Prepare body elements*/
                        Iterator itBodyElements = body.getChildElements();

                        String elementName = null;
                        /*Log all elements from body*/
                        log.info("Body elements: ");
                        NodeList nodeList = body.getElementsByTagName("*") ;
                        for (int i = 0; i < nodeList.getLength(); i++) {
                            Node node = nodeList.item(i);
                            if (node.getNodeType() == Node.ELEMENT_NODE) {
                                log.info(node.getNodeName());
                            }
                        }

                        /*Config body elements*/
                        while (itBodyElements.hasNext()) 
                        { 
                          Object o = itBodyElements.next();
                          SOAPBodyElement bodyElement = (SOAPBodyElement) o;
                          log.info("Elements from 'Body' element = " + bodyElement.getLocalName() );

                          Iterator it2 = bodyElement.getChildElements();
                                 while (it2.hasNext()) 
                                 { 
                                      Object requestElement = it2.next();
                                      SOAPBodyElement bodyRequest = (SOAPBodyElement) requestElement;
                                      log.info("  Elements from '"+ bodyElement.getLocalName() + "' element = " + bodyRequest.getLocalName()); 
                                      bodyRequest.removeNamespaceDeclaration(bodyRequest.getPrefix());
                                      bodyRequest.setPrefix("");

                                       Iterator it3 = bodyRequest.getChildElements();
                                            while (it3.hasNext())
                                            {
                                              Object bodyRequestElement = it3.next();
                                              SOAPBodyElement requestElementName = (SOAPBodyElement) bodyRequestElement;
                                              log.info("   Elements from 'In' name = " + requestElementName.getLocalName());
                                              requestElementName.setPrefix("");
                                              elementName =  requestElementName.getLocalName();

                                              Iterator it4  = requestElementName.getChildElements();                                              

                                                while (it4.hasNext()) 
                                                          {
                                                              Object o4 = it4.next();
                                                              SOAPBodyElement bodyElement4 = (SOAPBodyElement) o4;
                                                              log.info("    Element from '" + elementName + "' name = " + bodyElement4.getLocalName());
                                                              bodyElement4.setPrefix("");

                                                            Iterator it5 =  bodyElement4.getChildElements(); 

                                                            while (it5.hasNext())
                                                            {
                                                                Object o5 = it5.next(); 
                                                                //What it tried?
                                                            // TextImpl child5 = (TextImpl) o5; //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl cannot be cast to com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl
                                                            // ElementImpl child5 = (ElementImpl) o5; //Result:java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl cannot be cast to com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl
                                                            // SOAPElement child5 = (SOAPElement)o5; //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPElement
                                                            // SOAPBodyElement child5 = (SOAPBodyElement) o5;  //java.lang.ClassCastException: com.sun.xml.internal.messaging.saaj.soap.impl.TextImpl cannot be cast to javax.xml.soap.SOAPBodyElement
                                                                log.info(o5.getClass().toString());
                                                            }
                                                           }
                                            }
                                 }

                       } 
                     } catch (Exception e) {
                        e.printStackTrace();
                     }
                }

问题是我无法将对象 o5 (class com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl) 转换为代码中描述的任何类型。 如何正确投射对象 o5?

谢谢。

根据 JavadocgetChildElements 方法具有以下行为:

Returns an Iterator over all the immediate child Nodes of this element. This includes javax.xml.soap.Text objects as well as SOAPElement objects.

因此,在尝试投射之前,您需要检查节点是 Text 还是 SOAPElement