KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > documan > document > ShowDocumentAction


1 /*
2  * ShowDocumentAction.java
3  *
4  * Created on 11. September 2003, 15:40
5  */

6
7 package org.contineo.actions.documan.document;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
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 /**
37  *
38  * @author Michael Scholz
39  */

40 public class ShowDocumentAction extends Action {
41     
42     private InputStream JavaDoc is = null;
43     private File JavaDoc file = null;
44     private String JavaDoc path = "";
45
46     /**
47      * @uml.property name="logger"
48      * @uml.associationEnd
49      */

50     private Logger logger;
51
52     
53     /** Creates a new instance of ShowDocumentAction */
54     public ShowDocumentAction() {
55         logger = LoggingManager.getLogger(this.getClass());
56     }
57
58     /**
59      * This method performs the action.
60      */

61     public ActionForward execute(ActionMapping mapping,
62                     ActionForm form, HttpServletRequest JavaDoc request,
63                     HttpServletResponse JavaDoc response) {
64         ActionForward actionForward = new ActionForward();
65         HttpSession JavaDoc session = request.getSession();
66         if (SessionManagement.isValid(session)) {
67             SettingConfigurator conf = new SettingConfigurator();
68             try {
69                 String JavaDoc menuid = request.getParameter("menuid");
70                 String JavaDoc version = request.getParameter("version");
71                 String JavaDoc reference = request.getParameter("reference");
72                 String JavaDoc userName = (String JavaDoc)session.getAttribute("authuser");
73                 int id = Integer.parseInt(menuid);
74                 //select * from menugroup where menuid=10 and groupname in (select groupname from usergroup where username='admin')
75
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 JavaDoc extension = "";
81                     String JavaDoc filename = "";
82                     String JavaDoc docversion = doc.getDocVersion();
83                     docversion = docversion.trim();
84                     
85                     // store that user has accessed the doc to show it later as a recent document
86
DownloadDocUtil.addToRecentFiles(userName, id);
87                     
88                     // older version
89
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 JavaDoc userdir = conf.getValue("userdir") + "/" + userName + "/temp";
95                             FileBean.createDir(userdir);
96                             extension = doc.getDocType();
97                             if (extension.equals("zip")) {
98                                 // extract version
99
ZipBean.unzip(path + version, userdir);
100                                 path = userdir;
101                             }
102                             if (extension.equals("jar")) {
103                                 // extract version
104
JarBean.unjar(path + version, userdir);
105                                 path = userdir;
106                             }
107                         }
108                         // show version
109
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                     // get the mimetype and set it for the response
118
String JavaDoc mimetype = DownloadDocUtil.getMimeType(menu);
119                     response.setContentType(mimetype);
120
121                     if (reference == null || reference.equals("")) {
122                         file = new File JavaDoc(path + "/" + filename);
123                         is = new FileInputStream JavaDoc(file);
124                     } else {
125                         file = new File JavaDoc(reference);
126                         is = new FileInputStream JavaDoc(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 JavaDoc 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                     // do not forward, because we already sent something to the client
144
actionForward = null;
145                 } else
146                     actionForward = mapping.findForward("noaccess");
147             } catch (Exception JavaDoc 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