001: package org.contineo.web.document;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006:
007: import org.contineo.core.security.dao.MenuDAO;
008:
009: import org.contineo.util.Context;
010:
011: import org.contineo.web.SessionManagement;
012: import org.contineo.web.util.Constants;
013:
014: import java.io.IOException;
015: import java.io.PrintWriter;
016:
017: import javax.servlet.ServletException;
018: import javax.servlet.http.HttpServlet;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021: import javax.servlet.http.HttpSession;
022:
023: /**
024: * This servlet is responsible for document downloads. It searches for the
025: * attribute docId in any scope and extracts the proper document's content.
026: *
027: * @author Marco Meschieri
028: * @version $Id: DocumentDownload.java,v 1.5 2006/08/24 08:35:08 marco Exp $
029: * @since 2.6
030: */
031: public class DocumentDownload extends HttpServlet {
032: protected static Log logger = LogFactory
033: .getLog(DocumentDownload.class);
034:
035: /**
036: * Constructor of the object.
037: */
038: public DocumentDownload() {
039: super ();
040: }
041:
042: /**
043: * The doGet method of the servlet. <br>
044: *
045: * This method is called when a form has its tag value method equals to get.
046: *
047: * @param request the request send by the client to the server
048: * @param response the response send by the server to the client
049: * @throws ServletException if an error occurred
050: * @throws IOException if an error occurred
051: */
052: public void doGet(HttpServletRequest request,
053: HttpServletResponse response) throws ServletException,
054: IOException {
055: HttpSession session = request.getSession();
056: String username = (String) session
057: .getAttribute(Constants.AUTH_USERNAME);
058: String id = request.getParameter("menuId");
059:
060: if (StringUtils.isEmpty(id)) {
061: id = (String) request.getAttribute("menuId");
062: }
063:
064: if (StringUtils.isEmpty(id)) {
065: id = (String) session.getAttribute("menuId");
066: }
067:
068: String version = request.getParameter("versionId");
069:
070: if (StringUtils.isEmpty(id)) {
071: version = (String) request.getAttribute("versionId");
072: }
073:
074: if (StringUtils.isEmpty(id)) {
075: version = (String) session.getAttribute("versionId");
076: }
077:
078: logger.debug("Download document menuId=" + id + " " + version);
079:
080: if (SessionManagement.isValid(session)) {
081: try {
082: // if we have access to the document, return it
083: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
084: MenuDAO.class);
085:
086: if (mdao.isReadEnable(Integer.parseInt(id), username)) {
087: DownloadDocUtil.downloadDocument(response, Integer
088: .parseInt(id), version);
089:
090: // add the file to the recent files of the user
091: DownloadDocUtil.addToRecentFiles(username, Integer
092: .parseInt(id));
093: }
094: } catch (Exception ex) {
095: logger.error(ex.getMessage(), ex);
096: }
097: }
098: }
099:
100: /**
101: * The doPost method of the servlet. <br>
102: *
103: * This method is called when a form has its tag value method equals to
104: * post.
105: *
106: * @param request the request send by the client to the server
107: * @param response the response send by the server to the client
108: * @throws ServletException if an error occurred
109: * @throws IOException if an error occurred
110: */
111: public void doPost(HttpServletRequest request,
112: HttpServletResponse response) throws ServletException,
113: IOException {
114: response.setContentType("text/html");
115:
116: PrintWriter out = response.getWriter();
117: out
118: .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
119: out.println("<HTML>");
120: out
121: .println(" <HEAD><TITLE>Download Document Servlet</TITLE></HEAD>");
122: out.println(" <BODY>");
123: out.print(" This is ");
124: out.print(this .getClass());
125: out.println(", using the POST method");
126: out.println(" </BODY>");
127: out.println("</HTML>");
128: out.flush();
129: out.close();
130: }
131: }
|