use of org.apache.cxf.message.Attachment in project cxf by apache.
the class WrappedAttachments method addAll.
public boolean addAll(Collection<? extends Attachment> c) {
boolean b = false;
for (Iterator<? extends Attachment> it = c.iterator(); it.hasNext(); ) {
Attachment o = it.next();
if (!attachments.containsKey(o.getId())) {
b = true;
attachments.put(o.getId(), o.getDataHandler());
cache.put(o.getId(), o);
}
}
return b;
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AbstractXOPType method writeObject.
@Override
public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException {
// add the content type attribute even if we are going to fall back.
String contentType = getContentType(object, context);
if (contentType != null && useXmimeBinaryType) {
MessageWriter ctWriter = writer.getAttributeWriter(XML_MIME_CONTENT_TYPE);
ctWriter.writeValue(contentType);
}
if (!context.isMtomEnabled()) {
fallbackDelegate.writeObject(getBytes(object), writer, context);
return;
}
Collection<Attachment> attachments = context.getAttachments();
if (attachments == null) {
attachments = new ArrayList<>();
context.setAttachments(attachments);
}
String id = AttachmentUtil.createContentID(getSchemaType().getNamespaceURI());
Attachment att = createAttachment(object, id);
attachments.add(att);
MessageWriter include = writer.getElementWriter(XOP_INCLUDE);
MessageWriter href = include.getAttributeWriter(XOP_HREF);
href.writeValue("cid:" + id);
include.close();
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class LazyAttachmentCollection method loadAll.
private void loadAll() {
try {
Attachment a = deserializer.readNext();
int count = 0;
while (a != null) {
attachments.add(a);
count++;
if (count > maxAttachmentCount) {
throw new IOException("The message contains more attachments than are permitted");
}
a = deserializer.readNext();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class LazyAttachmentCollection method iterator.
public Iterator<Attachment> iterator() {
return new Iterator<Attachment>() {
int current;
boolean removed;
public boolean hasNext() {
if (attachments.size() > current) {
return true;
}
// check if there is another attachment
try {
Attachment a = deserializer.readNext();
if (a == null) {
return false;
}
attachments.add(a);
return true;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Attachment next() {
Attachment a = attachments.get(current);
current++;
removed = false;
return a;
}
@Override
public void remove() {
if (removed) {
throw new IllegalStateException();
}
attachments.remove(--current);
removed = true;
}
};
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentDeserializerTest method testDeserializerMtomWithAxis2StyleBoundaries.
@Test
public void testDeserializerMtomWithAxis2StyleBoundaries() throws Exception {
InputStream is = getClass().getResourceAsStream("axis2_mimedata");
String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=MIMEBoundaryurn_uuid_6BC4984D5D38EB283C1177616488109";
msg.put(Message.CONTENT_TYPE, ct);
msg.setContent(InputStream.class, is);
AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
deserializer.initializeAttachments();
InputStream attBody = msg.getContent(InputStream.class);
assertTrue(attBody != is);
assertTrue(attBody instanceof DelegatingInputStream);
Collection<Attachment> atts = msg.getAttachments();
assertNotNull(atts);
Iterator<Attachment> itr = atts.iterator();
assertTrue(itr.hasNext());
Attachment a = itr.next();
assertNotNull(a);
InputStream attIs = a.getDataHandler().getInputStream();
// check the cached output stream
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
IOUtils.copy(attBody, out);
assertTrue(out.toString().startsWith("<env:Envelope"));
}
// try streaming a character off the wire
assertEquals(255, attIs.read());
assertEquals(216, attIs.read());
// Attachment invalid = atts.get("INVALID");
// assertNull(invalid.getDataHandler().getInputStream());
//
// assertTrue(attIs instanceof ByteArrayInputStream);
is.close();
}
Aggregations