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;
}
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);
}
}
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);
}
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;
}
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);
}
Aggregations