KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > servlets > AttachmentDownloadController


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

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 JavaDoc 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 JavaDoc attachmentId = null;
66             IssueAttachmentModel attachment = null;
67
68             try {
69                 attachmentId = new Integer JavaDoc((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
70                 attachment = ih.getIssueAttachment(attachmentId);
71             } catch(NumberFormatException JavaDoc 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