001 package net.sf.jolene.servlet; 002 003 import net.sf.jolene.util.PrefsReader; 004 import net.sf.jolene.dom.Document; 005 import net.sf.jolene.factories.DocumentFactory; 006 import org.apache.log4j.LogManager; 007 import org.apache.log4j.Logger; 008 009 import javax.servlet.ServletException; 010 import javax.servlet.http.HttpServlet; 011 import javax.servlet.http.HttpServletRequest; 012 import javax.servlet.http.HttpServletResponse; 013 import java.io.IOException; 014 015 /** 016 * @author Dan Howard 017 * @since Aug 7, 2005 8:21:03 AM 018 */ 019 public class Domlet extends HttpServlet { 020 021 private static final Logger log = LogManager.getLogger(Domlet.class); 022 023 /** 024 * @param request HttpServletRequest 025 * @param response HttpServletResponse 026 * @throws ServletException 027 * @throws IOException 028 */ 029 @Override 030 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 031 handleRequest(request, response); 032 } 033 034 /** 035 * @param request HttpServletRequest 036 * @param response HttpServletResponse 037 * @throws ServletException 038 * @throws IOException 039 */ 040 @Override 041 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 042 handleRequest(request, response); 043 } 044 045 /** 046 * @throws ServletException 047 */ 048 @Override 049 public void init() throws ServletException { 050 // Initialize PrefsReader 051 log.info("Domlet Initializing PrefsReader..."); 052 PrefsReader.init(); 053 } 054 055 /** 056 * Basic request handler for servlet requests. Looks at the the request or session to locate the 057 * document object to stream out the response writer. 058 * 059 * @param request HttpServletRequest 060 * @param response HttpServletResponse 061 * @throws IOException if an IOException occurs 062 */ 063 protected static void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { 064 065 log.debug("handle request"); 066 log.debug("URI: " + request.getRequestURI()); 067 log.debug("URL: " + request.getRequestURL()); 068 log.debug("CP : " + request.getContextPath()); 069 log.debug("SP : " + request.getServletPath()); 070 071 // look 1st for the document in the request scope 072 // otherwise look in the session scope 073 // if none found log warn and add to request scope 074 // The key is the URI minus the context path 075 String uri = request.getRequestURI(); 076 if (request.getContextPath().length() > 0) { 077 if (uri.startsWith(request.getContextPath())) { 078 int n; 079 n = uri.indexOf("/", 1); 080 if (n > -1) { 081 uri = uri.substring(n); 082 } 083 } 084 } 085 086 Document document; 087 if (request.getAttribute(uri) != null) { 088 document = (Document) request.getAttribute(uri); 089 } else { 090 document = (Document) request.getSession().getAttribute(uri); 091 } 092 093 if (document == null) { 094 log.warn("No document found at request or session level for url " + uri + " attaching a new document to the request."); 095 document = DocumentFactory.getInstance().getDocument(request.getSession().getServletContext().getRealPath(uri), request.getContextPath(), uri); 096 } 097 log.debug("DOC URI: " + document.getUri()); 098 099 document.stream(response.getWriter()); 100 } 101 102 103 }