View Javadoc

1   package de.tivsource.page.admin.actions.maintenance.files;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileOutputStream;
6   import java.io.InputStream;
7   
8   import org.apache.logging.log4j.LogManager;
9   import org.apache.logging.log4j.Logger;
10  import org.apache.struts2.convention.annotation.Action;
11  import org.apache.struts2.convention.annotation.Actions;
12  import org.apache.struts2.convention.annotation.Result;
13  import org.apache.struts2.tiles.annotation.TilesDefinition;
14  import org.apache.struts2.tiles.annotation.TilesDefinitions;
15  import org.apache.struts2.tiles.annotation.TilesPutAttribute;
16  
17  import de.tivsource.ejb3plugin.InjectEJB;
18  import de.tivsource.page.admin.actions.EmptyAction;
19  import de.tivsource.page.dao.property.PropertyDaoLocal;
20  
21  /**
22   * 
23   * @author Marc Michele
24   *
25   */
26  @TilesDefinitions({
27    @TilesDefinition(name="cssAddForm",  extend = "adminTemplate", putAttributes = {
28      @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/maintenance.jsp"),
29      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/files/add_form.jsp")
30    }),
31    @TilesDefinition(name="cssAddError",  extend = "adminTemplate", putAttributes = {
32      @TilesPutAttribute(name = "navigation", value = "/WEB-INF/tiles/active/navigation/maintenance.jsp"),
33      @TilesPutAttribute(name = "content",    value = "/WEB-INF/tiles/active/view/files/css_add_error.jsp")
34    })
35  })
36  public class AddAction extends EmptyAction {
37  
38      /**
39  	 * Serial Version UID.
40  	 */
41  	private static final long serialVersionUID = 6514256494686145951L;
42  
43  	/**
44       * Statischer Logger der Klasse.
45       */
46      private static final Logger LOGGER = LogManager.getLogger(AddAction.class);
47  
48      @InjectEJB(name="PropertyDao")
49      private PropertyDaoLocal propertyDaoLocal;
50  
51      private File file;
52  
53      private String contentType;
54  
55      private String filename;
56      
57      public void setFile(File file) {
58          this.file = file;
59      }
60  
61      public void setFileContentType(String contentType) {
62          this.contentType = contentType;
63      }
64  
65      public void setFileFileName(String filename) {
66          this.filename = filename;
67      }
68      
69      @Override
70      @Actions({
71          @Action(
72          		value = "add", 
73          		results = {
74          				@Result(name = "success", type = "redirectAction", location = "index.html"),
75          				@Result(name = "input", type="tiles", location = "cssAddForm"),
76          				@Result(name = "error", type="tiles", location = "cssAddError") 
77          		}
78          )
79      })
80      public String execute() throws Exception {
81      	LOGGER.info("execute() aufgerufen.");
82      	String pathToFiles = propertyDaoLocal.findByKey("upload-path").getValue();
83  
84      	File fileToCreate = new File(pathToFiles, filename);
85          // Wenn die Datei noch nicht existiert wird Sie erstellt.
86          if (!fileToCreate.exists()) {
87          	saveCssFile(file, fileToCreate);
88              return SUCCESS;
89          }
90          else {
91          	return ERROR;
92          }
93          
94  
95      	
96      }// Ende execute()
97  
98      private static void saveCssFile(File source, File destination) throws Exception {
99          byte[] buffer = new byte[(int) source.length()];
100         InputStream in = new FileInputStream(source);
101         in.read(buffer);
102         FileOutputStream fileOutStream = new FileOutputStream(destination);
103         fileOutStream.write(buffer);
104         fileOutStream.flush();
105         fileOutStream.close();
106         in.close();
107     }
108     
109 }// Ende class