1 6 7 package org.contineo.actions.documan.document; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.InputStream ; 12 import java.io.PrintWriter ; 13 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.http.HttpSession ; 17 18 import org.apache.log4j.Level; 19 import org.apache.log4j.Logger; 20 import org.apache.struts.action.Action; 21 import org.apache.struts.action.ActionForm; 22 import org.apache.struts.action.ActionForward; 23 import org.apache.struts.action.ActionMapping; 24 import org.contineo.actions.documan.util.DownloadDocUtil; 25 import org.contineo.admin.Menu; 26 import org.contineo.admin.dao.MenuDAO; 27 import org.contineo.core.FileBean; 28 import org.contineo.core.JarBean; 29 import org.contineo.core.LoggingManager; 30 import org.contineo.core.SessionManagement; 31 import org.contineo.core.ZipBean; 32 import org.contineo.core.config.SettingConfigurator; 33 import org.contineo.core.util.HTMLReviser; 34 import org.contineo.documan.Document; 35 import org.contineo.documan.dao.DocumentDAO; 36 40 public class ShowDocumentAction extends Action { 41 42 private InputStream is = null; 43 private File file = null; 44 private String path = ""; 45 46 50 private Logger logger; 51 52 53 54 public ShowDocumentAction() { 55 logger = LoggingManager.getLogger(this.getClass()); 56 } 57 58 61 public ActionForward execute(ActionMapping mapping, 62 ActionForm form, HttpServletRequest request, 63 HttpServletResponse response) { 64 ActionForward actionForward = new ActionForward(); 65 HttpSession session = request.getSession(); 66 if (SessionManagement.isValid(session)) { 67 SettingConfigurator conf = new SettingConfigurator(); 68 try { 69 String menuid = request.getParameter("menuid"); 70 String version = request.getParameter("version"); 71 String reference = request.getParameter("reference"); 72 String userName = (String )session.getAttribute("authuser"); 73 int id = Integer.parseInt(menuid); 74 MenuDAO mdao = new MenuDAO(); 76 if (mdao.isReadEnable(id, userName)) { 77 Menu menu = mdao.findByPrimaryKey(id); 78 DocumentDAO ddao = new DocumentDAO(); 79 Document doc = ddao.findByMenuId(id); 80 String extension = ""; 81 String filename = ""; 82 String docversion = doc.getDocVersion(); 83 docversion = docversion.trim(); 84 85 DownloadDocUtil.addToRecentFiles(userName, id); 87 88 if ((version != null) && (!version.trim().equals(docversion))) { 90 path = conf.getValue("docdir") + "/"; 91 path += menu.getMenuPath() + "/"; 92 path += String.valueOf(menu.getMenuId()) + "/"; 93 if (reference == null || reference.equals("")) { 94 String userdir = conf.getValue("userdir") + "/" + userName + "/temp"; 95 FileBean.createDir(userdir); 96 extension = doc.getDocType(); 97 if (extension.equals("zip")) { 98 ZipBean.unzip(path + version, userdir); 100 path = userdir; 101 } 102 if (extension.equals("jar")) { 103 JarBean.unjar(path + version, userdir); 105 path = userdir; 106 } 107 } 108 if (extension.equals("zip") || extension.equals("jar")) 110 filename = menu.getMenuRef(); 111 else 112 filename = version; 113 } else { 114 path = conf.getValue("docdir") + menu.getMenuPath() + "/" + menu.getMenuId(); 115 filename = menu.getMenuRef(); 116 } 117 String mimetype = DownloadDocUtil.getMimeType(menu); 119 response.setContentType(mimetype); 120 121 if (reference == null || reference.equals("")) { 122 file = new File (path + "/" + filename); 123 is = new FileInputStream (file); 124 } else { 125 file = new File (reference); 126 is = new FileInputStream (file); 127 } 128 129 if (extension.equals("htm") || extension.equals("html")) { 130 HTMLReviser rev = new HTMLReviser(); 131 is = rev.revise(file, path, menuid); 132 } 133 response.setHeader("Content-disposition","attachment;filename=" + filename.replaceAll(" ", "_")); 134 PrintWriter pw = response.getWriter(); 135 int letter = 0; 136 while ((letter = is.read()) != -1) { 137 pw.write(letter); 138 } 139 pw.flush(); 140 pw.close(); 141 is.close(); 142 143 actionForward = null; 145 } else 146 actionForward = mapping.findForward("noaccess"); 147 } catch (Exception e) { 148 if (logger.isEnabledFor(Level.ERROR)) 149 logger.error(e.getMessage()); 150 actionForward = mapping.findForward("error"); 151 } 152 } else 153 actionForward = mapping.findForward("invalid"); 154 return actionForward; 155 } 156 } | Popular Tags |