Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * JBoss, Home of Professional Open Source 
   * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
   * as indicated by the @author tags. All rights reserved. 
   * See the copyright.txt in the distribution for a 
   * full listing of individual contributors.
   *
   * This copyrighted material is made available to anyone wishing to use, 
   * modify, copy, or redistribute it subject to the terms and conditions 
  * of the GNU Lesser General Public License, v. 2.1. 
  * This program is distributed in the hope that it will be useful, but WITHOUT A 
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details. 
  * You should have received a copy of the GNU Lesser General Public License, 
  * v.2.1 along with this distribution; if not, write to the Free Software 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  * MA  02110-1301, USA.
  */
  
 package org.switchyard.component.soap.util;
 
 
 
Contains utility methods to examine/manipulate SOAP Messages.

Author(s):
Magesh Kumar B <mageshbk@jboss.com> (C) 2011 Red Hat Inc.
 
 public final class SOAPUtil {
     private static final Logger LOGGER = Logger.getLogger(SOAPUtil.class);
    
 
     public static final QName SERVER_FAULT_QN = new QName("http://schemas.xmlsoap.org/soap/envelope/""Server");
     private static final boolean RETURN_STACK_TRACES = false;

    
SOAP Message Factory holder.
 
     public static final MessageFactory SOAP_MESSAGE_FACTORY;
 
     private SOAPUtil() {
     }
    
    
Retrieves the first element name in the SOAP Envelope's body.

Parameters:
soapMessage The SOAP message.
Returns:
The first Element name.
Throws:
javax.xml.soap.SOAPException If the SOAP message is invalid
 
     public static String getFirstBodyElement(final SOAPMessage soapMessagethrows SOAPException {
         String operationName = null;
         SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
         
         if (body != null) {
             Iterator<Nodenodes = body.getChildElements();
             Node node = null;
             while (nodes.hasNext()) {
                 node = nodes.next();
                 if (node instanceof Element) {
                     operationName = node.getLocalName();
                 }
             }
         }
         
         return operationName;
     }
    
    
Generates a SOAP Fault Message based on the Exception passed.

Parameters:
th The Exception.
Returns:
The SOAP Message containing the Fault.
Throws:
javax.xml.soap.SOAPException If the message could not be generated.
 
     public static SOAPMessage generateFault(final Throwable ththrows SOAPException {
         final SOAPMessage faultMsg = .createMessage();
        if (th instanceof SOAPFaultException) {
            // Copy the Fault from the exception
            SOAPFault exFault = ((SOAPFaultExceptionth).getFault();
            SOAPFault fault = faultMsg.getSOAPBody().addFault(exFault.getFaultCodeAsQName(), exFault.getFaultString());
            fault.setFaultActor(exFault.getFaultActor());
            if (exFault.hasDetail()) {
                Detail exDetail = exFault.getDetail();
                Detail detail = fault.addDetail();
                for (Iterator<DetailEntryentries = exDetail.getDetailEntries(); entries.hasNext();) {
                    Node entryImport = detail.getOwnerDocument().importNode(entries.next(), true);
                    detail.appendChild(entryImport);
                }
            }
        } else {
            if () {
                final StringWriter sw = new StringWriter();
                final PrintWriter pw = new PrintWriter(sw);
                th.printStackTrace(pw);
                pw.flush();
                pw.close();
                faultMsg.getSOAPBody().addFault(sw.toString());
            } else {
                String message = th.getMessage();
                if (message == null) {
                    message = th.toString();
                }
                faultMsg.getSOAPBody().addFault(message);
            }
        }
        return faultMsg;
    }

    
Create a new document based on a SOAP Message.

Parameters:
soapRes the SOAP Message
Returns:
the new document
Throws:
javax.xml.parsers.ParserConfigurationException for errors during creation
javax.xml.stream.XMLStreamException If the SOAP message could not be read
    public static Document parseAsDom(final String soapResthrows ParserConfigurationExceptionXMLStreamException {
        final XMLEventReader reader = XMLHelper.getXMLEventReader(new ByteArrayInputStream(soapRes.getBytes()));
        return XMLHelper.createDocument(reader);
    }

    
Generate String representation of SOAP message from javax.xml.soap.SOAPMessage.

Parameters:
msg SOAPMessage to parse
Returns:
String representation of SOAP message
    public static String soapMessageToString(SOAPMessage msg) {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter sw = new StringWriter();
            DOMSource source = new DOMSource(msg.getSOAPPart().getDocumentElement());
            StreamResult result = new StreamResult(sw);
            transformer.transform(sourceresult);
            return sw.toString();
        } catch (Exception e) {
            .error("Could not parse SOAP Message"e);
            return null;
        }
    }
    
    static {
        MessageFactory soapMessageFactory = null;
        try {
            soapMessageFactory = MessageFactory.newInstance();
        } catch (final SOAPException soape) {
            .error("Could not instantiate SOAP Message Factory"soape);
        }
         = soapMessageFactory;
    }
New to GrepCode? Check out our FAQ X