View Javadoc

1   package de.tivsource.page.admin.actions.maintenance.cssfile;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.util.Iterator;
9   import java.util.zip.ZipEntry;
10  import java.util.zip.ZipOutputStream;
11  
12  import org.apache.logging.log4j.LogManager;
13  import org.apache.logging.log4j.Logger;
14  import org.apache.struts2.convention.annotation.Action;
15  import org.apache.struts2.convention.annotation.Actions;
16  import org.apache.struts2.convention.annotation.Result;
17  
18  import de.tivsource.ejb3plugin.InjectEJB;
19  import de.tivsource.page.admin.actions.EmptyAction;
20  import de.tivsource.page.backup.BackupCSSFile;
21  import de.tivsource.page.dao.cssfile.CSSFileDaoLocal;
22  
23  /**
24   * 
25   * @author Marc Michele
26   *
27   */
28  public class BackupAction extends EmptyAction {
29  
30      /**
31       * Serial Version UID.
32       */
33      private static final long serialVersionUID = -4095835501014581918L;
34  
35      /**
36       * Statischer Logger der Klasse.
37       */
38      private static final Logger LOGGER = LogManager.getLogger(BackupAction.class);
39  
40      @InjectEJB(name="CSSFileDao")
41      private CSSFileDaoLocal cssFileDaoLocal;
42  
43      private InputStream fileStream;
44  
45      private static byte[] buffer = new byte[1024];
46  
47      @Override
48      @Actions({
49          @Action(
50              value = "backup",
51              results = { @Result(
52                      name = "success",
53                      type="stream",
54                      params={"contentType", "text/plain", "inputName", "fileStream", "contentDisposition", "attachment;filename=cssFile.zip"}
55              )}
56          )
57      })
58      public String execute() throws Exception {
59          LOGGER.info("execute() aufgerufen.");
60          File backupFile = this.getZipFile();
61          fileStream = new FileInputStream(backupFile);
62          backupFile.delete();
63          return SUCCESS;
64      }
65  
66      public InputStream getFileStream() {
67          return fileStream;
68      }
69  
70      private File getZipFile() throws IOException {
71          LOGGER.info("getZipFile() aufgerufen.");
72  
73          // Zip-Datei erstellen und Stream bereitstellen.
74          File zipFile = File.createTempFile("cssFile", "zip");
75          ZipOutputStream outZipFile = new ZipOutputStream(new FileOutputStream(zipFile));
76  
77          BackupCSSFile backupCSSFile = new BackupCSSFile(cssFileDaoLocal);
78          Iterator<File> cssFileIterator = backupCSSFile.getBackupFiles().iterator();
79          while(cssFileIterator.hasNext()) {
80              File next = cssFileIterator.next();
81              addData(next, outZipFile, next.getName());
82          }
83  
84          // Schließe die Zip-Datei.
85          outZipFile.close();
86  
87          return zipFile;
88      }
89  
90      private static void addData(File file, ZipOutputStream zipOutputStream, String filename) throws IOException {
91  
92          FileInputStream fileInputStream = new FileInputStream(file);
93  
94          // Erstelle neuen ZIP Datei Eintrag
95          zipOutputStream.putNextEntry(new ZipEntry(filename));
96  
97          // Transferiere bytes von der Datei in die ZIP Datei
98          int len;
99          while ((len = fileInputStream.read(buffer)) > 0) {
100             zipOutputStream.write(buffer, 0, len);
101         }
102 
103         zipOutputStream.closeEntry();
104         fileInputStream.close();
105 
106         // Lösche die Dateien nur wenn es nicht die Orginal Bilder sind.
107         if(!file.getName().endsWith(".css")) {
108             file.delete();
109         }
110     }
111     
112 }// Ende class