001: package com.technoetic.xplanner.actions;
002:
003: import com.technoetic.xplanner.file.Directory;
004: import com.technoetic.xplanner.file.File;
005: import com.technoetic.xplanner.file.FileSystem;
006: import com.technoetic.xplanner.forms.FileManagerForm;
007: import net.sf.hibernate.ObjectNotFoundException;
008: import net.sf.hibernate.Session;
009: import org.apache.struts.action.ActionForm;
010: import org.apache.struts.action.ActionForward;
011: import org.apache.struts.action.ActionMapping;
012: import org.apache.struts.upload.FormFile;
013:
014: import javax.servlet.ServletOutputStream;
015: import javax.servlet.http.HttpServletRequest;
016: import javax.servlet.http.HttpServletResponse;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.sql.SQLException;
020:
021: /**
022: * EXPERIMENTAL - File Management Action
023: */
024: public class FileManagerAction extends AbstractAction {
025: private final int BUFFER_SIZE = 4000;
026: private FileSystem fileSystem;
027:
028: protected ActionForward doExecute(ActionMapping mapping,
029: ActionForm form, HttpServletRequest request,
030: HttpServletResponse response) throws Exception {
031: Session hibernateSession = getSession(request);
032: FileManagerForm fform = (FileManagerForm) form;
033: try {
034: if (fform.getAction() == null) {
035: fform.setAction("list");
036: fform.setDirectoryId(Integer.toString(fileSystem
037: .getRootDirectory().getId()));
038: }
039: if (fform.getAction().equals("upload")) {
040: FormFile formFile = fform.getFormFile();
041: fileSystem.createFile(hibernateSession, Integer
042: .parseInt(fform.getDirectoryId()), formFile
043: .getFileName(), formFile.getContentType(),
044: formFile.getFileSize(), formFile
045: .getInputStream());
046: } else if (fform.getAction().equals("download")) {
047: File file = fileSystem.getFile(hibernateSession,
048: Integer.parseInt(fform.getFileId()));
049: writeFileToResponse(response, file);
050: } else if (fform.getAction().equals("delete")) {
051: fileSystem.deleteFile(hibernateSession, Integer
052: .parseInt(fform.getFileId()));
053: } else if (fform.getAction().equals("mkdir")) {
054: int parentDirectoryId = Integer.parseInt(fform
055: .getDirectoryId());
056: fileSystem.createDirectory(hibernateSession,
057: parentDirectoryId, fform.getName());
058: } else if (fform.getAction().equals("rmdir")) {
059: Directory directory = fileSystem.getDirectory(
060: hibernateSession, Integer.parseInt(fform
061: .getDirectoryId()));
062: Directory parent = directory.getParent();
063: if (parent == null) {
064: parent = fileSystem.getRootDirectory();
065: }
066: fform.setDirectoryId(Integer.toString(parent.getId()));
067: fileSystem.deleteDirectory(hibernateSession, directory
068: .getId());
069: }
070: hibernateSession.flush();
071: hibernateSession.connection().commit();
072: if (fform.getDirectoryId() != null) {
073: Directory directory = fileSystem.getDirectory(
074: hibernateSession, Integer.parseInt(fform
075: .getDirectoryId()));
076: request.setAttribute("directory", directory);
077: }
078: request.setAttribute("root", fileSystem.getRootDirectory());
079: return mapping.findForward("display");
080: } catch (ObjectNotFoundException ex) {
081: request.setAttribute("exception", ex);
082: return mapping.findForward("error/objectNotFound");
083: } catch (Exception e) {
084: hibernateSession.connection().rollback();
085: throw e;
086: }
087: }
088:
089: private void writeFileToResponse(HttpServletResponse response,
090: File file) throws IOException, SQLException {
091: response.setContentType(file.getContentType());
092: response.setHeader("Content-disposition", "note;filename=\""
093: + file.getName() + "\"");
094: response.addHeader("Content-description", file.getName());
095: ServletOutputStream stream = response.getOutputStream();
096: InputStream attachmentStream = file.getData().getBinaryStream();
097: byte[] buffer = new byte[BUFFER_SIZE];
098: int n = attachmentStream.read(buffer);
099: while (n > 0) {
100: stream.write(buffer, 0, n);
101: n = attachmentStream.read(buffer);
102: }
103: stream.flush();
104: stream.close();
105: }
106:
107: public void setFileSystem(FileSystem fileSystem) {
108: this.fileSystem = fileSystem;
109: }
110: }
|