Search in sources :

Example 31 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class TestMtomProviderImpl method invoke.

public SOAPMessage invoke(final SOAPMessage request) {
    try {
        System.out.println("=== Received client request ===");
        // create the SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = part.getEnvelope();
        SOAPBody body = envelope.getBody();
        SOAPBodyElement testResponse = body.addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
        SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
        name.setTextContent("return detail + call detail");
        SOAPElement attachinfo = testResponse.addChildElement("attachinfo", null, "http://cxf.apache.org/mime/types");
        SOAPElement include = attachinfo.addChildElement("Include", "xop", "http://www.w3.org/2004/08/xop/include");
        int fileSize = 0;
        try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
            for (int i = pre.read(); i != -1; i = pre.read()) {
                fileSize++;
            }
        }
        int count = 50;
        byte[] data = new byte[fileSize * count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
        }
        DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
        // create the image attachment
        AttachmentPart attachment = message.createAttachmentPart(dh);
        attachment.setContentId("mtom_xop.wsdl");
        message.addAttachmentPart(attachment);
        System.out.println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());
        // add the reference to the image attachment
        include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());
        return message;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPBody(javax.xml.soap.SOAPBody) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SOAPBodyElement(javax.xml.soap.SOAPBodyElement)

Example 32 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class AbstractSecurityTest method getSoapMessageForDom.

protected SoapMessage getSoapMessageForDom(Document doc, String protocol) throws Exception {
    SOAPMessage saajMsg = MessageFactory.newInstance(protocol).createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    SAAJStreamWriter writer = new SAAJStreamWriter(part);
    StaxUtils.copy(doc, writer);
    saajMsg.saveChanges();
    MessageImpl message = new MessageImpl();
    SoapMessage msg = new SoapMessage(message);
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    msg.setContent(SOAPMessage.class, saajMsg);
    return msg;
}
Also used : Exchange(org.apache.cxf.message.Exchange) SOAPPart(javax.xml.soap.SOAPPart) SOAPMessage(javax.xml.soap.SOAPMessage) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) SoapMessage(org.apache.cxf.binding.soap.SoapMessage)

Example 33 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class SAAJOutInterceptor method handleMessage.

public void handleMessage(SoapMessage message) throws Fault {
    SOAPMessage saaj = message.getContent(SOAPMessage.class);
    try {
        if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
            // creating an empty SOAPHeader at this point in the
            // pre-existing SOAPMessage avoids the <soap:body> and
            // <soap:header> appearing in reverse order when the envolope
            // is written to the wire
            // 
            saaj.getSOAPPart().getEnvelope().addHeader();
        }
    } catch (SOAPException e) {
        throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
    }
    if (saaj == null) {
        SoapVersion version = message.getVersion();
        try {
            MessageFactory factory = getFactory(message);
            SOAPMessage soapMessage = factory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
            if (origWriter == null) {
                origWriter = message.getContent(XMLStreamWriter.class);
            }
            message.put(ORIGINAL_XML_WRITER, origWriter);
            W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
            // Replace stax writer with DomStreamWriter
            message.setContent(XMLStreamWriter.class, writer);
            message.setContent(SOAPMessage.class, soapMessage);
            message.setContent(Node.class, soapMessage.getSOAPPart());
        } catch (SOAPException e) {
            throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
        }
    } else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
        // as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
        // anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
        XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
        message.put(ORIGINAL_XML_WRITER, origWriter);
        XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {

            public void write(int b) throws IOException {
            }

            public void write(byte[] b, int off, int len) throws IOException {
            }
        });
        message.setContent(XMLStreamWriter.class, dummyWriter);
    }
    // Add a final interceptor to write the message
    message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) MessageFactory(javax.xml.soap.MessageFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPException(javax.xml.soap.SOAPException) OutputStream(java.io.OutputStream) SOAPPart(javax.xml.soap.SOAPPart) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 34 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class ParseBodyTest method testReadSOAPFault.

@Test
public void testReadSOAPFault() throws Exception {
    InputStream inStream = getClass().getResourceAsStream("soap12-fault.xml");
    Document doc = StaxUtils.read(inStream);
    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    SOAPMessage saajMsg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    SAAJStreamWriter writer = new SAAJStreamWriter(part);
    StaxUtils.copy(doc, writer);
    // Source s = new StaxSource(StaxUtils.createXMLStreamReader(doc));
    // part.setContent(s);
    saajMsg.saveChanges();
    msg.setContent(SOAPMessage.class, saajMsg);
    doc = part;
    // System.out.println("OUTPUT: " + StaxUtils.toString(doc));
    byte[] docbytes = getMessageBytes(doc);
    // System.out.println("OUTPUT: " + new String(docbytes));
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    StaxUtils.read(db, reader, false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) NullResolver(org.apache.cxf.helpers.DOMUtils.NullResolver) Exchange(org.apache.cxf.message.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SOAPPart(javax.xml.soap.SOAPPart) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 35 with SOAPPart

use of javax.xml.soap.SOAPPart in project cxf by apache.

the class TestMustUnderstandHandler method handleMessage.

public boolean handleMessage(SOAPMessageContext ctx) {
    boolean continueProcessing = true;
    try {
        Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        boolean outbound = (Boolean) b;
        SOAPMessage msg = ctx.getMessage();
        if (isServerSideHandler()) {
            if (outbound) {
                QName qname = new QName("http://cxf.apache.org/mu", "MU");
                SOAPPart soapPart = msg.getSOAPPart();
                SOAPEnvelope envelope = soapPart.getEnvelope();
                SOAPHeader header = envelope.getHeader();
                if (header == null) {
                    header = envelope.addHeader();
                }
                SOAPHeaderElement headerElement = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));
                // QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
                // "mustUnderstand");
                Name name = SOAPFactory.newInstance().createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
                headerElement.addAttribute(name, "1");
            } else {
                getHandlerInfoList(ctx).add(getHandlerId());
            }
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return continueProcessing;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Aggregations

SOAPPart (javax.xml.soap.SOAPPart)45 SOAPMessage (javax.xml.soap.SOAPMessage)30 SOAPBody (javax.xml.soap.SOAPBody)28 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)26 SOAPException (javax.xml.soap.SOAPException)26 MessageFactory (javax.xml.soap.MessageFactory)17 SOAPElement (javax.xml.soap.SOAPElement)14 Iterator (java.util.Iterator)13 Element (org.w3c.dom.Element)13 StreamSource (javax.xml.transform.stream.StreamSource)12 QName (javax.xml.namespace.QName)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 Name (javax.xml.soap.Name)10 NodeList (org.w3c.dom.NodeList)9 BufferedWriter (java.io.BufferedWriter)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 SOAPHeader (javax.xml.soap.SOAPHeader)6