Java Reference
In-Depth Information
Figure 9.5:
Displaying attachments with links to download the files
This displays the attachments, as shown in Figure 9.5 . When the user
clicks a filename, Stripes sets the attachmentId parameter and calls the
downloadAttachment ( ) event handler of MessageDetailsActionBean .
We now need to stream the file to the user. We can send binary data
as a response to the browser by returning a StreamingResolution from an
event handler. StreamingResolution requires a content type and the data
to be sent. We can also set the filename that will be suggested to the
user when prompted to save the file. In our case, everything is provided
by the attachment:
Download email_19/src/stripesbook/action/MessageDetailsActionBean.java
public Integer attachmentId;
public Resolution downloadAttachment() throws Exception {
Attachment attachment = attachmentDao.read(attachmentId);
String fileName = attachment.getFileName();
String filePath = attachmentDao.getFilePath(attachment);
return new StreamingResolution(attachment.getContentType(),
new FileInputStream(filePath)).setFilename(fileName);
}
private AttachmentDao attachmentDao =
MockAttachmentDao.getInstance();
The constructors of the StreamingResolution class accept the data in the
form of an InputStream , a Reader , or a String . You can also build the data
yourself
by
subclassing
and
overriding
the
StreamingResolution
stream(HttpServletResponse) method. For example:
return new StreamingResolution(contentType) {
protected void stream(HttpServletResponse resp) throws Exception {
OutputStream output = resp.getOutputStream();
output.write(data);
}
}.setFilename("some_file.ext");
Wrapping Up
Wow. We used many new Stripes features to finish the Message List,
Message Details, and Message Compose pages of the webmail applica-
 
 
Search WWH ::




Custom Search