File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes FRom Element to SOAP Envelope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "FRom Element to SOAP Envelope" Watch "FRom Element to SOAP Envelope" New topic
Author

FRom Element to SOAP Envelope

taktak hajer
Greenhorn

Joined: Sep 24, 2012
Posts: 3
Hi,

I want to get SOAPEnvelope from a DOM document. I wrote this class :


public static SOAPEnvelope getSOAPEnvelopeFromDOMDocument(Document doc, boolean useDoom)
throws WSSecurityException {

Element documentElement = doc.getDocumentElement();
if (documentElement instanceof SOAPEnvelope) {
SOAPEnvelope env = (SOAPEnvelope)documentElement;
// If the DOM tree already implements the Axiom API and the corresponding
// Axiom implementation is also used as default implementation, then just return
// the SOAPEnvelope directly. Note that this will never be the case for DOOM,
// but may be the case for a non standard Axiom implementation.
if (env.getOMFactory().getMetaFactory() == OMAbstractFactory.getMetaFactory()) {
return env;
}
}

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();
children.remove();
header.addChild(child);
}

headerBlocs.remove();

soapHeader.build();

header.setProcessed();

}

if(header.isProcessed()) {
processedHeaderQNames.add(element.getQName());
}
}

}
XMLStreamReader reader = ((OMElement) doc.getDocumentElement())
.getXMLStreamReader();
SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(
reader);
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());

SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createSOAPModelBuilder(bais, null);
return stAXSOAPModelBuilder.getSOAPEnvelope();
} catch (Exception e) {
throw new WSSecurityException(e.getMessage());
}
}
}

The problem concern the cast every time i execute, i got this error : xception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl cannot be cast to org.apache.axiom.soap.SOAPEnvelope

Any help please :''((

thks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FRom Element to SOAP Envelope