Here's the code:
import javax.xml.soap.*;
public class Test {
public static void main(String[] args) throws Exception {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
AttachmentPart attachment = message.createAttachmentPart();
String stringContent = "Update address for Sunny Skies ";
stringContent += "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
attachment.setContent(stringContent, "text/plain");
attachment.setContentId("update_address");
message.addAttachmentPart(attachment);
message.writeTo(System.out);
}
}
Here's the output under JDK6:
------=_Part_0_4875744.1173067363234
Content-Type: text/xml; charset=utf-8
------=_Part_0_4875744.1173067363234
Content-Type: text/plain
Content-ID: update_address
Update address for Sunny Skies Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439
See the "Content-ID" in the attachment's mime header? Let's see what the latest and greatest SAAJ 1.3 spec says about it. I downloaded the "Download Specification With Change Bars" and "Download Documentation (JavaDocs)" from the following url: http://java.sun.com/xml/downloads/saaj.html In the PDF, for the API for AttachmentPart class, there are 2 methods getContentId and setContentId (See pages 25, 28, 32, 158, 160, 162). In all the references it is clear that the set/get should work with "Content-Id" and not "Content-ID". But guess what? open the javadoc zip and you can see that it is not in sync with the PDF. The javadoc uses "Content-ID". The main JSR page for SAAJ has link that says "Change Log for JSR 67". That link has no reference to any javadoc changes from the previous saaj revisions. If you see the J2EE 1.4 spec javadoc for AttachementPart, that has the old lower case version.
So, which is right? the javadoc or the spec? Oh forgot to mention, the TCK tests for the uppercase ("Content-ID"). My friend Steve is a huge advocate for test suites to accompany specs. In this case, having a test suite just was not enough for whatever reason. Guess, this is another reason why it was a bad idea to include JAX-WS/SAAJ in the JDK itself.