Search in sources :

Example 1 with SOAPPartImpl

use of com.sun.xml.messaging.saaj.soap.SOAPPartImpl in project tdi-studio-se by Talend.

the class SOAPUtil method extractContentAsDocument.

/**
	 * invoke soap and return the response document
	 */
public String extractContentAsDocument(String version, String destination, String soapAction, String soapMessage) throws SOAPException, TransformerException, ParserConfigurationException, FileNotFoundException, UnsupportedEncodingException {
    MessageFactory messageFactory = null;
    if (version.equals(SOAP12)) {
        messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    } else {
        messageFactory = MessageFactory.newInstance();
    }
    SOAPMessage message = messageFactory.createMessage();
    MimeHeaders mimeHeaders = message.getMimeHeaders();
    mimeHeaders.setHeader("SOAPAction", soapAction);
    SOAPPart soapPart = message.getSOAPPart();
    String encoding = getEncoding(soapMessage);
    ByteArrayInputStream stream = new ByteArrayInputStream(soapMessage.getBytes(encoding));
    StreamSource preppedMsgSrc = new StreamSource(stream);
    soapPart.setContent(preppedMsgSrc);
    message.saveChanges();
    SOAPMessage reply = connection.call(message, destination);
    SOAPPart reSoapPart = reply.getSOAPPart();
    if (reSoapPart != null && reSoapPart instanceof SOAPPartImpl) {
        ((SOAPPartImpl) reSoapPart).setSourceCharsetEncoding(encoding);
    }
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(reSoapPart.getContent(), new StreamResult(bos));
        return bos.toString(encoding);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SOAPPartImpl(com.sun.xml.messaging.saaj.soap.SOAPPartImpl) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) MessageFactory(javax.xml.soap.MessageFactory) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) TransformerException(javax.xml.transform.TransformerException) SOAPException(javax.xml.soap.SOAPException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MimeHeaders(javax.xml.soap.MimeHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPPart(javax.xml.soap.SOAPPart)

Example 2 with SOAPPartImpl

use of com.sun.xml.messaging.saaj.soap.SOAPPartImpl in project tdi-studio-se by Talend.

the class SOAPUtil method invokeSOAP.

public void invokeSOAP(String version, String destination, String soapAction, String soapMessage) throws SOAPException, TransformerException, ParserConfigurationException, FileNotFoundException, UnsupportedEncodingException {
    MessageFactory messageFactory = null;
    if (version.equals(SOAP12)) {
        messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    } else {
        messageFactory = MessageFactory.newInstance();
    }
    SOAPMessage message = messageFactory.createMessage();
    MimeHeaders mimeHeaders = message.getMimeHeaders();
    mimeHeaders.setHeader("SOAPAction", soapAction);
    if (basicAuth) {
        addBasicAuthHeader(mimeHeaders, username, password);
    }
    // Create objects for the message parts
    SOAPPart soapPart = message.getSOAPPart();
    String encoding = getEncoding(soapMessage);
    ByteArrayInputStream stream = new ByteArrayInputStream(soapMessage.getBytes(encoding));
    StreamSource preppedMsgSrc = new StreamSource(stream);
    soapPart.setContent(preppedMsgSrc);
    // InputStream stream = new FileInputStream(new File("d://soap.txt"));
    // StreamSource preppedMsgSrc = new StreamSource(stream);
    // soapPart.setContent(preppedMsgSrc);
    message.saveChanges();
    // Send the message
    SOAPMessage reply = connection.call(message, destination);
    SOAPPart reSoapPart = reply.getSOAPPart();
    if (reSoapPart != null && reSoapPart instanceof SOAPPartImpl) {
        ((SOAPPartImpl) reSoapPart).setSourceCharsetEncoding(encoding);
    }
    SOAPEnvelope reEnvelope = reSoapPart.getEnvelope();
    SOAPHeader reHeader = reEnvelope.getHeader();
    if (reHeader != null) {
        setReHeaderMessage(Doc2StringWithoutDeclare(extractContentAsDocument(reHeader)));
    }
    SOAPBody reBody = reEnvelope.getBody();
    if (reBody.getFault() != null) {
        hasFault = true;
        setReFaultMessage(Doc2StringWithoutDeclare(extractContentAsDocument(reBody)));
        setReBodyMessage(null);
    } else {
        hasFault = false;
        if (reBody.getChildNodes().getLength() < 1) {
            setReBodyMessage(null);
        } else if (reBody.getChildNodes().getLength() == 1 && reBody.getChildNodes().item(0) instanceof javax.xml.soap.Text) {
            setReBodyMessage(null);
        } else {
            setReBodyMessage(Doc2StringWithoutDeclare(extractContentAsDocument(reBody)));
        }
        setReFaultMessage(null);
    }
}
Also used : SOAPPartImpl(com.sun.xml.messaging.saaj.soap.SOAPPartImpl) MessageFactory(javax.xml.soap.MessageFactory) StreamSource(javax.xml.transform.stream.StreamSource) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) MimeHeaders(javax.xml.soap.MimeHeaders) SOAPBody(javax.xml.soap.SOAPBody) ByteArrayInputStream(java.io.ByteArrayInputStream) SOAPPart(javax.xml.soap.SOAPPart) SOAPHeader(javax.xml.soap.SOAPHeader)

Aggregations

SOAPPartImpl (com.sun.xml.messaging.saaj.soap.SOAPPartImpl)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MessageFactory (javax.xml.soap.MessageFactory)2 MimeHeaders (javax.xml.soap.MimeHeaders)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 SOAPPart (javax.xml.soap.SOAPPart)2 StreamSource (javax.xml.transform.stream.StreamSource)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SOAPBody (javax.xml.soap.SOAPBody)1 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)1 SOAPException (javax.xml.soap.SOAPException)1 SOAPHeader (javax.xml.soap.SOAPHeader)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 StreamResult (javax.xml.transform.stream.StreamResult)1