Search in sources :

Example 1 with ByteDataSource

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

the class ByteArrayType method createAttachment.

@Override
protected Attachment createAttachment(Object object, String id) {
    byte[] data = (byte[]) object;
    ByteDataSource source = new ByteDataSource(data);
    source.setContentType(getContentType(object, null));
    AttachmentImpl att = new AttachmentImpl(id, new DataHandler(source));
    att.setXOP(true);
    return att;
}
Also used : ByteDataSource(org.apache.cxf.attachment.ByteDataSource) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler)

Example 2 with ByteDataSource

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

the class SwAOutInterceptor method processAttachments.

protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
    Collection<Attachment> atts = setupAttachmentOutput(message);
    List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
    for (MessagePartInfo mpi : sbi.getAttachments()) {
        String partName = mpi.getConcreteName().getLocalPart();
        String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
        String id = new StringBuilder().append(partName).append("=").append(UUID.randomUUID()).append("@apache.org").toString();
        // this assumes things are in order...
        int idx = mpi.getIndex();
        Object o = outObjects.get(idx);
        if (o == null) {
            continue;
        }
        outObjects.set(idx, null);
        DataHandler dh = null;
        // This code could probably be refactored out somewhere...
        if (o instanceof Source) {
            dh = new DataHandler(createDataSource((Source) o, ct));
        } else if (o instanceof Image) {
            final Image img = (Image) o;
            final String contentType = ct;
            dh = new DataHandler(o, ct) {

                @Override
                public InputStream getInputStream() throws IOException {
                    LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
                    writeTo(bout);
                    return bout.createInputStream();
                }

                @Override
                public void writeTo(OutputStream out) throws IOException {
                    ImageWriter writer = null;
                    Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
                    if (writers.hasNext()) {
                        writer = writers.next();
                    }
                    if (writer != null) {
                        BufferedImage bimg = convertToBufferedImage(img);
                        ImageOutputStream iout = ImageIO.createImageOutputStream(out);
                        writer.setOutput(iout);
                        writer.write(bimg);
                        writer.dispose();
                        iout.flush();
                        out.flush();
                    }
                }
            };
        } else if (o instanceof DataHandler) {
            dh = (DataHandler) o;
            ct = dh.getContentType();
            try {
                if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
                    dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
                }
            } catch (IOException e) {
            // ignore, use same dh
            }
        } else if (o instanceof byte[]) {
            if (ct == null) {
                ct = "application/octet-stream";
            }
            dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
        } else if (o instanceof String) {
            if (ct == null) {
                ct = "text/plain; charset=\'UTF-8\'";
            }
            dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
        } else {
            throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
        }
        AttachmentImpl att = new AttachmentImpl(id);
        att.setDataHandler(dh);
        att.setHeader("Content-Type", ct);
        att.setHeader("Content-ID", "<" + id + ">");
        atts.add(att);
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ImageWriter(javax.imageio.ImageWriter) Attachment(org.apache.cxf.message.Attachment) Fault(org.apache.cxf.interceptor.Fault) DataHandler(javax.activation.DataHandler) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IOException(java.io.IOException) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Example 3 with ByteDataSource

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

the class MultipartProvider method createDataHandler.

private <T> Attachment createDataHandler(T obj, Class<T> cls, Type genericType, Annotation[] anns, String mimeType, String mainMediaType, int id) throws IOException {
    DataHandler dh = null;
    if (InputStream.class.isAssignableFrom(obj.getClass())) {
        dh = createInputStreamDH((InputStream) obj, mimeType);
    } else if (DataHandler.class.isAssignableFrom(obj.getClass())) {
        dh = (DataHandler) obj;
    } else if (DataSource.class.isAssignableFrom(obj.getClass())) {
        dh = new DataHandler((DataSource) obj);
    } else if (File.class.isAssignableFrom(obj.getClass())) {
        File f = (File) obj;
        ContentDisposition cd = mainMediaType.startsWith(MediaType.MULTIPART_FORM_DATA) ? new ContentDisposition("form-data;name=file;filename=" + f.getName()) : null;
        return new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, Files.newInputStream(f.toPath()), cd);
    } else if (Attachment.class.isAssignableFrom(obj.getClass())) {
        Attachment att = (Attachment) obj;
        if (att.getObject() == null) {
            return att;
        }
        dh = getHandlerForObject(att.getObject(), att.getObject().getClass(), new Annotation[] {}, att.getContentType().toString(), id);
        return new Attachment(att.getContentId(), dh, att.getHeaders());
    } else if (byte[].class.isAssignableFrom(obj.getClass())) {
        ByteDataSource source = new ByteDataSource((byte[]) obj);
        source.setContentType(mimeType);
        dh = new DataHandler(source);
    } else {
        dh = getHandlerForObject(obj, cls, genericType, anns, mimeType, id);
    }
    String contentId = getContentId(anns, id);
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.putSingle("Content-Type", mimeType);
    return new Attachment(contentId, dh, headers);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) File(java.io.File) Annotation(java.lang.annotation.Annotation) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource)

