Search in sources :

Example 1 with AttachmentDeserializer

use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.

the class MtomServerTest method testURLBasedAttachment.

@Test
public void testURLBasedAttachment() throws Exception {
    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceBean(new EchoService());
    sf.setBus(getStaticBus());
    String address = "http://localhost:" + PORT2 + "/EchoService";
    sf.setAddress(address);
    Map<String, Object> props = new HashMap<>();
    props.put(Message.MTOM_ENABLED, "true");
    sf.setProperties(props);
    Server server = sf.create();
    server.getEndpoint().getService().getDataBinding().setMtomThreshold(0);
    servStatic(getClass().getResource("mtom-policy.xml"), "http://localhost:" + PORT2 + "/policy.xsd");
    EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
    ei.setAddress(address);
    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());
    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);
    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);
    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request-url-attachment");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }
    try (ByteArrayOutputStream bout = new ByteArrayOutputStream()) {
        IOUtils.copy(is, bout);
        String s = bout.toString(StandardCharsets.UTF_8.name());
        s = s.replaceAll(":9036/", ":" + PORT2 + "/");
        os.write(s.getBytes(StandardCharsets.UTF_8));
    }
    os.flush();
    is.close();
    os.close();
    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();
    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());
    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertTrue("Wrong size: " + out.size() + "\n" + out.toString(), out.size() > 970 && out.size() < 1020);
    }
    unregisterServStatic("http://localhost:" + PORT2 + "/policy.xsd");
}
Also used : Server(org.apache.cxf.endpoint.Server) Message(org.apache.cxf.message.Message) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Attachment(org.apache.cxf.message.Attachment) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ConduitInitiator(org.apache.cxf.transport.ConduitInitiator) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TestUtilities(org.apache.cxf.test.TestUtilities) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Conduit(org.apache.cxf.transport.Conduit) ConduitInitiatorManager(org.apache.cxf.transport.ConduitInitiatorManager) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 2 with AttachmentDeserializer

use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.

the class MtomPolicyTest method sendMtomMessage.

private void sendMtomMessage(String a) throws Exception {
    EndpointInfo ei = new EndpointInfo(null, "http://schemas.xmlsoap.org/wsdl/http");
    ei.setAddress(a);
    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());
    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);
    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);
    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }
    IOUtils.copy(is, os);
    os.flush();
    is.close();
    os.close();
    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();
    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());
    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertEquals(27364, out.size());
    }
}
Also used : Message(org.apache.cxf.message.Message) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TestUtilities(org.apache.cxf.test.TestUtilities) Attachment(org.apache.cxf.message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) Conduit(org.apache.cxf.transport.Conduit) ConduitInitiatorManager(org.apache.cxf.transport.ConduitInitiatorManager) ConduitInitiator(org.apache.cxf.transport.ConduitInitiator) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 3 with AttachmentDeserializer

use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.

