background image

AttachmentPart Object

<< Adding Attachments | Accessing an AttachmentPart Object >>
<< Adding Attachments | Accessing an AttachmentPart Object >>

AttachmentPart Object

automatically has a Content-Type header with the value "text/xml" because the content must
be in XML. In contrast, the type of content in an AttachmentPart object must be specified
because it can be any type.
Each AttachmentPart object has one or more MIME headers associated with it. When you
specify a type to the setContent method, that type is used for the header Content-Type. Note
that Content-Type is the only header that is required. You may set other optional headers, such
as Content-Id and Content-Location. For convenience, SAAJ provides get and set methods
for the headers Content-Type, Content-Id, and Content-Location. These headers can be
helpful in accessing a particular attachment when a message has multiple attachments. For
example, to access the attachments that have particular headers, you can call the SOAPMessage
method getAttachments and pass it a MIMEHeaders object containing the MIME headers you
are interested in.
The following code fragment shows one of the ways to use the method setContent. The Java
Object
in the first parameter can be a String, a stream, a javax.xml.transform.Source
object, or a javax.activation.DataHandler object. The Java Object being added in the
following code fragment is a String, which is plain text, so the second argument must be
"text/plain". The code also sets a content identifier, which can be used to identify this
AttachmentPart
object. After you have added content to attachment, you must add it to the
SOAPMessage
object, something that is done in the last line.
String stringContent =
"Update address for Sunny Skies " +
"Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
attachment.setContent(stringContent,
"text/plain");
attachment.setContentId(
"update_address");
message.addAttachmentPart(attachment);
The attachment variable now represents an AttachmentPart object that contains the string
stringContent
and has a header that contains the string text/plain. It also has a Content-Id
header with update_address as its value. And attachment is now part of message.
The other two SOAPMessage.createAttachment methods create an AttachmentPart object
complete with content. One is very similar to the AttachmentPart.setContent method in that
it takes the same parameters and does essentially the same thing. It takes a Java Object
containing the content and a String giving the content type. As with
AttachmentPart.setContent
, the Object can be a String, a stream, a
javax.xml.transform.Source
object, or a javax.activation.DataHandler object.
The other method for creating an AttachmentPart object with content takes a DataHandler
object, which is part of the JavaBeans Activation Framework (JAF). Using a DataHandler object
is fairly straightforward. First, you create a java.net.URL object for the file you want to add as
content. Then you create a DataHandler object initialized with the URL object:
SAAJ Tutorial
The Java EE 5 Tutorial · September 2007
602