Java Reference
In-Depth Information
Accessing Inbound Attachment Data
Problem
You need to access an attachment to a SOAP message you are receiving.
Solution
Use the AttachmentPart.getContent method to retrieve all of the attachments from the
message, iterate over each one, and check their content type before processing.
Discussion
This is very straightforward. You just need to iterate over the result returned from invoking
SOAPMessage.getAttachments . The following servlet code receives a request from a client
that has passed it an attachment. You read the attachment from the SOAP message, and create
an attachment of your own. You then put the attachment onto the SOAP message to return
with the response to the client.
The following method is defined within a SAAJ provider servlet. This private method accepts
a SOAPMessage and calls its getAttachments method, which returns an attachment part. Then
you can call any of the available methods to check the content ID, check the content type, and
so on. In this case, you print out the contents of the attachment ID, the content type, and the
attachment data:
@SuppressWarnings("unchecked")
private void printReceivedAttachmentData(SOAPMessage msg)
throws SOAPException {
System.out.print("Getting attachment...");
Iterator<AttachmentPart> it = msg.getAttachments();
while (it.hasNext()) {
AttachmentPart attachment = it.next();
String id = attachment.getContentId();
//Check the ID, just to pretend some business logic
if ("clientVersion".equals(id)){
String type = attachment.getContentType();
System.out.print("Attachment ID=" + id +
". Content Type=" + type);
Search WWH ::




Custom Search