1 18 19 package cowsultants.itracker.web.servlets; 20 21 import java.io.*; 22 import java.rmi.*; 23 import java.util.*; 24 import javax.ejb.*; 25 import javax.rmi.*; 26 import javax.naming.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 import org.apache.struts.action.ActionError; 31 import org.apache.struts.action.ActionErrors; 32 33 import cowsultants.itracker.ejb.client.interfaces.*; 34 import cowsultants.itracker.ejb.client.models.*; 35 import cowsultants.itracker.ejb.client.util.*; 36 37 public class AttachmentDownloadController extends GenericController { 38 InitialContext ic; 39 40 public AttachmentDownloadController() { 41 } 42 43 public void init(ServletConfig config) { 44 try { 45 ic = new InitialContext(); 46 } catch(NamingException ne) { 47 Logger.logError("Could not locate session EJB during initalization.", ne); 48 } 49 } 50 51 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 52 ServletOutputStream out = null; 53 54 if(! isLoggedInWithRedirect(request, response)) { 55 return; 56 } 57 58 HttpSession session = request.getSession(); 59 UserModel user = (UserModel) session.getAttribute("user"); 60 try { 61 Object ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME); 62 IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class); 63 IssueHandler ih = ihHome.create(); 64 65 Integer attachmentId = null; 66 IssueAttachmentModel attachment = null; 67 68 try { 69 attachmentId = new Integer ((request.getParameter("id") == null ? "-1" : (request.getParameter("id")))); 70 attachment = ih.getIssueAttachment(attachmentId); 71 } catch(NumberFormatException nfe) { 72 if(Logger.isLoggingDebug()) { 73 Logger.logDebug("Invalid attachmentId " + request.getParameter("id") + " specified."); 74 } 75 } 76 77 if(attachment == null) { 78 ActionErrors errors = new ActionErrors(); 79 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidattachment")); 80 saveErrors(request, errors); 81 forward("/error.jsp", request, response); 82 return; 83 } 84 85 if(! ih.canViewIssue(attachment.getIssueId(), user)) { 86 forward("/unauthorized.jsp", request, response); 87 return; 88 } 89 90 byte[] fileData = ih.getIssueAttachmentData(attachmentId); 91 if(fileData == null) { 92 ActionErrors errors = new ActionErrors(); 93 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.missingattachmentdata")); 94 saveErrors(request, errors); 95 forward("/error.jsp", request, response); 96 return; 97 } 98 99 response.setContentType(attachment.getType()); 100 response.setHeader("Content-Disposition", "inline; filename=" + attachment.getOriginalFileName() + ""); 101 out = response.getOutputStream(); 102 Logger.logDebug("Displaying attachment " + attachment.getId() + " of type " + attachment.getType() + " to client. Attachment size: " + fileData.length); 103 out.write(fileData); 104 } catch(CreateException ce) { 105 } catch(NamingException ne) { 106 } catch(IOException ioe) { 107 Logger.logInfo("Unable to display attachment.", ioe); 108 } finally { 109 if(out != null) { 110 out.flush(); 111 out.close(); 112 } 113 } 114 115 return; 116 } 117 } | Popular Tags |