KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > DownloadZipServlet


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.io.*;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import org.meshcms.util.*;
29
30 /**
31  * A simple servlet to allow to download a file regardless of its type.
32  */

33 public final class DownloadZipServlet extends HttpServlet {
34   /**
35    * Handles the HTTP <code>GET</code> method.
36    *
37    * @param request servlet request
38    * @param response servlet response
39    */

40   protected void doGet(HttpServletRequest request, HttpServletResponse response)
41       throws ServletException, IOException {
42     processRequest(request, response);
43   }
44
45   /**
46    * Handles the HTTP <code>POST</code> method.
47    *
48    * @param request servlet request
49    * @param response servlet response
50    */

51   protected void doPost(HttpServletRequest request, HttpServletResponse response)
52       throws ServletException, IOException {
53     processRequest(request, response);
54   }
55
56   /**
57    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
58    *
59    * @param request servlet request
60    * @param response servlet response
61    */

62   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
63       throws IOException {
64     /* Usage of this servlet must be restricted to authenticated users,
65        otherwise it'd be really easy to download the whole site */

66     UserInfo userInfo = (UserInfo)
67         request.getSession(true).getAttribute("userInfo");
68
69     if (userInfo == null || userInfo.isGuest()) {
70       response.sendError(HttpServletResponse.SC_NOT_FOUND);
71       return;
72     }
73
74     WebSite webSite = (WebSite) request.getAttribute("webSite");
75     Path path = new Path(request.getPathInfo());
76
77     /* if (webSite.isSystem(path)) {
78       response.sendError(HttpServletResponse.SC_NOT_FOUND);
79       return;
80     } */

81
82     File file = webSite.getFile(path);
83
84     if (!file.exists()) {
85       response.sendError(HttpServletResponse.SC_NOT_FOUND,
86           "File not found on server");
87       return;
88     }
89
90     response.setContentType("application/x-download");
91     String JavaDoc fileName = request.getParameter("filename");
92
93     if (Utils.isNullOrEmpty(fileName)) {
94       fileName = path.getLastElement();
95     }
96
97     if (!file.isDirectory()) {
98       fileName = Utils.removeExtension(fileName);
99     }
100
101     response.setHeader("Content-Disposition", "attachment; filename=\"" +
102         fileName + ".zip\"");
103     new ZipArchiver(file, response.getOutputStream()).process();
104   }
105 }
106
Popular Tags