1
2
3
4
5
6
7
8
9
10
11 package org.paneris.melati.site;
12
13 import java.io.File;
14 import java.io.IOException;
15
16 import javax.servlet.ServletException;
17
18 import org.melati.Melati;
19 import org.melati.PoemContext;
20 import org.melati.poem.Column;
21 import org.melati.poem.Persistent;
22 import org.melati.poem.Table;
23 import org.melati.servlet.PathInfoException;
24 import org.melati.servlet.TemplateServlet;
25 import org.melati.template.ServletTemplateContext;
26 import org.melati.util.StringUtils;
27
28
29
30
31
32 public abstract class SiteServlet extends TemplateServlet {
33
34
35 public static final boolean DEBUG = true;
36
37 private static final String STATIC_ROOT = "/dist/MelatiSite/src/main/webapp";
38
39 private static final String DB_NAME = "site";
40
41
42
43
44
45
46 protected void doConfiguredRequest(final Melati melati)
47 throws ServletException, IOException {
48 String pathInfo = getPathInfo(melati);
49 System.err.println("pathinfo:" + pathInfo);
50
51
52
53 while (pathInfo != "" && !fileAt(pathInfo)) {
54 String s = pathInfo.substring(1);
55 int i = s.indexOf('/');
56 if (i == -1)
57 pathInfo = "";
58 else
59 pathInfo = s.substring(i);
60 }
61
62 if (pathInfo != "") {
63 System.err.println("pathinfo:" + pathInfo);
64 System.err.println("Ref:" + melati.getRequest().getHeader("Referer"));
65 StringBuffer url = new StringBuffer();
66 String scheme = melati.getRequest().getScheme();
67 url.append(scheme);
68 url.append("://");
69 url.append(melati.getRequest().getServerName());
70 url.append(pathInfo);
71
72
73
74 melati.getResponse().sendRedirect(url.toString());
75 return;
76
77 }
78 super.doConfiguredRequest(melati);
79 melati.setResponseContentType("text/html");
80 }
81
82 protected boolean fileAt(String filename) {
83 if (filename.equals(""))
84 return false;
85 if (filename.equals("/"))
86 return false;
87 String fsName = getSTATIC_ROOT() + filename;
88 File it = new File(fsName);
89 System.err.println("FS:" + fsName + " " + it.exists());
90 return it.exists();
91 }
92
93
94
95
96
97
98
99
100 protected String doTemplateRequest(Melati melati,
101 ServletTemplateContext context) throws Exception {
102 return melati.templateName(reallyDoTemplateRequest(melati, context));
103 }
104
105
106
107
108
109
110 protected abstract String reallyDoTemplateRequest(Melati melati,
111 ServletTemplateContext templateContext) throws Exception;
112
113 protected String getSetting(Melati melati, String settingName) {
114 String returnString = melati.getDatabase().getSettingTable().get(
115 settingName);
116 if (returnString == null)
117 throw new RuntimeException("Setting " + settingName
118 + " not found in setting table");
119 return returnString;
120 }
121
122
123
124
125
126
127 protected PoemContext poemContext(Melati melati) throws PathInfoException {
128
129 PoemContext it = new PoemContext();
130 it.setLogicalDatabase(getDB_NAME());
131
132 String initParameterPathInfo = getInitParameter("pathinfo");
133 String[] parts;
134 if (initParameterPathInfo != null) {
135 parts = StringUtils.split(initParameterPathInfo, '/');
136 System.err.println("Got init param:" + initParameterPathInfo);
137 } else {
138 System.err.println("Did not find init param");
139 parts = melati.getPathInfoParts();
140 }
141 if (parts.length > 0) {
142
143
144
145 if (parts.length == 1)
146 it.setMethod(parts[0]);
147
148
149
150
151 if (parts.length == 2) {
152 String r = parts[1];
153 if (r.lastIndexOf(".htm") != -1) {
154 r = r.substring(0, r.lastIndexOf(".htm"));
155
156 }
157 it.setTable(parts[0]);
158 try {
159 it.setTroid(new Integer(r));
160 } catch (NumberFormatException e) {
161 it.setMethod(parts[1]);
162 }
163 }
164
165
166
167 if (parts.length == 3) {
168 it.setTable(parts[0]);
169 try {
170 it.setTroid(new Integer(parts[1]));
171 } catch (NumberFormatException e) {
172 String r = parts[1];
173 if (r.lastIndexOf(".htm") != -1) {
174 r = r.substring(0, r.lastIndexOf(".htm"));
175 }
176 String value = StringUtils.tr(r, '.', ' ');
177 value = StringUtils.tr(r, '_', ' ');
178 Table t = melati.getTable();
179 if (t != null) {
180 Column c = t.primaryCriterionColumn();
181 if (c == null)
182 throw new NullPointerException("primaryCriterionColumn null");
183 Persistent o = c.firstWhereEq(value);
184 if (o != null)
185 it.setTroid(o.troid());
186 }
187 }
188 it.setMethod(parts[2]);
189 }
190
191
192
193
194
195
196
197
198
199
200
201 }
202 return it;
203 }
204
205
206
207
208
209
210
211
212
213 public static class Util {
214
215
216
217
218
219
220
221
222 public String contrastingColour(String rgb) {
223 if (rgb.length() != 6)
224 throw new IllegalArgumentException();
225 String red = rgb.substring(0, 2);
226 String green = rgb.substring(2, 4);
227 String blue = rgb.substring(4);
228
229 int redI = Integer.parseInt(red, 16);
230 int greenI = Integer.parseInt(green, 16);
231 int blueI = Integer.parseInt(blue, 16);
232 int luminence = redI + greenI + blueI;
233 int redNew;
234 int greenNew;
235 int blueNew;
236 if (luminence > ((3 * 255) / 2)) {
237
238
239
240
241
242
243 if (redI > blueI) {
244 if (blueI > greenI) {
245 redNew = (greenI / 2);
246 greenNew = (redI / 2);
247 blueNew = (blueI / 2);
248 } else {
249 if (redI > greenI) {
250 redNew = (blueI / 2);
251 greenNew = (greenI / 2);
252 blueNew = (redI / 2);
253 } else {
254 redNew = (redI / 2);
255 greenNew = (blueI / 2);
256 blueNew = (greenI / 2);
257 }
258 }
259 } else {
260 if (redI > greenI) {
261 redNew = (redI / 2);
262 greenNew = (blueI / 2);
263 blueNew = (greenI / 2);
264 } else {
265 if (blueI > greenI) {
266 redNew = (blueI / 2);
267 greenNew = (greenI / 2);
268 blueNew = (redI / 2);
269 } else {
270 redNew = (greenI / 2);
271 greenNew = (redI / 2);
272 blueNew = (blueI / 2);
273 }
274 }
275 }
276 } else {
277 if (redI > blueI) {
278 if (blueI > greenI) {
279 redNew = 255 - (greenI / 2);
280 greenNew = 255 - (redI / 2);
281 blueNew = 255 - (blueI / 2);
282 } else {
283 if (redI > greenI) {
284 redNew = 255 - (blueI / 2);
285 greenNew = 255 - (greenI / 2);
286 blueNew = 255 - (redI / 2);
287 } else {
288 redNew = 255 - (redI / 2);
289 greenNew = 255 - (blueI / 2);
290 blueNew = 255 - (greenI / 2);
291 }
292 }
293 } else {
294 if (redI > greenI) {
295 redNew = 255 - (redI / 2);
296 greenNew = 255 - (blueI / 2);
297 blueNew = 255 - (greenI / 2);
298 } else {
299 if (blueI > greenI) {
300 redNew = 255 - (blueI / 2);
301 greenNew = 255 - (greenI / 2);
302 blueNew = 255 - (redI / 2);
303 } else {
304 redNew = 255 - (greenI / 2);
305 greenNew = 255 - (redI / 2);
306 blueNew = 255 - (blueI / 2);
307 }
308 }
309 }
310 }
311 String redNewS = (redNew < 16) ? "0" + Integer.toHexString(redNew)
312 : Integer.toHexString(redNew);
313 String greenNewS = (greenNew < 16) ? "0" + Integer.toHexString(greenNew)
314 : Integer.toHexString(greenNew);
315 String blueNewS = (blueNew < 16) ? "0" + Integer.toHexString(blueNew)
316 : Integer.toHexString(blueNew);
317
318 return redNewS + greenNewS + blueNewS;
319 }
320 }
321
322
323
324
325 public String getDB_NAME() {
326 return DB_NAME;
327 }
328
329
330
331
332 public String getSTATIC_ROOT() {
333 return STATIC_ROOT;
334 }
335
336 private String getPathInfo(Melati melati) {
337 String initParameterPathInfo = getInitParameter("pathinfo");
338 String pathInfo = melati.getRequest().getPathInfo();
339 if (initParameterPathInfo == null) {
340 return pathInfo == null ? "" : pathInfo;
341 } else {
342 return initParameterPathInfo + (pathInfo == null ? "" : pathInfo);
343 }
344
345 }
346
347 }