/** * Builds a SOAPEnvelope from DOM Document. * * @param doc - The dom document that contains a SOAP message * @param useDoom * @return * @throws WSSecurityException */ public static SOAPEnvelope getSOAPEnvelopeFromDOMDocument(Document doc, boolean useDoom) throws WSSecurityException { if (useDoom) { try { // Get processed headers SOAPEnvelope env = (SOAPEnvelope) doc.getDocumentElement(); ArrayList processedHeaderQNames = new ArrayList(); SOAPHeader soapHeader = env.getHeader(); if (soapHeader != null) { Iterator headerBlocs = soapHeader.getChildElements(); while (headerBlocs.hasNext()) { OMElement element = (OMElement) headerBlocs.next(); SOAPHeaderBlock header = null; if (element instanceof SOAPHeaderBlock) { header = (SOAPHeaderBlock) element; // If a header block is not an instance of SOAPHeaderBlock, it means that // it is a header we have added in rampart eg. EncryptedHeader and should // be converted to SOAPHeaderBlock for processing } else { header = soapHeader.addHeaderBlock(element.getLocalName(), element.getNamespace()); Iterator attrIter = element.getAllAttributes(); while (attrIter.hasNext()) { OMAttribute attr = (OMAttribute) attrIter.next(); header.addAttribute( attr.getLocalName(), attr.getAttributeValue(), attr.getNamespace()); } Iterator nsIter = element.getAllDeclaredNamespaces(); while (nsIter.hasNext()) { OMNamespace ns = (OMNamespace) nsIter.next(); header.declareNamespace(ns); } // retrieve all child nodes (including any text nodes) // and re-attach to header block Iterator children = element.getChildren(); while (children.hasNext()) { OMNode child = (OMNode) children.next(); child.detach(); header.addChild(child); } element.detach(); soapHeader.build(); header.setProcessed(); } if (header.isProcessed()) { processedHeaderQNames.add(element.getQName()); } } } XMLStreamReader reader = ((OMElement) doc.getDocumentElement()).getXMLStreamReader(); StAXSOAPModelBuilder stAXSOAPModelBuilder = new StAXSOAPModelBuilder(reader, null); SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope(); // 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(); } } envelope.build(); return envelope; } catch (FactoryConfigurationError e) { throw new WSSecurityException(e.getMessage()); } } else { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); XMLUtils.outputDOM(doc.getDocumentElement(), os, true); ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray()); StAXSOAPModelBuilder stAXSOAPModelBuilder = new StAXSOAPModelBuilder( XMLInputFactory.newInstance().createXMLStreamReader(bais), null); return stAXSOAPModelBuilder.getSOAPEnvelope(); } catch (Exception e) { throw new WSSecurityException(e.getMessage()); } } }
public Object fromHeaderBlock(SOAPHeaderBlock sequenceAckElement) throws OMException, SandeshaException { originalSequenceAckElement = sequenceAckElement; OMElement identifierPart = null; Iterator childElements = sequenceAckElement.getChildElements(); while (childElements.hasNext()) { OMElement element = (OMElement) childElements.next(); String elementNamespace = element.getQName().getNamespaceURI(); String elementLocalName = element.getQName().getLocalPart(); if (namespaceValue.equals(elementNamespace)) { if (Sandesha2Constants.WSRM_COMMON.ACK_RANGE.equals(elementLocalName)) { String lowerAttribValue = element.getAttributeValue(new QName(Sandesha2Constants.WSRM_COMMON.LOWER)); String upperAttribValue = element.getAttributeValue(new QName(Sandesha2Constants.WSRM_COMMON.UPPER)); if (lowerAttribValue == null || upperAttribValue == null) throw new OMException( SandeshaMessageHelper.getMessage( SandeshaMessageKeys.noUpperOrLowerAttributesInElement, element.toString())); try { long lower = Long.parseLong(lowerAttribValue); long upper = Long.parseLong(upperAttribValue); acknowledgementRangeList.add(new Range(lower, upper)); } catch (Exception ex) { throw new OMException( SandeshaMessageHelper.getMessage( SandeshaMessageKeys.ackRandDoesNotHaveCorrectValues, element.toString())); } } else if (Sandesha2Constants.WSRM_COMMON.NACK.equals(elementLocalName)) { try { Long nack = Long.valueOf(element.getText()); nackList.add(nack); } catch (Exception ex) { throw new OMException( SandeshaMessageHelper.getMessage( SandeshaMessageKeys.nackDoesNotContainValidLongValue)); } } else if (Sandesha2Constants.WSRM_COMMON.IDENTIFIER.equals(elementLocalName)) { identifierPart = element; } else { String rmSpecVersion = SpecSpecificConstants.getSpecVersionString(namespaceValue); if (SpecSpecificConstants.isAckFinalAllowed(rmSpecVersion)) { if (Sandesha2Constants.WSRM_COMMON.FINAL.equals(elementLocalName)) { ackFinal = true; } } if (SpecSpecificConstants.isAckNoneAllowed(rmSpecVersion)) { if (Sandesha2Constants.WSRM_COMMON.NONE.equals(elementLocalName)) { ackNone = true; } } } } } identifier = new Identifier(namespaceValue); identifier.fromOMElement(identifierPart); // Indicate that we have processed this part of the message. sequenceAckElement.setProcessed(); return this; }