View Javadoc

1   package org.itracker.web.actions.admin.attachment;
2   
3   import javax.servlet.ServletOutputStream;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   
7   import org.apache.log4j.Logger;
8   import org.apache.struts.action.ActionForm;
9   import org.apache.struts.action.ActionForward;
10  import org.apache.struts.action.ActionMapping;
11  import org.apache.struts.action.ActionMessage;
12  import org.apache.struts.action.ActionMessages;
13  import org.itracker.model.IssueAttachment;
14  import org.itracker.services.IssueService;
15  import org.itracker.web.actions.base.ItrackerBaseAction;
16  import org.springframework.web.bind.ServletRequestUtils;
17  
18  public class DownloadAttachmentAction extends ItrackerBaseAction {
19  
20  	private static final Logger log = Logger.getLogger(DownloadAttachmentAction.class);
21  	
22  	public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request,
23  			HttpServletResponse response) throws Exception {
24  
25  		Integer attachmentID = ServletRequestUtils.getIntParameter(request, "id");
26  
27  		IssueService issueService = getITrackerServices().getIssueService();
28  
29  		IssueAttachment attachment = issueService.getIssueAttachment(attachmentID);
30  
31  		if (attachment.getFileData() == null) {
32  			ActionMessages errors = new ActionMessages();
33  
34  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingattachmentdata"));
35  
36  			saveErrors(request, errors);
37  
38  			return actionMapping.findForward("error_page");
39  		}
40  
41  		response.setContentType(attachment.getType());
42  		response.setHeader("Content-Disposition", "inline; filename=" + attachment.getOriginalFileName() + "");
43  		ServletOutputStream outputStream = response.getOutputStream();
44  		log.debug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType()
45  				+ " to client.  Attachment size: " + attachment.getFileData().length);
46  
47  		outputStream.write(attachment.getFileData());
48  
49  		return null;
50  
51  	}
52  
53  }