KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > ExportAttachmentsAction


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.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import java.util.zip.*;
25 import javax.ejb.*;
26 import javax.rmi.*;
27 import javax.naming.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import org.apache.commons.beanutils.*;
32 import org.apache.struts.action.*;
33 import org.apache.struts.upload.*;
34 import org.apache.struts.util.*;
35 import org.apache.struts.validator.*;
36
37 import cowsultants.itracker.ejb.client.exceptions.*;
38 import cowsultants.itracker.ejb.client.interfaces.*;
39 import cowsultants.itracker.ejb.client.models.*;
40 import cowsultants.itracker.ejb.client.resources.*;
41 import cowsultants.itracker.ejb.client.util.*;
42 import cowsultants.itracker.web.util.*;
43
44
45 public class ExportAttachmentsAction extends ITrackerAction {
46
47     public ExportAttachmentsAction () {
48     }
49
50     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51         ActionErrors errors = new ActionErrors();
52
53         if(! isLoggedIn(request, response)) {
54             return mapping.findForward("login");
55         }
56
57         if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
58             return mapping.findForward("unauthorized");
59         }
60
61         try {
62             InitialContext ic = new InitialContext();
63
64             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
65             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
66             IssueHandler ih = ihHome.create();
67
68             IssueAttachmentModel[] attachments = ih.getAllIssueAttachments();
69             if(attachments.length > 0) {
70                 response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_attachments.zip\"");
71                 ServletOutputStream out = response.getOutputStream();
72                 ZipOutputStream zipOut = new ZipOutputStream(out);
73                 try {
74                     for(int i = 0; i < attachments.length; i++) {
75                         Logger.logDebug("Attempting export for: " + attachments[i]);
76                         byte[] attachmentData = ih.getIssueAttachmentData(attachments[i].getId());
77                         if(attachmentData.length > 0) {
78                             ZipEntry zipEntry = new ZipEntry(attachments[i].getFileName() + attachments[i].getFileExtension());
79                             zipEntry.setSize(attachmentData.length);
80                             zipEntry.setTime(attachments[i].getLastModifiedDate().getTime());
81                             zipOut.putNextEntry(zipEntry);
82                             zipOut.write(attachmentData, 0, attachmentData.length);
83                             zipOut.closeEntry();
84                         }
85                     }
86                     zipOut.close();
87                     out.flush();
88                     out.close();
89                 } catch(Exception JavaDoc e) {
90                     Logger.logError("Exception while exporting attachments.", e);
91                 }
92                 return null;
93             }
94             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.noattachments"));
95         } catch(Exception JavaDoc e) {
96             Logger.logError("Exception while exporting attachments.", e);
97             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
98         }
99
100         if(! errors.isEmpty()) {
101             saveErrors(request, errors);
102         }
103
104         return mapping.findForward("error");
105     }
106
107 }
108   
Popular Tags