View Javadoc

1   package net.sourceforge.blogentis.modules.screens;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: BlogFilesAsZip.java,v 1.3 2004/11/01 22:44:35 tassos Exp $
23  //
24  
25  import java.io.BufferedInputStream;
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.zip.ZipEntry;
33  import java.util.zip.ZipOutputStream;
34  
35  import net.sourceforge.blogentis.om.Blog;
36  import net.sourceforge.blogentis.storage.AbstractFileResource;
37  import net.sourceforge.blogentis.storage.FileResourceFilter;
38  import net.sourceforge.blogentis.storage.FileRetrieverService;
39  import net.sourceforge.blogentis.turbine.BlogParameterParser;
40  import net.sourceforge.blogentis.turbine.BlogRunData;
41  import net.sourceforge.blogentis.turbine.BlogRunDataService;
42  import net.sourceforge.blogentis.utils.BlogConstants;
43  
44  import org.apache.commons.io.CopyUtils;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  import org.apache.turbine.modules.screens.RawScreen;
48  import org.apache.turbine.util.RunData;
49  import org.apache.turbine.util.security.AccessControlList;
50  import org.apache.velocity.context.Context;
51  
52  /***
53   * @author abas
54   */
55  public class BlogFilesAsZip
56          extends RawScreen {
57      private static final Log log = LogFactory.getLog(BlogFilesAsZip.class);
58  
59      public static final String ZIP_MIME_TYPE = "application/zip";
60  
61      public static class MediaFilter
62              implements FileResourceFilter {
63          public boolean ignored(String path) {
64              return path.endsWith("/CVS") || !path.startsWith("/media");
65          }
66  
67          public boolean descendInto(String folder) {
68              return true;
69          }
70      }
71  
72      protected String getContentType(RunData data) {
73          return ZIP_MIME_TYPE;
74      }
75  
76      protected InputStream getInputStream(BlogRunData data)
77              throws Exception {
78          BlogParameterParser bpp = (BlogParameterParser)BlogRunDataService
79                  .getCurrentRunData().getParameters();
80  
81          boolean doMedia = bpp.containsKey("media");
82  
83          log.info("Creating Zip of templates for blog "
84                  + bpp.getBlog().getName());
85  
86          FileResourceFilter filter;
87          if (doMedia)
88              filter = new MediaFilter();
89          else
90              filter = new BlogFiles.FileFilter();
91  
92          List l = FileRetrieverService.getInstance()
93                  .getTemplatePathList(data, bpp.getBlog(), "/", filter);
94  
95          data.getResponse().setHeader(
96                                       "Content-Disposition",
97                                       "attachment; file-name=\""
98                                               + bpp.getBlog().getName()
99                                               + "-"
100                                              + (bpp.containsKey("media")
101                                                      ? "media" : "templates")
102                                              + ".zip");
103 
104         ByteArrayOutputStream bos = new ByteArrayOutputStream(2000);
105         ZipOutputStream zos = new ZipOutputStream(bos);
106 
107         for(Iterator i = l.iterator(); i.hasNext();) {
108             String path = (String)i.next();
109             AbstractFileResource file = FileRetrieverService.getInstance()
110                     .getFile(bpp.getBlog(), path);
111             if (file.isDirectory() || file.isOriginal() || file.getSize() == 0)
112                 continue;
113             ZipEntry ze = new ZipEntry(path);
114             ze.setSize(file.getSize());
115             zos.putNextEntry(ze);
116             InputStream fis = file.getFileAsInputStream();
117             CopyUtils.copy(fis, zos);
118             fis.close();
119             zos.closeEntry();
120         }
121         zos.close();
122         return new ByteArrayInputStream(bos.toByteArray());
123     }
124 
125     protected boolean isAuthorized(RunData data, Context context) {
126         if (!data.getUser().hasLoggedIn())
127             return false;
128         BlogParameterParser pp = (BlogParameterParser)data.getParameters();
129         Blog blog = pp.getBlog();
130         if (blog == null)
131             return false;
132         AccessControlList acl = data.getACL();
133         if (acl == null || !acl.hasPermission(BlogConstants.PERM_ADMIN_BLOG, blog.getName()))
134             return false;
135         return true;
136     }
137 
138     protected final void doOutput(RunData data)
139             throws Exception {
140         BufferedInputStream r = new BufferedInputStream(
141             getInputStream((BlogRunData)data));
142         OutputStream os = data.getResponse().getOutputStream();
143         byte[] buffer = new byte[4096];
144         int len;
145         while ((len = r.read(buffer)) > 0) {
146             os.write(buffer, 0, len);
147         }
148         os.flush();
149     }
150 }