KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > workplace > rfsfile > CmsRfsFileDownloadServlet


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/rfsfile/CmsRfsFileDownloadServlet.java,v $
3  * Date : $Date: 2006/07/20 10:14:23 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.workplace.rfsfile;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.flex.CmsFlexController;
36 import org.opencms.main.CmsException;
37 import org.opencms.security.CmsRole;
38 import org.opencms.security.CmsRoleViolationException;
39 import org.opencms.util.CmsStringUtil;
40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.net.SocketException JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.ServletOutputStream JavaDoc;
50 import javax.servlet.http.HttpServlet JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 /**
55  * Helper <code>Servlet</code> that reads the
56  * <b>request parameter <i>fileName</i></b> for a valid RFS file name and
57  * pushes this file to the client browser where it triggers a download
58  * (popup-box with save dialog). <p>
59  *
60  * @author Achim Westermann
61  *
62  * @version $Revision: 1.12 $
63  *
64  * @since 6.0.0
65  */

66 public final class CmsRfsFileDownloadServlet extends HttpServlet JavaDoc {
67
68     /** Serial version UID required for safe serialization. */
69     private static final long serialVersionUID = -2408134516284724987L;
70
71     /**
72      * Default constructor for this stateless class.
73      */

74     public CmsRfsFileDownloadServlet() {
75
76         // nop
77
}
78
79     /**
80      * Forwards the call to <code>{@link #doPost(HttpServletRequest, HttpServletResponse)}</code>.<p>
81      *
82      * @param request Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
83      * by the implicit jsp variable "request"
84      * @param response Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
85      * by the implicit jsp variable "response"
86      *
87      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
88      * @throws ServletException if the needed parameter 'filePath' cannot be found
89      * @throws IOException if work related to the download process fails
90      */

91     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
92
93         doPost(request, response);
94     }
95
96     /**
97      * Pushes a file that has been specified by request parameter
98      * <em>"filePath"</em> to the client browser.<p>
99      *
100      * The browser will open a popup menu that offers a donwload even if the
101      * file type is known to the client's OS.<p>
102      *
103      * Note that the <b>parameter "filePath"</b> is read from the
104      * given <code>{@link HttpServletRequest}</code> for the file name to serve.<p>
105      *
106      * @param req Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
107      * by the implicit jsp variable "request"
108      *
109      * @param res Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
110      * by the implicit jsp variable "response"
111      *
112      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
113      *
114      * @throws ServletException if the needed parameter 'filePath' cannot be found
115      * @throws IOException if work related to the download process fails
116      */

117     public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws IOException JavaDoc, ServletException JavaDoc {
118
119         // find the file:
120
String JavaDoc fileToFind = req.getParameter("filePath");
121         if (CmsStringUtil.isEmpty(fileToFind)) {
122             throw new ServletException JavaDoc(Messages.get().getBundle().key(Messages.ERR_DOWNLOAD_SERVLET_FILE_ARG_0));
123         } else {
124
125             CmsFlexController controller = CmsFlexController.getController(req);
126             try {
127                 // check if the current user is allowed to download files
128
controller.getCmsObject().checkRole(CmsRole.WORKPLACE_MANAGER);
129             } catch (CmsRoleViolationException e) {
130                 // user is not allowed, throw exception
131
CmsObject cms = controller.getCmsObject();
132                 CmsException exc = CmsRole.WORKPLACE_MANAGER.createRoleViolationException(cms.getRequestContext());
133                 throw new ServletException JavaDoc(exc.getLocalizedMessage(cms.getRequestContext().getLocale()));
134             }
135             
136             File JavaDoc downloadFile = new File JavaDoc(fileToFind);
137             res.setHeader("Content-Disposition", new StringBuffer JavaDoc("attachment; filename=\"").append(
138                 downloadFile.getName()).append("\"").toString());
139             res.setContentLength((int)downloadFile.length());
140
141             
142             res = controller.getTopResponse();
143             res.setContentType("application/octet-stream");
144
145             InputStream JavaDoc in = null;
146
147             // push the file:
148
ServletOutputStream JavaDoc outStream = null;
149
150             // getOutputStream() throws IllegalStateException if this servlet
151
// is triggered from jsp if the jsp directive buffer="none" is set.
152
// In that case the tomcat jsp-compiler accesses getWriter() before
153
// this call!!!
154
outStream = res.getOutputStream();
155             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(downloadFile));
156
157             try {
158                 // don't write the last '-1'
159
int bit = in.read();
160                 while ((bit) >= 0) {
161                     outStream.write(bit);
162                     bit = in.read();
163                 }
164             } catch (SocketException JavaDoc soe) {
165                 // this is the case for ie if cancel in download popup window is chosen:
166
// "Connection reset by peer: socket write error". But not for firefox -> don't care
167
} catch (IOException JavaDoc ioe) {
168                 // TODO: write nice exception?
169
throw ioe;
170             } finally {
171                 if (outStream != null) {
172                     outStream.flush();
173                     outStream.close();
174                 }
175                 in.close();
176             }
177         }
178     }
179
180     /**
181      * Dispatches to the single implemented method that deals with get and post requests.<p>
182      *
183      * @param request Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
184      * by the implicit jsp variable "request"
185      *
186      * @param response Provided by the servlet container if this servlet is directly used from the container's servlet-mappings or
187      * by the implicit jsp variable "response"
188      *
189      * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
190      * @throws ServletException if the needed parameter 'filePath' cannot be found
191      * @throws IOException if work related to the download process fails
192      */

193     protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
194     throws ServletException JavaDoc, IOException JavaDoc {
195
196         doPost(request, response);
197     }
198 }
199
Popular Tags