May12
May12

Reputation: 2520

How to cast SAAJ object during handling SOAP message?

Colleagues, i have xml which need to be handle and configure before sending to client:

<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>[email protected]</ns2:Email>
                    </ns2:Applicant>
                </ns2:FullDataView>
            </in>
        </ns2:request>
    </soapenv:Body>
</soapenv:Envelope>

I use the next method to configure soap message:

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();
                     }
                }

The problem is that i can not cast the Object o5 (class com.sun.xml.internal.messaging.saaj.soap.impl.ElementImpl) to any types described in code. How correctly cast the object o5?

Thank you.

Upvotes: 1

Views: 7277

Answers (1)

Andreas Veithen
Andreas Veithen

Reputation: 9154

According to the Javadoc, the getChildElements method has the following behavior:

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

Therefore, before attempting to cast, you need to check whether the node is a Text or a SOAPElement.

Upvotes: 2

Related Questions