Search in sources :

Example 1 with XMLMessage

use of org.apache.cxf.message.XMLMessage in project cxf by apache.

the class AttachmentDeserializerTest method imitateAttachmentInInterceptorForMessageWithMissingBoundary.

@Test
public void imitateAttachmentInInterceptorForMessageWithMissingBoundary() throws Exception {
    String contentType = "multipart/mixed;boundary=abc123";
    String data = "--abc123\r\n\r\n<Document></Document>\r\n\r\n";
    ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes());
    Message message = new XMLMessage(new MessageImpl());
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, inputStream);
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System.getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(AttachmentDeserializer.THRESHOLD));
    AttachmentDeserializer ad = new AttachmentDeserializer(message, Collections.singletonList("multipart/mixed"));
    ad.initializeAttachments();
    assertEquals(0, message.getAttachments().size());
    inputStream.close();
}
Also used : XMLMessage(org.apache.cxf.message.XMLMessage) Message(org.apache.cxf.message.Message) XMLMessage(org.apache.cxf.message.XMLMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 2 with XMLMessage

use of org.apache.cxf.message.XMLMessage in project cxf by apache.

the class XMLBinding method createMessage.

public Message createMessage(Message m) {
    if (!m.containsKey(Message.CONTENT_TYPE)) {
        String ct = null;
        // Should this be done in ServiceInvokerInterceptor to support a case where the
        // response content type is detected early on the inbound chain for all the bindings ?
        Exchange exchange = m.getExchange();
        if (exchange != null) {
            ct = (String) exchange.get(Message.CONTENT_TYPE);
        }
        if (ct == null) {
            ct = "text/xml";
        }
        m.put(Message.CONTENT_TYPE, ct);
    }
    return new XMLMessage(m);
}
Also used : Exchange(org.apache.cxf.message.Exchange) XMLMessage(org.apache.cxf.message.XMLMessage)

Example 3 with XMLMessage

use of org.apache.cxf.message.XMLMessage in project cxf by apache.

the class LogicalMessageImpl method handleDispatchProviderCase.

private Source handleDispatchProviderCase(Service.Mode mode) {
    Source source = null;
    Message message = msgContext.getWrappedMessage();
    Source obj = message.getContent(Source.class);
    if (message instanceof SoapMessage) {
        // StreamSource may only be used once, need to make a copy
        if (obj instanceof StreamSource) {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                obj = new StreamSource(cos.getInputStream());
                message.setContent(Source.class, new StreamSource(cos.getInputStream()));
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
        if (mode == Service.Mode.PAYLOAD) {
            source = obj;
        } else {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                InputStream in = cos.getInputStream();
                SOAPMessage msg = initSOAPMessage(in);
                source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild());
                in.close();
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
    } else if (message instanceof XMLMessage) {
        if (obj != null) {
            source = obj;
        } else if (message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    }
    return source;
}
Also used : XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) DataSource(javax.activation.DataSource)

Example 4 with XMLMessage

use of org.apache.cxf.message.XMLMessage in project cxf by apache.

the class LogicalMessageImpl method setPayload.

public void setPayload(Source s) {
    Message message = msgContext.getWrappedMessage();
    Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
    SOAPMessage m = message.getContent(SOAPMessage.class);
    if (m != null && !MessageUtils.isOutbound(message)) {
        try {
            SAAJUtils.getBody(m).removeContents();
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
            StaxUtils.copy(s, writer);
            writer.flush();
            writer.close();
            if (mode == Service.Mode.MESSAGE) {
                s = new DOMSource(m.getSOAPPart());
            } else {
                s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
            }
            W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
            message.setContent(XMLStreamReader.class, r);
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (mode != null) {
        if (message instanceof SoapMessage) {
            if (mode == Service.Mode.MESSAGE) {
                try {
                    // REVISIT: should try to use the original SOAPMessage
                    // instead of creating a new empty one.
                    SOAPMessage msg = initSOAPMessage(null);
                    write(s, SAAJUtils.getBody(msg));
                    s = new DOMSource(msg.getSOAPPart());
                } catch (Exception e) {
                    throw new Fault(e);
                }
            }
        } else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    } else {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
        msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
    }
    msgContext.getWrappedMessage().setContent(Source.class, s);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) Service(javax.xml.ws.Service) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Aggregations

XMLMessage (org.apache.cxf.message.XMLMessage)4 Message (org.apache.cxf.message.Message)3 IOException (java.io.IOException)2 JAXBException (javax.xml.bind.JAXBException)2 SOAPException (javax.xml.soap.SOAPException)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 LogicalMessage (javax.xml.ws.LogicalMessage)2 WebServiceException (javax.xml.ws.WebServiceException)2 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)2 Fault (org.apache.cxf.interceptor.Fault)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 DataSource (javax.activation.DataSource)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Service (javax.xml.ws.Service)1 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)1