Example 4 with ByteDataSource

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

the class SwAOutInterceptor method createDataSource.

private DataSource createDataSource(Source o, String ct) {
    DataSource ds = null;
    if (o instanceof StreamSource) {
        StreamSource src = (StreamSource) o;
        try {
            if (src.getInputStream() != null) {
                try (ByteArrayOutputStream bos = new ByteArrayOutputStream(2048)) {
                    IOUtils.copy(src.getInputStream(), bos, 1024);
                    ds = new ByteDataSource(bos.toByteArray(), ct);
                }
            } else {
                ds = new ByteDataSource(IOUtils.toString(src.getReader()).getBytes(StandardCharsets.UTF_8), ct);
            }
        } catch (IOException e) {
            throw new Fault(e);
        }
    } else {
        ByteArrayOutputStream bwriter = new ByteArrayOutputStream();
        XMLStreamWriter writer = null;
        try {
            writer = StaxUtils.createXMLStreamWriter(bwriter);
            StaxUtils.copy(o, writer);
            writer.flush();
            ds = new ByteDataSource(bwriter.toByteArray(), ct);
        } catch (XMLStreamException e1) {
            throw new Fault(e1);
        } finally {
            StaxUtils.close(writer);
        }
    }
    return ds;
}
Also used : ByteDataSource(org.apache.cxf.attachment.ByteDataSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource)

Example 5 with ByteDataSource

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

the class ContextPropertiesMappingTest method testCreateWebServiceContextWithInAttachments.

@Test
public void testCreateWebServiceContextWithInAttachments() {
    Exchange exchange = new ExchangeImpl();
    Message inMessage = new MessageImpl();
    Collection<Attachment> attachments = new LinkedList<Attachment>();
    DataSource source = new ByteDataSource(new byte[0], "text/xml");
    DataHandler handler1 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part1", handler1));
    DataHandler handler2 = new DataHandler(source);
    attachments.add(new AttachmentImpl("part2", handler2));
    inMessage.setAttachments(attachments);
    inMessage.putAll(message);
    exchange.setInMessage(inMessage);
    exchange.setOutMessage(new MessageImpl());
    MessageContext ctx = new WrappedMessageContext(exchange.getInMessage(), Scope.APPLICATION);
    Object inAttachments = ctx.get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
    assertNotNull("inbound attachments object must be initialized", inAttachments);
    assertTrue("inbound attachments must be in a Map", inAttachments instanceof Map);
    Map<String, DataHandler> dataHandlers = CastUtils.cast((Map<?, ?>) inAttachments);
    assertEquals("two inbound attachments expected", 2, dataHandlers.size());
    assertTrue("part1 attachment is missing", dataHandlers.containsKey("part1"));
    // should do as it's the same instance
    assertTrue("part1 handler is missing", dataHandlers.get("part1") == handler1);
    assertTrue("part2 attachment is missing", dataHandlers.containsKey("part2"));
    assertTrue("part2 handler is missing", dataHandlers.get("part2") == handler2);
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) LinkedList(java.util.LinkedList) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) DataSource(javax.activation.DataSource) Exchange(org.apache.cxf.message.Exchange) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) HashMap(java.util.HashMap) Map(java.util.Map) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

ByteDataSource (org.apache.cxf.attachment.ByteDataSource)5 DataHandler (javax.activation.DataHandler)4 DataSource (javax.activation.DataSource)4 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 StreamSource (javax.xml.transform.stream.StreamSource)2 LoadingByteArrayOutputStream (org.apache.cxf.helpers.LoadingByteArrayOutputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 Attachment (org.apache.cxf.message.Attachment)2 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1