Example usage for javax.xml.soap SOAPMessage getSOAPPart

List of usage examples for javax.xml.soap SOAPMessage getSOAPPart

Introduction

In this page you can find the example usage for javax.xml.soap SOAPMessage getSOAPPart.

Prototype

public abstract SOAPPart getSOAPPart();

Source Link

Document

Gets the SOAP part of this SOAPMessage object.

Usage

From source file:edu.unc.lib.dl.util.SOAPUtil.java

private static void print(SOAPMessage msg, StreamResult result) {
    try {//from   www.j a  v  a 2 s .  c o  m
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        Source sourceContent = msg.getSOAPPart().getContent();
        //StreamResult result = new StreamResult(out);
        transformer.transform(sourceContent, result);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (SOAPException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds a specified namespace to a {@link SOAPMessage}.
 *
 * @param message The {@link SOAPMessage} to add the namespace to.
 * @param prefix namespace prefix/*ww  w.  j  a va  2s. co  m*/
 * @param uri namespace URI
 * @throws SOAPException
 */
public static void addNamespace(SOAPMessage message, String prefix, String uri) throws SOAPException {
    message.getSOAPPart().getEnvelope().addNamespaceDeclaration(prefix, uri);
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
 * Returns true, if the SOAP message is RPC-encoded.
 * @param soap the SOAP message//w  ww  .  j  a  v  a 2  s .c o m
 * @return boolean
 * @throws Exception if there are errors reading the SOAP envelope
 */
public static boolean isRpcMessage(SOAPMessage soap) throws Exception {
    SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
    return RPC_ENCODING.equals(envelope.getAttribute(RPC_ATTR));
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
        /*w w  w  . jav  a2  s  .  com*/
 * Returns namespace URIs from a SOAPMessage.
 * @param soap soap message from which to retrieve namespace URIs
 * @return a List of namespace URI Strings
 * @throws Exception if there is a SOAP error
 */
public static List<String> getNamespaceURIs(SOAPMessage soap) throws Exception {
    List<String> nsURIs = new ArrayList<>();

    SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
    Iterator<?> it = envelope.getNamespacePrefixes();
    while (it.hasNext()) {
        nsURIs.add(envelope.getNamespaceURI((String) it.next()));
    }

    return nsURIs;
}

From source file:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

private static final SOAPMessage toMessage(Document jdomDocument) throws IOException, SOAPException {
    SOAPMessage message = MessageFactory.newInstance().createMessage();
    SOAPPart sp = message.getSOAPPart();
    sp.setContent(new DOMSource(jdomDocument.getFirstChild()));

    return message;
}

From source file:com.bernardomg.example.swss.test.util.factory.SecureSoapMessages.java

private static final SOAPMessage toMessage(final Document jdomDocument) throws IOException, SOAPException {
    final SOAPMessage message = MessageFactory.newInstance().createMessage();
    final SOAPPart sp = message.getSOAPPart();
    sp.setContent(new DOMSource(jdomDocument.getFirstChild()));

    return message;
}

From source file:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

private static final Document toDocument(SOAPMessage soapMsg)
        throws TransformerConfigurationException, TransformerException, SOAPException, IOException {
    Source src = soapMsg.getSOAPPart().getContent();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMResult result = new DOMResult();
    transformer.transform(src, result);//from  w ww.  j av  a  2  s .co m
    return (Document) result.getNode();
}

From source file:com.bernardomg.example.swss.test.util.factory.SecureSoapMessages.java

private static final Document toDocument(final SOAPMessage soapMsg)
        throws TransformerConfigurationException, TransformerException, SOAPException, IOException {
    final Source src = soapMsg.getSOAPPart().getContent();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    final DOMResult result = new DOMResult();
    transformer.transform(src, result);//from   ww w.  j av a 2s . co  m
    return (Document) result.getNode();
}

From source file:eu.planets_project.tb.gui.backing.admin.wsclient.util.WSClient.java

/**
 * Invokes an operation using SAAJ/*from   w ww. j av  a 2 s. c o  m*/
 *
 * @param operation The operation to invoke
 */
public static String invokeOperation(OperationInfo operation) throws Exception {
    try {
        // Determine if the operation style is RPC
        boolean isRPC = false;
        if (operation.getStyle() != null)
            isRPC = operation.getStyle().equalsIgnoreCase("rpc");
        else
            ;

        // All connections are created by using a connection factory
        SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();

        // Now we can create a SOAPConnection object using the connection factory
        SOAPConnection connection = conFactory.createConnection();

        // All SOAP messages are created by using a message factory
        MessageFactory msgFactory = MessageFactory.newInstance();

        // Now we can create the SOAP message object
        SOAPMessage msg = msgFactory.createMessage();

        // Get the SOAP part from the SOAP message object
        SOAPPart soapPart = msg.getSOAPPart();

        // The SOAP part object will automatically contain the SOAP envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        //my extension - START
        //envelope.addNamespaceDeclaration("_ns1", operation.getNamespaceURI());
        //my extension - END

        if (isRPC) {
            // Add namespace declarations to the envelope, usually only required for RPC/encoded
            envelope.addNamespaceDeclaration(XSI_NAMESPACE_PREFIX, XSI_NAMESPACE_URI);
            envelope.addNamespaceDeclaration(XSD_NAMESPACE_PREFIX, XSD_NAMESPACE_URI);
        }

        // Get the SOAP header from the envelope
        SOAPHeader header = envelope.getHeader();

        // The client does not yet support SOAP headers
        header.detachNode();

        // Get the SOAP body from the envelope and populate it
        SOAPBody body = envelope.getBody();

        // Create the default namespace for the SOAP body
        //body.addNamespaceDeclaration("", operation.getNamespaceURI());

        // Add the service information
        String targetObjectURI = operation.getTargetObjectURI();

        if (targetObjectURI == null) {
            // The target object URI should not be null
            targetObjectURI = "";
        }

        // Add the service information         
        //Name svcInfo = envelope.createName(operation.getTargetMethodName(), "", targetObjectURI);
        Name svcInfo = envelope.createName(operation.getTargetMethodName(), "ns1", operation.getNamespaceURI());
        SOAPElement svcElem = body.addChildElement(svcInfo);

        if (isRPC) {
            // Set the encoding style of the service element
            svcElem.setEncodingStyle(operation.getEncodingStyle());
        }

        // Add the message contents to the SOAP body
        Document doc = XMLSupport.readXML(operation.getInputMessageText());

        if (doc.hasRootElement()) {
            // Begin building content
            buildSoapElement(envelope, svcElem, doc.getRootElement(), isRPC);
        }

        //svcElem.addTextNode(operation.getInputMessageText());
        //svcElem.

        // Check for a SOAPAction
        String soapActionURI = operation.getSoapActionURI();

        if (soapActionURI != null && soapActionURI.length() > 0) {
            // Add the SOAPAction value as a MIME header
            MimeHeaders mimeHeaders = msg.getMimeHeaders();
            mimeHeaders.setHeader("SOAPAction", "\"" + operation.getSoapActionURI() + "\"");
        }

        // Save changes to the message we just populated
        msg.saveChanges();

        // Get ready for the invocation
        URLEndpoint endpoint = new URLEndpoint(operation.getTargetURL());

        // Show the URL endpoint message in the log
        ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
        msg.writeTo(msgStream);

        log.debug("SOAP Message MeasurementTarget URL: " + endpoint.getURL());
        log.debug("SOAP Request: " + msgStream.toString());

        // Make the call
        SOAPMessage response = connection.call(msg, endpoint);

        // Close the connection, we are done with it
        connection.close();

        // Get the content of the SOAP response
        Source responseContent = response.getSOAPPart().getContent();

        // Convert the SOAP response into a JDOM
        TransformerFactory tFact = TransformerFactory.newInstance();
        Transformer transformer = tFact.newTransformer();

        JDOMResult jdomResult = new JDOMResult();
        transformer.transform(responseContent, jdomResult);

        // Get the document created by the transform operation
        Document responseDoc = jdomResult.getDocument();

        // Send the response to the Log
        String strResponse = XMLSupport.outputString(responseDoc);
        log.debug("SOAP Response from: " + operation.getTargetMethodName() + ": " + strResponse);

        // Set the response as the output message
        operation.setOutputMessageText(strResponse);

        // Return the response generated
        return strResponse;
    }

    catch (Throwable ex) {
        throw new Exception("Error invoking operation", ex);
    }

}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Adds base MIME headers to a {@link SOAPMessage}.
 *
 * @param message The {@link SOAPMessage} to add the headers to.
 *//*ww  w  . ja va2  s . c o  m*/
public static void addBaseMimeHeaders(SOAPMessage message) {
    SOAPUtil.addMimeHeader(message, "Content-Type", "multipart/related");
    SOAPUtil.addMimeHeader(message, "SOAPAction", "\"\"");
    SOAPUtil.addMimeHeader(message, "Accept",
            "application/soap+xml, application/mime, multipart/related, text/*");
    SOAPUtil.addMimeHeader(message, "Cache-Control", "no-cache");
    SOAPUtil.addMimeHeader(message, "Pragma", "no-cache");

    message.getSOAPPart().setMimeHeader("Content-Type", "text/xml; charset=UTF-8");
    message.getSOAPPart().setMimeHeader("Content-Transfer-Encoding", "8bit");
}