View Javadoc

1   package net.sourceforge.blogentis.modules.actions;
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: BlogFileRestoreZip.java,v 1.2 2004/10/31 07:16:22 tassos Exp $
23  //
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.IOException;
29  import java.util.ArrayList;
30  import java.util.Date;
31  import java.util.Enumeration;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.zip.ZipEntry;
35  import java.util.zip.ZipInputStream;
36  
37  import net.sourceforge.blogentis.om.Blog;
38  import net.sourceforge.blogentis.slide.SlideMemoryFile;
39  import net.sourceforge.blogentis.slide.SlideService;
40  import net.sourceforge.blogentis.turbine.BlogParameterParser;
41  import net.sourceforge.blogentis.turbine.SecureBlogAction;
42  import net.sourceforge.blogentis.utils.BlogConstants;
43  import net.sourceforge.blogentis.utils.BlogRequestWrapper;
44  
45  import org.apache.commons.io.CopyUtils;
46  import org.apache.torque.TorqueException;
47  import org.apache.turbine.services.mimetype.TurbineMimeTypes;
48  import org.apache.turbine.util.RunData;
49  import org.apache.velocity.context.Context;
50  
51  /***
52   * @author abas
53   */
54  public class BlogFileRestoreZip
55          extends SecureBlogAction {
56      private SlideMemoryFile getBlogFile(ZipInputStream zip, ZipEntry ze, Blog b)
57              throws TorqueException, IOException {
58          SlideMemoryFile smf = new SlideMemoryFile(b, ze.getName());
59          smf.setLastModified(new Date(ze.getTime()));
60          ByteArrayOutputStream bos = new ByteArrayOutputStream();
61          CopyUtils.copy(zip, bos);
62          smf.setMimeType(TurbineMimeTypes.getContentType(smf.getPath()));
63          smf.setContents(bos.toByteArray());
64          zip.closeEntry();
65          return smf;
66      }
67  
68      public void doPerform(RunData data, Context context)
69              throws Exception {
70          BlogRequestWrapper brw = (BlogRequestWrapper)data.getRequest();
71  
72          if (!brw.isMultipart()) {
73              data.setMessage("No file has been uploaded");
74              return;
75          }
76          Enumeration e = brw.getFileNames();
77          if (!e.hasMoreElements()) {
78              data.setMessage("No file has been uploaded");
79              return;
80          }
81          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
82          String uploadName = (String)e.nextElement();
83          File f = brw.getFile(uploadName);
84  
85          ZipInputStream zip = null;
86          List fileList = new ArrayList();
87          try {
88              zip = new ZipInputStream(new FileInputStream(f));
89              ZipEntry ze;
90              while ((ze = zip.getNextEntry()) != null) {
91                  fileList.add(getBlogFile(zip, ze, pp.getBlog()));
92              }
93          } catch (IOException ioe) {
94              data.setMessage("Error: perhaps not a ZIP file? "
95                      + ioe.getMessage());
96              return;
97          }
98  
99          if (fileList.size() == 0) {
100             data.setMessage("No files found: perhaps not a ZIP file?");
101             return;
102         }
103 
104         for(Iterator i = fileList.iterator(); i.hasNext();) {
105             SlideMemoryFile smf = (SlideMemoryFile)i.next();
106             if (SlideService.getInstance().saveFile(null, smf)) {
107                 log.warn("Hmm... some problems with" + smf.getPath());
108             }
109         }
110     }
111     
112     protected String[] getPermissions() {
113         return new String[]{BlogConstants.PERM_ADMIN_BLOG};
114     }
115 }