Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/view/servlet/BaseServlet.java


1   
2   package com.eireneh.bible.view.servlet;
3   
4   import java.io.*;
5   import java.net.*;
6   import java.util.*;
7   
8   import javax.servlet.*;
9   import javax.servlet.http.*;
10  
11  import org.w3c.dom.*;
12  import org.xml.sax.*;
13  import org.apache.xerces.dom.DocumentImpl;
14  
15  import com.eireneh.util.*;
16  import com.eireneh.mail.*;
17  import com.eireneh.config.*;
18  import com.eireneh.config.servlet.*;
19  import com.eireneh.util.config.*;
20  import com.eireneh.mail.config.*;
21  
22  import com.eireneh.bible.util.*;
23  import com.eireneh.bible.util.config.*;
24  import com.eireneh.bible.control.*;
25  import com.eireneh.bible.book.config.*;
26  import com.eireneh.bible.book.raw.config.*;
27  import com.eireneh.bible.book.sword.config.*;
28  
29  /**
30   * The base servlet class that makes it easier for other servets and
31   * promotes the use of XML.
32   *
33   * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
34   * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
35   * Distribution Licence:<br />
36   * Project B is free software; you can redistribute it
37   * and/or modify it under the terms of the GNU General Public License,
38   * version 2 as published by the Free Software Foundation.<br />
39   * This program is distributed in the hope that it will be useful,
40   * but WITHOUT ANY WARRANTY; without even the implied warranty of
41   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42   * General Public License for more details.<br />
43   * The License is available on the internet
44   * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
45   * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
46   * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
47   * The copyright to this program is held by it's authors.
48   * </font></td></tr></table>
49   * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
50   * @see docs.Licence
51   * @author Joe Walker
52   */
53  public abstract class BaseServlet extends HttpServlet
54  {
55      /**
56       * Get the Config class to set up up
57       */
58      public static Config getConfig()
59      {
60          Config config = new Config("Servlet");
61  
62          config.add("Bibles.Cache-Versions", new CacheBiblesChoice());
63          config.add("Bibles.Raw.Cache-Data", new CacheDataChoice());
64          config.add("Bibles.Sword.Base Directory", new SwordDirChoice());
65  
66          config.add("Mail.To", new MailCaptureChoices.EMailToAddrChoice());
67          config.add("Mail.From", new MailCaptureChoices.EMailFromAddrChoice());
68          config.add("Mail.Server", new MailCaptureChoices.EMailServerChoice());
69  
70          config.add("Capture.Mail", new MailCaptureChoices.CaptureEMailChoice());
71          config.add("Capture.Log", new LogCaptureChoice());
72          config.add("Capture.StdOut", new StdOutCaptureInformChoice());
73  
74          config.add("Log.File", new FileChoice());
75          config.add("Log.StdOut", new StdOutCaptureLogChoice());
76  
77          config.add("Source.JavaDoc", new JavaDocLinkChoice());
78  
79          config.add("Advanced.Available Drivers", new DriversChoice());
80  
81          return config;
82      }
83  
84      /**
85       * Create a Bible to read references from
86       */
87      public void init(ServletConfig config) throws ServletException, UnavailableException
88      {
89          super.init(config);
90  
91          try
92          {
93              ServletContext context = config.getServletContext();
94              String init0 = (String) context.getAttribute("init");
95              log.fine("init0="+init0);
96  
97              Enumeration en = config.getInitParameterNames();
98              while (en.hasMoreElements())
99              {
100                 String key = (String) en.nextElement();
101                 String val = config.getInitParameter(key);
102                 log.fine(key+"="+val);
103             }
104 
105             String init = config.getInitParameter("init");
106             com.eireneh.bible.util.Project.init(init);
107 
108             URL config_url = NetUtil.lengthenURL(Project.getConfigRoot(), "Servlet.properties");
109 
110             Config conf = getConfig();
111             conf.permanentToLocal(config_url);
112             conf.localToApplication(true);
113         }
114         catch (Throwable ex)
115         {
116             Reporter.informUser(BaseServlet.class, ex);
117         }
118 
119         log.fine("init called on "+getClass().getName());
120     }
121 
122     /**
123      * Some basic info about what is going on
124      */
125     public String getServletInfo()
126     {
127         return "Project B";
128     }
129 
130     /**
131      * Enter elements into an XML Document that can be transformed into
132      * an HTML document using XSL
133      * @param state data about the current request
134      * @param node The place to start adding data
135      * @param request A description of the request
136      */
137     public abstract void generateData(State state, Node node, HttpServletRequest request) throws Exception;
138 
139     /**
140      * Respond to a GET request
141      * @param request A description of the request
142      * @param response Data on the reply
143      */
144     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
145     {
146         State state = new CookieState(request, response);
147         response.setContentType("text/html");
148         PrintWriter out = response.getWriter();
149 
150         try
151         {
152             logAccess(request);
153 
154             Document doc = new DocumentImpl();
155             generateData(state, doc, request);
156 
157             String name = state.getWebStyleName();
158             out.println(state.getWebStyle().applyStyleString(doc, name));
159 
160             if (state.isDebugging())
161                 debug(request, response, state, out);
162         }
163         catch (Throwable ex)
164         {
165             if (ex instanceof ThreadDeath)
166                 throw (ThreadDeath) ex;
167 
168             logError(request, ex);
169 
170             out.println("<html><body>");
171 
172             // The exception
173             out.println("<h3>Exception</h3>");
174             out.println("<pre>");
175             ex.printStackTrace(out);
176             if (ex instanceof SAXParseException)
177             {
178                 SAXParseException spex = (SAXParseException) ex;
179                 out.println("Location: line="+spex.getLineNumber()+", col="+spex.getColumnNumber());
180                 out.println("Public ID="+spex.getSystemId()+" System ID="+spex.getSystemId());
181             }
182             out.println("</pre>");
183 
184             debug(request, response, state, out);
185         }
186     }
187 
188     /**
189      * Respond to a POST request
190      * @param request A description of the request
191      * @param response Data on the reply
192      */
193     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
194     {
195         doGet(request, response);
196     }
197 
198     /**
199      * Debug output to the web page
200      */
201     private void debug(HttpServletRequest request, HttpServletResponse response, State state, PrintWriter out) throws IOException
202     {
203         // The cookies
204         out.println("<h3>Cookies</h3>");
205         Cookie[] cookies = request.getCookies();
206         for (int i = 0; i < cookies.length; i++)
207         {
208             out.println(cookies[i].getName()+"="+cookies[i].getValue()+"<br>");
209         }
210 
211         // The headers
212         out.println("<h3>Headers</h3>");
213         Enumeration en = request.getHeaderNames();
214         while (en.hasMoreElements())
215         {
216             String name = (String) en.nextElement();
217             out.println(name+"="+request.getHeader(name)+"<br>");
218         }
219 
220         // The parameters
221         out.println("<h3>Parameters</h3>");
222         en = request.getParameterNames();
223         while (en.hasMoreElements())
224         {
225             String name = (String) en.nextElement();
226             out.println(name+"="+request.getParameter(name)+"<br>");
227         }
228 
229         // The request info
230         out.println("<h3>Request Info</h3>");
231         out.println("getMethod="+request.getMethod()+"<br>");
232         out.println("getRequestURI="+request.getRequestURI()+"<br>");
233         out.println("getProtocol="+request.getProtocol()+"<br>");
234         out.println("getPathInfo="+request.getPathInfo()+"<br>");
235         out.println("getRemoteAddr="+request.getRemoteAddr()+"<br>");
236         out.println("getRemoteUser="+request.getRemoteUser()+"<br>");
237 
238         // The cookie state
239         out.println("<h3>Cookie Properties</h3>");
240         out.println("<pre>");
241         out.println(state.getDebugString());
242         out.println("</pre>");
243         out.println("</body></html>");
244     }
245 
246     /**
247      * Log an access
248      * @param state data about the current request
249      */
250     public void logAccess(HttpServletRequest request) throws IOException
251     {
252         log.info(request.getRemoteAddr() + "\t" +
253                  request.getRequestURI() + "\t" +
254                  request.getHeader("Referer"));
255     }
256 
257     /**
258      * Log an error
259      * @param state data about the current request
260      */
261     public void logError(HttpServletRequest request, Throwable ex) throws IOException
262     {
263         logAccess(request);
264         Reporter.informUser(this, ex);
265     }
266 
267     /** The log stream */
268     protected static Logger log = Logger.getLogger("view.servlet");
269 }