/** 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()); }
public void testGetMessageFromSoap12() throws Exception { // On inbound, there will already be an OM // which represents the message. The following code simulates the input // OM StringReader sr = new StringReader(sampleSoap12Envelope); XMLStreamReader inflow = inputFactory.createXMLStreamReader(sr); StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inflow, null); OMElement omElement = builder.getSOAPEnvelope(); // The JAX-WS layer creates a Message from the OM MessageFactory mf = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class); Message m = mf.createFrom(omElement, null); // Make sure the right Protocol was set on the Message assertTrue(m.getProtocol().equals(Protocol.soap12)); // Check the SOAPEnvelope to make sure we've got the right // protocol namespace there as well. SOAPEnvelope soapEnv = (SOAPEnvelope) m.getAsOMElement(); OMNamespace ns = soapEnv.getNamespace(); assertTrue(ns.getNamespaceURI().equals(SOAP12_NS_URI)); // Assuming no handlers are installed, the next thing that will happen // is the proxy code will ask for the business object (String). XMLStringBlockFactory blockFactory = (XMLStringBlockFactory) FactoryRegistry.getFactory(XMLStringBlockFactory.class); Block block = blockFactory.createFrom(m.getAsOMElement(), null, null); Object bo = block.getBusinessObject(true); assertTrue(bo instanceof String); // The block should be consumed assertTrue(block.isConsumed()); // Check the String for accuracy assertTrue(((String) bo).contains("<soapenv:Body><echo>test string</echo></soapenv:Body>")); }
/** * Creates a DOM Document using the SOAP Envelope. * * @param env An org.apache.axiom.soap.SOAPEnvelope instance * @return Returns the DOM Document of the given SOAP Envelope. * @throws Exception */ public static Document getDocumentFromSOAPEnvelope(SOAPEnvelope env, boolean useDoom) throws WSSecurityException { try { if (env instanceof Element) { return ((Element) env).getOwnerDocument(); } if (useDoom) { env.build(); // Workaround to prevent a bug in AXIOM where // there can be an incomplete OMElement as the first child body OMElement firstElement = env.getBody().getFirstElement(); if (firstElement != null) { firstElement.build(); } // Get processed headers SOAPHeader soapHeader = env.getHeader(); ArrayList processedHeaderQNames = new ArrayList(); if (soapHeader != null) { Iterator headerBlocs = soapHeader.getChildElements(); while (headerBlocs.hasNext()) { SOAPHeaderBlock element = (SOAPHeaderBlock) headerBlocs.next(); if (element.isProcessed()) { processedHeaderQNames.add(element.getQName()); } } } // Check the namespace and find SOAP version and factory String nsURI = null; SOAPFactory factory; if (env.getNamespace() .getNamespaceURI() .equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) { nsURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI; factory = DOOMAbstractFactory.getSOAP11Factory(); } else { nsURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI; factory = DOOMAbstractFactory.getSOAP12Factory(); } StAXSOAPModelBuilder stAXSOAPModelBuilder = new StAXSOAPModelBuilder(env.getXMLStreamReader(), factory, nsURI); SOAPEnvelope envelope = (stAXSOAPModelBuilder).getSOAPEnvelope(); ((OMNode) envelope.getParent()).build(); // Set the processed flag of the processed headers SOAPHeader header = envelope.getHeader(); for (Iterator iter = processedHeaderQNames.iterator(); iter.hasNext(); ) { QName name = (QName) iter.next(); Iterator omKids = header.getChildrenWithName(name); if (omKids.hasNext()) { ((SOAPHeaderBlock) omKids.next()).setProcessed(); } } Element envElem = (Element) envelope; return envElem.getOwnerDocument(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); env.build(); env.serialize(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return factory.newDocumentBuilder().parse(bais); } } catch (Exception e) { throw new WSSecurityException("Error in converting SOAP Envelope to Document", e); } }
public boolean mediate(MessageContext synCtx) { if (synCtx.getEnvironment().isDebugEnabled()) { if (super.divertMediationRoute(synCtx)) { return true; } } SynapseLog synLog = getLog(synCtx); if (synLog.isTraceOrDebugEnabled()) { synLog.traceOrDebug("Start : Fault mediator"); if (synLog.isTraceTraceEnabled()) { synLog.traceTrace("Message : " + synCtx.getEnvelope()); } } switch (soapVersion) { case SOAP11: makeSOAPFault(synCtx, SOAP11, synLog); break; case SOAP12: makeSOAPFault(synCtx, SOAP12, synLog); break; case POX: makePOXFault(synCtx, synLog); break; default: { // if this is a POX or REST message then make a POX fault if (synCtx.isDoingPOX() || synCtx.isDoingGET()) { makePOXFault(synCtx, synLog); } else { // determine from current message's SOAP envelope namespace SOAPEnvelope envelop = synCtx.getEnvelope(); if (envelop != null) { if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals( envelop.getNamespace().getNamespaceURI())) { makeSOAPFault(synCtx, SOAP12, synLog); } else { makeSOAPFault(synCtx, SOAP11, synLog); } } else { // default to SOAP 11 makeSOAPFault(synCtx, SOAP11, synLog); } } } } final Pipe pipe = (Pipe) (((Axis2MessageContext) synCtx)) .getAxis2MessageContext() .getProperty(PassThroughConstants.PASS_THROUGH_PIPE); if (pipe != null) { // cleaning the OUTPUT PIPE with older references // if there is a [protocal violation when sending out message etc.] pipe.getBuffer().clear(); pipe.resetOutputStream(); } // if the message has to be marked as a response mark it as response if (markAsResponse) { synCtx.setResponse(true); synCtx.setTo(synCtx.getReplyTo()); } return true; }