e.g. Calendar Search Help
You must enter a value before pressing Search
xplanner

Class: com.technoetic.xplanner.actions.FileManagerAction   ©

 OK to copy?
01 package com.technoetic.xplanner.actions;
02 
03 import com.technoetic.xplanner.file.Directory;
04 import com.technoetic.xplanner.file.File;
05 import com.technoetic.xplanner.file.FileSystem;
06 import com.technoetic.xplanner.forms.FileManagerForm;
07 import net.sf.hibernate.ObjectNotFoundException;
08 import net.sf.hibernate.Session;
09 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12 import org.apache.struts.upload.FormFile;
13 
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.sql.SQLException;
20 
21 /**
22  * EXPERIMENTAL - File Management Action
23  */
24 public class FileManagerAction extends AbstractAction {
25     private final int BUFFER_SIZE = 4000;
26     private FileSystem fileSystem;
27 
28     protected ActionForward doExecute(ActionMapping mapping,
29             ActionForm form, HttpServletRequest request,
30             HttpServletResponse response) throws Exception {
31         Session hibernateSession = getSession(request);
32         FileManagerForm fform = (FileManagerForm)form;
33         try {
34             if (fform.getAction() == null) {
35                 fform.setAction("list");
36                 fform.setDirectoryId(Integer.toString(
37                         fileSystem.getRootDirectory().getId()));
38             }
39             if (fform.getAction().equals("upload")) {
40                 FormFile formFile = fform.getFormFile();
41                 fileSystem.createFile(hibernateSession, Integer.parseInt(fform.getDirectoryId()), formFile.getFileName(),
42                         formFile.getContentType(), formFile.getFileSize(), formFile.getInputStream());
43             } else if (fform.getAction().equals("download")) {
44                 File file = fileSystem.getFile(hibernateSession, Integer.parseInt(fform.getFileId()));
45                 writeFileToResponse(response, file);
46             } else if (fform.getAction().equals("delete")) {
47                 fileSystem.deleteFile(hibernateSession, Integer.parseInt(fform.getFileId()));
48             } else if (fform.getAction().equals("mkdir")) {
49                 int parentDirectoryId = Integer.parseInt(fform.getDirectoryId());
50                 fileSystem.createDirectory(hibernateSession, parentDirectoryId, fform.getName());
51             } else if (fform.getAction().equals("rmdir")) {
52                 Directory directory = fileSystem.getDirectory(hibernateSession, Integer.parseInt(fform.getDirectoryId()));
53                 Directory parent = directory.getParent();
54                 if (parent == null) {
55                     parent = fileSystem.getRootDirectory();
56                 }
57                 fform.setDirectoryId(Integer.toString(parent.getId()));
58                 fileSystem.deleteDirectory(hibernateSession, directory.getId());
59             }
60             hibernateSession.flush();
Rate61             hibernateSession.connection().commit();
62             if (fform.getDirectoryId() != null) {
63                 Directory directory = fileSystem.getDirectory(hibernateSession, Integer.parseInt(fform.getDirectoryId()));
64                 request.setAttribute("directory", directory);
65             }
66             request.setAttribute("root", fileSystem.getRootDirectory());
67             return mapping.findForward("display");
68         } catch (ObjectNotFoundException ex) {
69             request.setAttribute("exception", ex);
70             return mapping.findForward("error/objectNotFound");
71         } catch (Exception e) {
72             hibernateSession.connection().rollback();
73             throw e;
74         }
75     }
76 
77     private void writeFileToResponse(HttpServletResponse response, File file) throws IOException, SQLException {
78         response.setContentType(file.getContentType());
79         response.setHeader("Content-disposition", "note;filename=\"" +
80                 file.getName() + "\"");
81         response.addHeader("Content-description", file.getName());
82         ServletOutputStream stream = response.getOutputStream();
83         InputStream attachmentStream = file.getData().getBinaryStream();
84         byte[] buffer = new byte[BUFFER_SIZE];
85         int n = attachmentStream.read(buffer);
86         while (n > 0) {
87             stream.write(buffer, 0, n);
88             n = attachmentStream.read(buffer);
89         }
90         stream.flush();
91         stream.close();
92     }
93 
94     public void setFileSystem(FileSystem fileSystem) {
95         this.fileSystem = fileSystem;
96     }
97 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
61 0% of 0