1 package net.sourceforge.blogentis.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 }