KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > servlet > GenericDownloadServlet


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.servlet;
21
22 import za.org.coefficient.authentication.CoefficientUser;
23 import za.org.coefficient.core.CoefficientWebContext;
24 import za.org.coefficient.interfaces.CoefficientContext;
25 import za.org.coefficient.util.common.FileDownloadData;
26 import za.org.coefficient.util.common.InvokerFactory;
27
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 /**
40  * This is the generic download servlet. It allows any module to register
41  * a listener method for downloading files. Once the downloader servlet is
42  * called it calls the registered method from a module.
43  *
44  * @author <a HREF="mailto:leon@psybergate.com">Leon Messerschmidt</a>
45  *
46  * @web.servlet
47  * name="GenericDownloadServlet"
48  * display-name="Generic Download Servlet"
49  * description="This servlet is used to download files from the system"
50  * @web.servlet-mapping
51  * url-pattern="/downloader.html"
52  */

53 public class GenericDownloadServlet extends HttpServlet JavaDoc {
54   
55     static private Map JavaDoc fileProviders = new HashMap JavaDoc();
56     
57     /**
58      * Add a new downloader provider to the system.
59      *
60      * @param provider
61      * @param module
62      * @param method
63      */

64     static public void addProvider (String JavaDoc provider, String JavaDoc module, String JavaDoc method) {
65         fileProviders.put(provider, new Provider(module,method));
66     }
67     
68     //~ Methods ================================================================
69

70     /**
71      * Call the download provider
72      */

73     protected void doGet(HttpServletRequest JavaDoc request,
74                          HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
75           
76         String JavaDoc providerName = request.getParameter("provider");
77         
78         if (providerName == null) {
79             sendError (response,"File provider not specified.");
80             return;
81         }
82         
83         Provider provider = (Provider)this.fileProviders.get(providerName);
84
85         if (provider == null) {
86             sendError (response,"Provider does not exist: "+providerName);
87             return;
88         }
89
90         CoefficientWebContext webContext =
91             new CoefficientWebContext(request,response);
92
93         try {
94             FileDownloadData fileDownloadData = (FileDownloadData)InvokerFactory.
95                 getRemoteInvoker().invokeOpOnModule(provider.getModule(),
96                                                     provider.getMethod(),
97                                                     webContext);
98
99             if (fileDownloadData == null) {
100                 sendError(response,"Provider Error: No FileData Object");
101                 return;
102             } else if (fileDownloadData.getBytes() == null) {
103                 sendError(response,"Provider Error: No File Content");
104                 return;
105             } else if (fileDownloadData.getFilename() == null) {
106                 sendError(response,"Provider Error: No File Name");
107                 return;
108             }
109
110             response.setHeader("Content-disposition", "attachment; filename="
111                                + fileDownloadData.getFilename());
112             response.setContentType(fileDownloadData.getMimeType());
113             response.setContentLength(fileDownloadData.getBytes().length);
114             response.getOutputStream()
115                 .write(fileDownloadData.getBytes());
116             response.flushBuffer();
117         } catch (Exception JavaDoc e) {
118             e.printStackTrace();
119             sendError(response,"Download Error: "+e);
120         }
121         webContext.synchronize(request, response);
122     }
123
124     /**
125      * Redirects to doGet
126      *
127      * @see doGet
128      *
129      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
130      */

131     protected void doPost(HttpServletRequest JavaDoc request,
132                           HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
133         doGet(request, response);
134     }
135
136     /**
137      * Formats an error that is sent to the user in a human readible string
138      * @param response
139      * @param error
140      */

141     private void sendError(HttpServletResponse JavaDoc response, String JavaDoc error) {
142         response.setContentType("text/html");
143         try {
144             PrintWriter JavaDoc writer = response.getWriter();
145             writer.println(error);
146             writer.flush();
147         } catch (IOException JavaDoc e) {
148             e.printStackTrace();
149         }
150     }
151     
152     /**
153      * This is a static inner class that stores the data for an email provider
154      *
155      * @author <a HREF="mailto:leon@psybergate.com">Leon Messerschmidt</a>
156      * @version $version$
157      *
158      */

159     static public class Provider {
160         private String JavaDoc module;
161         private String JavaDoc method;
162       
163         public Provider (String JavaDoc module, String JavaDoc method) {
164             this.module = module;
165             this.method = method;
166         }
167       
168         /**
169          * @return
170          */

171         public String JavaDoc getMethod() {
172             return method;
173         }
174
175         /**
176          * @return
177          */

178         public String JavaDoc getModule() {
179             return module;
180         }
181
182         /**
183          * @param string
184          */

185         public void setMethod(String JavaDoc string) {
186             method = string;
187         }
188
189         /**
190          * @param string
191          */

192         public void setModule(String JavaDoc string) {
193             module = string;
194         }
195
196     }
197 }
198
Popular Tags