A SERVICE OF

logo

116 Chapter 10. Kernel Tutorial
Note that the Message class only support bodies with a primary MIME type of text. The Mes-
sageType interface deļ¬nes legal MIME types for use in specifying a message body.
10.2.3.2. Referring to Other Objects
Messages can store an optional reference to an ACSObject. This is useful for attaching messages
to other items in the system. For example, a discussion forum might implement a Topic class to
represent a collection of related messages. The process of adding a message to a given topic can be
implemented by setting a reference to that topic:
// One topic in a discussion forum
class Topic extends ACSObject {
// Add a new posting for this topic
addNewPosting (User postedBy, String subject, String post) {
Message msg = new Message(postedBy, subject, post);
msg.setRefersTo(this);
msg.save();
}
}
10.2.3.3. Attachments
Additional pieces of content can be attached to a message. These content items can be any media
type with a supported DataHandler. Convenience methods are provided for attaching plain text
documents directly to a message:
Message msg = new Message(from, subject);
msg.setText("See the attached document.");
msg.attach("A simple document", "simple.txt");
msg.save();
To attach more complex types of content, you use a MessagePart and an appropriate data source
or DataHandler. There are convenience methods to attach content retrieved from a File or a URL.
The following example shows how to attach an uploaded image to a message:
File file = new File("image.gif");
MessagePart part = new MessagePart();
part.setContent(image, "image.gif", "An image");
Message msg = new Message(from, subject, "See attached image");
msg.attach(part);
msg.save();
The code for downloading the image from a remote server is almost identical:
URL url = new URL("http://mysite.net/image.gif");
MessagePart part = new MessagePart();
part.setContent(url, "image.gif", "An image from mysite");
Message msg = new Message(from, subject, "See attached image");
msg.attach(part);
msg.save();