Package net.sourceforge.stripes.examples.bugzooky

Source Code of net.sourceforge.stripes.examples.bugzooky.DownloadAttachmentActionBean

package net.sourceforge.stripes.examples.bugzooky;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.examples.bugzooky.biz.Attachment;
import net.sourceforge.stripes.examples.bugzooky.biz.Bug;
import net.sourceforge.stripes.examples.bugzooky.biz.BugManager;

import java.io.ByteArrayInputStream;

/**
* Action that responds to a user's request to download an attachment to a bug.
*
* @author Tim Fennell
*/
public class DownloadAttachmentActionBean extends BugzookyActionBean {
    private Integer bugId;
    private Integer attachmentIndex;

    public Integer getBugId() { return bugId; }
    public void setBugId(Integer bugId) { this.bugId = bugId; }

    public Integer getAttachmentIndex() { return attachmentIndex; }
    public void setAttachmentIndex(Integer attachmentIndex) { this.attachmentIndex = attachmentIndex; }

    @DefaultHandler
    public Resolution getAttachment() {
        BugManager bm = new BugManager();
        Bug bug = bm.getBug(this.bugId);
        Attachment attachment = bug.getAttachments().get(this.attachmentIndex);

        // Uses a StreamingResolution to send the file contents back to the user.
        // Note the use of the chained .setFilename() method, which causes the
        // browser to [prompt to] save the "file" instead of displaying it in browser
        return new StreamingResolution
                (attachment.getContentType(), new ByteArrayInputStream(attachment.getData()))
                    .setFilename(attachment.getName());
    }
}
TOP

Related Classes of net.sourceforge.stripes.examples.bugzooky.DownloadAttachmentActionBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.