Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.9
-
None
-
None
-
Windows 7, 64 Bit
RHEL 7
Description
Hi there,
I got a similar error with AXIS2-5770 even using version no 1.7.9,
The error was
java.lang.ClassCastException: org.apache.axis2.saaj.SOAPElementImpl cannot be cast to org.apache.axiom.om.OMElement
2020-08-04 11:45:18 ERROR stderr - at org.apache.axis2.saaj.SOAPElementImpl.addChildElement(SOAPElementImpl.java:99)
2020-08-04 11:45:18 ERROR stderr - at org.apache.axis2.saaj.SOAPBodyImpl.toSAAJNode(SOAPBodyImpl.java:462)
2020-08-04 11:45:18 ERROR stderr - at org.apache.axis2.saaj.SOAPBodyImpl.toSAAJNode(SOAPBodyImpl.java:458)
2020-08-04 11:45:18 ERROR stderr - at org.apache.axis2.saaj.SOAPBodyImpl.addDocument(SOAPBodyImpl.java:298)
How to produce it :
Maven
<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-jaxws</artifactId> <version>1.7.9</version> </dependency> </dependencies>
Code
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource;/** * * * @author aswzen */ public class MainClass { public static void main(String[] args) throws IOException, SOAPException, JAXBException, ParserConfigurationException { String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:rm=\"rm:soap\"> <soapenv:Header/> <soapenv:Body> <rm:notifyUserInfo> <inPara> <content>UserID=123456789012345,QuotaInitialValue=102400,QuotaConsumption=38400,QuotaBalance=64000,QuotaLevel=1,NextResetTime=20140322000000,QuotaName=FUP001,TimeStamp=20140303182534</content> </inPara> </rm:notifyUserInfo> </soapenv:Body> </soapenv:Envelope>"; System.out.println("xml " + xml); SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(xml.getBytes())); JAXBContext jaxbContext = JAXBContext.newInstance(PCRFNotifyUserInfoRequest.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Node node = message.getSOAPBody().getFirstChild().getNextSibling(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document newDocument = builder.newDocument(); Node importedNode = newDocument.importNode(node, true); newDocument.appendChild(importedNode); Document dd = newDocument; PCRFNotifyUserInfoRequest response = (PCRFNotifyUserInfoRequest) jaxbUnmarshaller.unmarshal(dd); System.out.println("response " + response.getInParaContent()); PCRFNotifyUserInfoResponse pcrfResp = new PCRFNotifyUserInfoResponse(); pcrfResp.setResult("0", "Operation succeeded"); String res = createMarshal(pcrfResp, PCRFNotifyUserInfoResponse.class); System.out.println("results " + res); } public static String createMarshal(Object content, Class type) { String xmlContent = "?"; try { JAXBContext jaxbContextResp = JAXBContext.newInstance(type); Marshaller jaxbMarshaller = jaxbContextResp.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(content, sw); String bodyXml = sw.toString(); System.out.println("bodyXml " + bodyXml); MessageFactory mfactory = MessageFactory.newInstance(); SOAPMessage soapMessage = mfactory.createMessage(); soapMessage.getSOAPHeader().recycleNode(); SOAPBody soapBody = soapMessage.getSOAPBody(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); soapBody.addDocument(convertStringToDocument(bodyXml)); SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope(); envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); soapMessage.saveChanges(); ByteArrayOutputStream out = new ByteArrayOutputStream(); soapMessage.writeTo(out); xmlContent = new String(out.toByteArray()); } catch (Exception ex) { ex.printStackTrace(); xmlContent = "<result xmlns=\"\"><resultCode>1</resultCode><resultDesc>Operation error " + ex.getMessage() + "</resultDesc></result>"; } return xmlContent; } private static Document convertStringToDocument(String xmlStr) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } }
The error was on this part
soapBody.addDocument(convertStringToDocument(bodyXml));
Any explanation? thanks