use of jakarta.xml.soap.AttachmentPart in project metro-jax-ws by eclipse-ee4j.
the class StreamAttachment method writeTo.
public void writeTo(SOAPMessage saaj) throws SOAPException {
AttachmentPart part = saaj.createAttachmentPart();
part.setRawContentBytes(data, 0, len, getContentType());
part.setContentId(contentId);
saaj.addAttachmentPart(part);
}
use of jakarta.xml.soap.AttachmentPart in project metro-jax-ws by eclipse-ee4j.
the class SAAJMessageTest method testMtomAttachmentCid.
public void testMtomAttachmentCid() throws Exception {
String testMtomMessageReload_01 = "multipart/related;type=\"application/xop+xml\";boundary=\"----=_Part_0_1145105632.1353005695468\";start=\"<cbe648b3-2055-413e-b8ed-877cdf0f2477>\";start-info=\"text/xml\"";
MessageContext m1 = mcf.createContext(getResource("testMtomMessageReload_01.msg"), testMtomMessageReload_01);
Packet packet = (Packet) m1;
Message riMsg = packet.getInternalMessage();
// This will cause all the attachments to be created ...
// Iterator<Attachment> as = packet.getInternalMessage().getAttachments().iterator();
// SAAJFactory:
SOAPVersion soapVersion = packet.getMessage().getSOAPVersion();
SOAPMessage saajMsg = soapVersion.getMessageFactory().createMessage();
SaajStaxWriterEx writer = new SaajStaxWriterEx(saajMsg, soapVersion.nsUri);
try {
riMsg.writeTo(writer);
} catch (XMLStreamException e) {
throw (e.getCause() instanceof SOAPException) ? (SOAPException) e.getCause() : new SOAPException(e);
}
saajMsg = writer.getSOAPMessage();
int counter = 0;
String hredCid = null;
for (Attachment a : riMsg.getAttachments()) {
hredCid = ((StreamingDataHandler) a.asDataHandler()).getHrefCid();
counter++;
}
assertTrue(writer.ma.size() == counter);
AttachmentPart ap = null;
// for (Iterator<AttachmentPart> itr = saajMsg.getAttachments(); itr.hasNext(); ) {
// System.out.println("\r\n itr.next().getContentId() " + itr.next().getContentId() );
// }
StreamingDataHandler sdh = (StreamingDataHandler) writer.ma.get(0);
assertEquals(hredCid, sdh.getHrefCid());
}
use of jakarta.xml.soap.AttachmentPart in project metro-jax-ws by eclipse-ee4j.
the class StreamMessageTest method testWriteMtomToStreamClientRequest.
public void testWriteMtomToStreamClientRequest() throws Exception {
// DISI may set the packet.state to State.ClientRequest
// ((Packet)mc).setState(State.ClientRequest)
InputStream is = null;
ByteArrayOutputStream baos = null;
ByteArrayInputStream bais = null;
try {
String ctype = "multipart/related; boundary=MIME_Boundary; " + "start=\"<ff2b0fb4-9bcf-4ff5-ba16-d3436c78c3fd>\"; " + "type=\"application/xop+xml\"; start-info=\"text/xml\"";
MessageContextFactory mcf = MessageContextFactory.createFactory(new MTOMFeature(true));
is = getClass().getClassLoader().getResourceAsStream("etc/testMtomMessage_osb_disi.msg");
MessageContext mc = mcf.createContext(is, ctype);
Packet packet = (Packet) mc;
Message message = packet.getInternalMessage();
assertTrue("StreamMessage not found, got : " + message.getClass(), StreamMessage.class.isAssignableFrom(message.getClass()));
baos = new ByteArrayOutputStream();
((Packet) mc).setState(State.ClientRequest);
mc.writeTo(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
MessageFactory mf = MessageFactory.newInstance();
MimeHeaders mh = new MimeHeaders();
mh.addHeader("Content-Type", ctype);
SOAPMessage sm = mf.createMessage(mh, bais);
assertEquals("wrong attachment count", 1, sm.countAttachments());
AttachmentPart ap = (AttachmentPart) sm.getAttachments().next();
assertEquals("wrong attachemnt Content-Id", "<testAttachmentContentId>", ap.getContentId());
NodeList nl = sm.getSOAPBody().getElementsByTagNameNS(MtomCodec.XOP_NAMESPACEURI, MtomCodec.XOP_LOCALNAME);
assertEquals(MtomCodec.XOP_NAMESPACEURI + ":" + MtomCodec.XOP_LOCALNAME + " not found", 1, nl.getLength());
Element elt = (Element) nl.item(0);
assertEquals("wrong href value", "cid:testAttachmentContentId", elt.getAttribute("href"));
} finally {
Exception e = null;
try {
if (is != null)
is.close();
} catch (IOException e1) {
e = e1;
}
try {
if (baos != null)
baos.close();
} catch (IOException e2) {
e = e2;
}
try {
if (bais != null)
bais.close();
} catch (Exception e3) {
e = e3;
}
if (e != null)
throw e;
}
}
use of jakarta.xml.soap.AttachmentPart in project metro-jax-ws by eclipse-ee4j.
the class StreamMessageTest method testWriteMtomToStream.
public void testWriteMtomToStream() throws Exception {
// Ensure writing StreamMessage to OutputputStream preserves CID and
// does not create duplicate attachment. We check this by re-parsing
// as a SOAP message and verifying the contents.
InputStream is = null;
ByteArrayOutputStream baos = null;
ByteArrayInputStream bais = null;
try {
String ctype = "multipart/related;type=\"application/xop+xml\";" + "boundary=\"----=_Part_0_1145105632.1353005695468\";" + "start=\"<cbe648b3-2055-413e-b8ed-877cdf0f2477>\";start-info=\"text/xml\"";
MessageContextFactory mcf = MessageContextFactory.createFactory(new MTOMFeature(true));
is = getClass().getClassLoader().getResourceAsStream("etc/testMtomMessageReload_01.msg");
MessageContext mc = mcf.createContext(is, ctype);
Packet packet = (Packet) mc;
Message message = packet.getInternalMessage();
assertTrue("StreamMessage not found, got : " + message.getClass(), StreamMessage.class.isAssignableFrom(message.getClass()));
baos = new ByteArrayOutputStream();
mc.writeTo(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
MessageFactory mf = MessageFactory.newInstance();
MimeHeaders mh = new MimeHeaders();
mh.addHeader("Content-Type", ctype);
SOAPMessage sm = mf.createMessage(mh, bais);
assertEquals("wrong attachment count", 1, sm.countAttachments());
AttachmentPart ap = (AttachmentPart) sm.getAttachments().next();
assertEquals("wrong attachemnt Content-Id", "<534475ae-bdab-4594-9f97-c09908bacfbd>", ap.getContentId());
NodeList nl = sm.getSOAPBody().getElementsByTagNameNS(MtomCodec.XOP_NAMESPACEURI, MtomCodec.XOP_LOCALNAME);
assertEquals(MtomCodec.XOP_NAMESPACEURI + ":" + MtomCodec.XOP_LOCALNAME + " not found", 1, nl.getLength());
Element elt = (Element) nl.item(0);
assertEquals("wrong href value", "cid:534475ae-bdab-4594-9f97-c09908bacfbd", elt.getAttribute("href"));
} finally {
Exception e = null;
try {
if (is != null)
is.close();
} catch (IOException e1) {
e = e1;
}
try {
if (baos != null)
baos.close();
} catch (IOException e2) {
e = e2;
}
try {
if (bais != null)
bais.close();
} catch (Exception e3) {
e = e3;
}
if (e != null)
throw e;
}
}
use of jakarta.xml.soap.AttachmentPart in project metro-jax-ws by eclipse-ee4j.
the class SAAJFactoryTest method assertCustomMimeHeadersOnAttachments.
private void assertCustomMimeHeadersOnAttachments(SOAPMessage msg) {
@SuppressWarnings("unchecked") Iterator<AttachmentPart> attachments = msg.getAttachments();
assertTrue(attachments.hasNext());
while (attachments.hasNext()) {
AttachmentPart part = attachments.next();
String[] hdr = part.getMimeHeader(CUSTOM_MIME_HEADER_NAME);
assertNotNull("Missing first custom MIME header", hdr);
assertEquals("Expected one header value", hdr.length, 1);
assertEquals("Wrong value for first header", hdr[0], CUSTOM_MIME_HEADER_VALUE);
hdr = part.getMimeHeader(CUSTOM_MIME_HEADER_NAME2);
assertNotNull("Missing second custom MIME header", hdr);
assertEquals("Expected one header value", hdr.length, 1);
assertEquals("Wrong value for second header", hdr[0], CUSTOM_MIME_HEADER_VALUE2);
assertNull("Unexpected header found", part.getMimeHeader("not found header"));
}
}
Aggregations