View Javadoc
1   
2   package org.melati.courteouspoem;
3   
4   import java.io.File;
5   import java.io.IOException;
6   
7   import javax.servlet.ServletException;
8   
9   import org.melati.Melati;
10  import org.melati.servlet.TemplateServlet;
11  import org.melati.template.ServletTemplateContext;
12  //import org.melati.courteouspoem.poem.CourteouspoemDatabase;
13  
14  /**
15   * Base servlet for Courteouspoem servlets.
16   */
17  public abstract class CourteouspoemServlet extends TemplateServlet {
18  
19  
20    public static final String templatePrefix = "org/melati/courteouspoem/view/";
21  
22    public String getSysAdminName () {
23      return "TimP";
24    }
25    public String getSysAdminEmail () {
26      return "timp@paneris.org";
27    }
28  
29    /**
30     * @see org.melati.servlet.ConfigServlet#doConfiguredRequest(org.melati.Melati)
31     */
32    protected void doConfiguredRequest(final Melati melati)
33        throws ServletException, IOException {
34      String pathInfo = melati.getRequest().getPathInfo();
35      if (pathInfo == null) pathInfo = "";
36  
37      // check if pathinfo exists in filesystem
38      // if so then redirect to it, unless we came from there
39      while (pathInfo != "" && !fileAt(pathInfo)) {
40        String s = pathInfo.substring(1);
41        int i = s.indexOf('/');
42        if (i != -1)
43          pathInfo = s.substring(i);
44        else
45          pathInfo = "";
46      }
47      if (pathInfo != "") {
48        String referer = melati.getRequest().getHeader("Referer");
49        if (referer != null &&
50            referer.indexOf(pathInfo) == -1) {
51          melati.getResponse().sendRedirect(pathInfo);
52          return;
53        }
54      }
55      super.doConfiguredRequest(melati);
56    }
57    private boolean fileAt(String filename){
58      if (filename.equals("")) return false;
59      if (filename.equals("/")) return false;
60      String fsName = "/dist/courteouspoem/www" + filename;
61      File it = new File(fsName);
62      return it.exists();
63    }
64  
65    public String courteouspoemTemplate(String name) {
66      return addExtension(templatePrefix + name);
67    }
68  
69      // Override org.melati.TemplateServlet.addExtension()
70      // to cope with heterogenous naming convention :)
71    protected String addExtension(String templateName) {
72      int index = templateName.indexOf(".wm");
73      if (index == -1)
74        templateName = templateName + ".wm";
75      return templateName;
76    }
77  
78    /**
79     * Concrete class for {@link TemplateServlet}.
80     *
81     * @param melati
82     * @param context
83     * @return Template name
84     */
85    protected String doTemplateRequest(Melati melati, ServletTemplateContext context)
86        throws Exception {
87      return courteouspoemTemplate(reallyDoTemplateRequest(melati, context));
88    }
89  
90    /**
91     * Override this method to build up output in individual servlets.
92     *
93     * @return Template name without path or extension
94     */
95    protected abstract String
96      reallyDoTemplateRequest(Melati melati,
97                              ServletTemplateContext templateContext)
98        throws Exception;
99  
100   protected String getSetting(Melati melati, String settingName) {
101     String returnString = melati.getDatabase().getSettingTable().get(settingName);
102     if (returnString == null)
103       throw new RuntimeException("Setting " + settingName + " not found in setting table");
104     return returnString;
105   }
106 
107 }