//create the SOAPPart
createSOAPPart(request);
//Attach a text/plain object with the SOAP request
String sampleMessage = "Sample Message: Hello World!";
AttachmentPart textAttach = request.createAttachmentPart(sampleMessage, "text/plain");
textAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
textAttach.setContentId("submitSampleText@apache.org");
request.addAttachmentPart(textAttach);
//Attach a java.awt.Image object to the SOAP request
String jpgfilename = System.getProperty("basedir", ".") + "/" + "test-resources/axis2.jpg";
File myfile = new File(jpgfilename);
FileDataSource fds = new FileDataSource(myfile);
DataHandler imageDH = new DataHandler(fds);
AttachmentPart jpegAttach = request.createAttachmentPart(imageDH);
jpegAttach.addMimeHeader("Content-Transfer-Encoding", "binary");
jpegAttach.setContentId("submitSampleImage@apache.org");
jpegAttach.setContentType("image/jpg");
request.addAttachmentPart(jpegAttach);
SOAPConnection sCon = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = sCon.call(request, getAddress());
int attachmentCount = response.countAttachments();
assertTrue(attachmentCount == 2);
Iterator attachIter = response.getAttachments();
int i = 0;
while (attachIter.hasNext()) {
AttachmentPart attachment = (AttachmentPart)attachIter.next();
final Object content = attachment.getDataHandler().getContent();
if (content instanceof String) {
assertEquals(sampleMessage, (String)content);
} else if (content instanceof ByteArrayInputStream) {
ByteArrayInputStream bais = (ByteArrayInputStream)content;
byte[] b = new byte[15000];
final int lengthRead = bais.read(b);
FileOutputStream fos =
new FileOutputStream(new File(System.getProperty("basedir", ".") + "/" +
"target/test-resources/result" + (i++) + ".jpg"));
fos.write(b, 0, lengthRead);
fos.flush();
fos.close();
assertTrue(attachment.getContentType().equals("image/jpeg")
|| attachment.getContentType().equals("text/plain"));
}
}
sCon.close();