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
13
14
15
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
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
38
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
70
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
80
81
82
83
84
85 protected String doTemplateRequest(Melati melati, ServletTemplateContext context)
86 throws Exception {
87 return courteouspoemTemplate(reallyDoTemplateRequest(melati, context));
88 }
89
90
91
92
93
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 }