public static SOAPEnvelope str2Envelope(String xmlString) { try { // TODO: verify the SOAP version in the string SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory(); OMElement documentElement = AXIOMUtil.stringToOM(xmlString); StAXSOAPModelBuilder builder = OMXMLBuilderFactory.createStAXSOAPModelBuilder( soapFactory, documentElement.getXMLStreamReader()); return builder.getSOAPEnvelope(); } catch (XMLStreamException e) { return null; } }
private Element getDOMElement(final OMElement omElement) { // Get the StAX reader from the created element XMLStreamReader llomReader = omElement.getXMLStreamReader(); // Create the DOOM OMFactory OMFactory doomFactory = DOOMAbstractFactory.getOMFactory(); // Create the new builder StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader); // Get the document element OMElement newElem = doomBuilder.getDocumentElement(); return newElem instanceof Element ? (Element) newElem : null; }
/** Simulate creating a SOAP 1.2 message when the business object provided is just the payload. */ public void testCreateSoap12FromPayload() throws Exception { // Create a SOAP 1.2 Message MessageFactory mf = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class); Message m = mf.create(Protocol.soap12); // Get the BlockFactory XMLStringBlockFactory f = (XMLStringBlockFactory) FactoryRegistry.getFactory(XMLStringBlockFactory.class); // Create a Block using the sample string as the content. This simulates // what occurs on the outbound JAX-WS dispatch<String> client Block block = f.createFrom(sampleText, null, null); // Add the block to the message as normal body content. m.setBodyBlock(block); // Assuming no handlers are installed, the next thing that will happen // is a XMLStreamReader will be requested...to go to OM. At this point the // block should be consumed. OMElement om = m.getAsOMElement(); // The block should not be consumed yet...because the message has not been read assertTrue(!block.isConsumed()); // To check that the output is correct, get the String contents of the // reader Reader2Writer r2w = new Reader2Writer(om.getXMLStreamReader()); String newText = r2w.getAsString(); TestLogger.logger.debug(newText); assertTrue(newText.contains(sampleText)); assertTrue(newText.contains("soap")); assertTrue(newText.contains("Envelope")); assertTrue(newText.contains("Body")); assertTrue(m.getProtocol().equals(Protocol.soap12)); SOAPEnvelope omSoapEnv = (SOAPEnvelope) m.getAsOMElement(); OMNamespace ns = omSoapEnv.getNamespace(); assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI)); // The block should be consumed at this point assertTrue(block.isConsumed()); }
/** * This will build a DOOM Element that is of the same <code>Document</code> * * @param factory * @param element * @return */ public static OMElement toDOOM(OMFactory factory, OMElement element) { StAXOMBuilder builder = new StAXOMBuilder(factory, element.getXMLStreamReader()); OMElement elem = builder.getDocumentElement(); elem.build(); return elem; }