the class MessageModeOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop != null && !bindingName.equals(bop.getBinding().getName())) {
        return;
    }
    if (saajOut != null) {
        doSoap(message);
    } else if (DataSource.class.isAssignableFrom(type)) {
        // datasource stuff, must check if multi-source
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        DataSource ds = (DataSource) list.get(0);
        String ct = ds.getContentType();
        if (ct.toLowerCase().contains("multipart/related")) {
            Message msg = new MessageImpl();
            msg.setExchange(message.getExchange());
            msg.put(Message.CONTENT_TYPE, ct);
            try {
                msg.setContent(InputStream.class, ds.getInputStream());
                AttachmentDeserializer deser = new AttachmentDeserializer(msg);
                deser.initializeAttachments();
            } catch (IOException ex) {
                throw new Fault(ex);
            }
            message.setAttachments(msg.getAttachments());
            final InputStream in = msg.getContent(InputStream.class);
            final String ct2 = (String) msg.get(Message.CONTENT_TYPE);
            list.set(0, new DataSource() {

                public String getContentType() {
                    return ct2;
                }

                public InputStream getInputStream() throws IOException {
                    return in;
                }

                public String getName() {
                    return ct2;
                }

                public OutputStream getOutputStream() throws IOException {
                    return null;
                }
            });
        } else if (!ct.toLowerCase().contains("xml")) {
            // not XML based, need to stream out directly.  This is a bit tricky as
            // we don't want the stax stuff triggering and such
            OutputStream out = message.getContent(OutputStream.class);
            message.put(Message.CONTENT_TYPE, ct);
            try {
                InputStream in = ds.getInputStream();
                IOUtils.copy(in, out);
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new Fault(e);
            }
            list.remove(0);
            out = new CachedOutputStream();
            message.setContent(OutputStream.class, out);
            XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
            message.setContent(XMLStreamWriter.class, writer);
        }
    } else if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message) && Source.class.isAssignableFrom(type)) {
        // if schema validation is on, we'll end up converting to a DOMSource anyway,
        // let's convert and check for a fault
        MessageContentsList list = (MessageContentsList) message.getContent(List.class);
        Source ds = (Source) list.get(0);
        if (!(ds instanceof DOMSource)) {
            try {
                ds = new DOMSource(StaxUtils.read(ds));
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
            list.set(0, ds);
            validatePossibleFault(message, bop, ((DOMSource) ds).getNode());
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) DataSource(javax.activation.DataSource) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 4 with AttachmentDeserializer

use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.

the class PersistenceUtils method decodeRMContent.

public static void decodeRMContent(RMMessage rmmsg, Message msg) throws IOException {
    String contentType = rmmsg.getContentType();
    final CachedOutputStream cos = rmmsg.getContent();
    if ((null != contentType) && contentType.startsWith("multipart/related")) {
        final InputStream is = cos.getInputStream();
        msg.put(Message.CONTENT_TYPE, contentType);
        msg.setContent(InputStream.class, is);
        AttachmentDeserializer ad = new AttachmentDeserializer(msg);
        ad.initializeAttachments();
        // create new cos with soap envelope only
        CachedOutputStream cosSoap = new CachedOutputStream();
        IOUtils.copy(msg.getContent(InputStream.class), cosSoap);
        cosSoap.flush();
        msg.put(RMMessageConstants.SAVED_CONTENT, cosSoap);
        // REVISIT -- At the moment references must be hold for retransmission
        // and the final cleanup of the CachedOutputStream.
        msg.put(RMMessageConstants.ATTACHMENTS_CLOSEABLE, new Closeable() {

            @Override
            public void close() throws IOException {
                try {
                    is.close();
                } catch (IOException e) {
                // Ignore
                }
                try {
                    cos.close();
                } catch (IOException e) {
                // Ignore
                }
            }
        });
    } else {
        msg.put(RMMessageConstants.SAVED_CONTENT, cos);
    }
}
Also used : AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) InputStream(java.io.InputStream) Closeable(java.io.Closeable) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 5 with AttachmentDeserializer

use of org.apache.cxf.attachment.AttachmentDeserializer in project cxf by apache.

the class MtomServerTest method testMtomRequest.

@Test
public void testMtomRequest() throws Exception {
    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceBean(new EchoService());
    sf.setBus(getStaticBus());
    String address = "http://localhost:" + PORT1 + "/EchoService";
    sf.setAddress(address);
    Map<String, Object> props = new HashMap<>();
    props.put(Message.MTOM_ENABLED, "true");
    sf.setProperties(props);
    sf.create();
    EndpointInfo ei = new EndpointInfo(null, HTTP_ID);
    ei.setAddress(address);
    ConduitInitiatorManager conduitMgr = getStaticBus().getExtension(ConduitInitiatorManager.class);
    ConduitInitiator conduitInit = conduitMgr.getConduitInitiator("http://schemas.xmlsoap.org/soap/http");
    Conduit conduit = conduitInit.getConduit(ei, getStaticBus());
    TestUtilities.TestMessageObserver obs = new TestUtilities.TestMessageObserver();
    conduit.setMessageObserver(obs);
    Message m = new MessageImpl();
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    m.put(Message.CONTENT_TYPE, ct);
    conduit.prepare(m);
    OutputStream os = m.getContent(OutputStream.class);
    InputStream is = testUtilities.getResourceAsStream("request");
    if (is == null) {
        throw new RuntimeException("Could not find resource " + "request");
    }
    IOUtils.copy(is, os);
    os.flush();
    is.close();
    os.close();
    byte[] res = obs.getResponseStream().toByteArray();
    MessageImpl resMsg = new MessageImpl();
    resMsg.setContent(InputStream.class, new ByteArrayInputStream(res));
    resMsg.put(Message.CONTENT_TYPE, obs.getResponseContentType());
    resMsg.setExchange(new ExchangeImpl());
    AttachmentDeserializer deserializer = new AttachmentDeserializer(resMsg);
    deserializer.initializeAttachments();
    Collection<Attachment> attachments = resMsg.getAttachments();
    assertNotNull(attachments);
    assertEquals(1, attachments.size());
    Attachment inAtt = attachments.iterator().next();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(inAtt.getDataHandler().getInputStream(), out);
        assertEquals(27364, out.size());
    }
}
Also used : Message(org.apache.cxf.message.Message) AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Attachment(org.apache.cxf.message.Attachment) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ConduitInitiator(org.apache.cxf.transport.ConduitInitiator) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TestUtilities(org.apache.cxf.test.TestUtilities) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Conduit(org.apache.cxf.transport.Conduit) ConduitInitiatorManager(org.apache.cxf.transport.ConduitInitiatorManager) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)5 AttachmentDeserializer (org.apache.cxf.attachment.AttachmentDeserializer)5 OutputStream (java.io.OutputStream)4 Message (org.apache.cxf.message.Message)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Attachment (org.apache.cxf.message.Attachment)3 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)3 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)3 TestUtilities (org.apache.cxf.test.TestUtilities)3 Conduit (org.apache.cxf.transport.Conduit)3 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)3 ConduitInitiatorManager (org.apache.cxf.transport.ConduitInitiatorManager)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 JaxWsServerFactoryBean (org.apache.cxf.jaxws.JaxWsServerFactoryBean)2 Test (org.junit.Test)2 Closeable (java.io.Closeable)1