1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.webmacro.servlet;
25
26 import org.webmacro.*;
27 import org.webmacro.util.LogSystem;
28
29 import javax.servlet.ServletConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServlet;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpSession;
35
36 import java.io.*;
37 import java.lang.reflect.Method;
38 import java.util.Locale;
39
40
41 import org.paneris.jal.model.DDRecord;
42 import org.paneris.jal.model.SystemProperties;
43 import org.paneris.paneris.model.PageContent;
44 import org.paneris.util.ContextUtil;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public abstract class PanerisPage extends HttpServlet implements WebMacro
76 {
77
78 private WebMacro _wm = null;
79 private Broker _broker = null;
80 private boolean _started = false;
81
82
83
84
85 final static String ERROR_VARIABLE = "ErrorVariable";
86
87
88
89
90
91 final static String ERROR_TEMPLATE = "ErrorTemplate";
92
93
94
95
96 final static String ERROR_TEMPLATE_DEFAULT = "error.wm";
97 final static String ERROR_VARIABLE_DEFAULT = "error";
98
99
100
101
102 protected Log _log;
103
104
105
106
107 private String _problem = "Not yet initialized: Your servlet API tried to access WebMacro without first calling init()!!!";
108
109
110
111
112
113
114 public synchronized void init (ServletConfig sc)
115 throws ServletException
116 {
117 super.init(sc);
118 init();
119 }
120
121
122
123
124
125
126
127 public synchronized void init ()
128 {
129
130 if (_started)
131 {
132 return;
133 }
134
135
136
137 if (_wm == null)
138 {
139 try
140 {
141 _wm = initWebMacro();
142 _broker = _wm.getBroker();
143 }
144 catch (InitException e)
145 {
146 _problem = "Could not initialize the broker!\n\n"
147 + "*** Check that WebMacro.properties was in your servlet\n"
148 + "*** classpath, in a similar place to webmacro.jar \n"
149 + "*** and that all values were set correctly.\n\n"
150 + e.getMessage();
151 Log sysLog = LogSystem.getSystemLog("servlet");
152 sysLog.error(_problem, e);
153 return;
154 }
155 }
156 _log = _broker.getLog("servlet", "WMServlet lifecycle information");
157
158 try
159 {
160 if (_log.loggingDebug())
161 {
162 java.net.URL url = getBroker().getResource(Broker.WEBMACRO_PROPERTIES);
163 if (url != null)
164 _log.debug("Using properties from " + url.toExternalForm());
165 else
166 _log.debug("No WebMacro.properties file was found.");
167 }
168 start();
169 _problem = null;
170 }
171 catch (ServletException e)
172 {
173 _problem = "WebMacro application code failed to initialize: \n"
174 + e + "\n" + "This error is the result of a failure in the\n"
175 + "code supplied by the application programmer.\n";
176 _log.error(_problem, e);
177 }
178 _log.notice("started: " + this);
179 _started = true;
180
181 }
182
183
184
185
186
187
188
189 public synchronized void destroy ()
190 {
191 stop();
192 _log.notice("stopped: " + this);
193 _wm = null;
194 _started = false;
195 super.destroy();
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 protected void doGet (HttpServletRequest req, HttpServletResponse resp)
213 throws ServletException, IOException
214 {
215 doRequest(req, resp);
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230 protected void doPost (HttpServletRequest req, HttpServletResponse resp)
231 throws ServletException, IOException
232 {
233 doRequest(req, resp);
234 }
235
236 final private void doRequest (
237 HttpServletRequest req, HttpServletResponse resp)
238 throws IOException
239 {
240
241 WebContext context = null;
242
243 if (_problem != null)
244 {
245 init();
246 if (_problem != null)
247 {
248 try
249 {
250 resp.setContentType("text/html");
251 Writer out = resp.getWriter();
252
253 out.write("<html><head><title>WebMacro Error</title></head>");
254 out.write("<body><h1><font color=\"red\">WebMacro Error: ");
255 out.write("</font></h1><pre>");
256 out.write(_problem);
257 out.write("</pre>");
258 out.write("Please contact the server administrator");
259 out.flush();
260 out.close();
261 }
262 catch (Exception e)
263 {
264 _log.error(_problem, e);
265 }
266 return;
267 }
268 }
269
270
271 context = newWebContext(req, resp);
272 try
273 {
274
275 doPaneris(context);
276
277 Template t;
278 t = handle(context);
279
280 if (t != null)
281 {
282 execute(t, context);
283 }
284 destroyContext(context);
285 }
286 catch (HandlerException e)
287 {
288 _log.error("Your handler failed to handle the request:" + this, e);
289 Template tmpl = error(context,
290 "Your handler was unable to process the request successfully " +
291 "for some reason. Here are the details:<p>" +
292 "<pre>" + e + "</pre>");
293 execute(tmpl, context);
294 }
295 catch (Exception e)
296 {
297 _log.error("Your handler failed to handle the request:" + this, e);
298 Template tmpl = error(context,
299 "The handler WebMacro used to handle this request failed for " +
300 "some reason. This is likely a bug in the handler written " +
301 "for this application. Here are the details:<p>" +
302 "<pre>" + e + "</pre>");
303 execute(tmpl, context);
304 }
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318 protected Template error (WebContext context, String error)
319 {
320 Template tmpl = null;
321
322 try
323 {
324 context.put(getErrorVariableName(),
325 error);
326
327 tmpl = getErrorTemplate();
328 }
329 catch (Exception e2)
330 {
331 _log.error("Unable to use ErrorHandler", e2);
332 }
333 return tmpl;
334 }
335
336
337
338
339
340 protected String getErrorVariableName ()
341 {
342 return getConfig(ERROR_VARIABLE, ERROR_VARIABLE_DEFAULT);
343 }
344
345
346
347
348
349
350
351
352
353 public Broker getBroker ()
354 {
355
356
357
358
359
360 return _broker;
361 }
362
363
364
365
366
367
368
369
370 public Log getLog (String type, String description)
371 {
372 return _broker.getLog(type, description);
373 }
374
375
376
377
378
379
380 public Log getLog (String type)
381 {
382 return _broker.getLog(type, type);
383 }
384
385
386
387
388
389
390
391 public Template getTemplate (String key)
392 throws ResourceException
393 {
394 return _wm.getTemplate(key);
395 }
396
397
398
399
400
401
402
403 public String getURL (String url)
404 throws ResourceException
405 {
406 return _wm.getURL(url);
407 }
408
409
410
411
412
413
414
415 public String getConfig (String key)
416 throws NotFoundException
417 {
418 return _wm.getConfig(key);
419 }
420
421
422
423
424
425 public String getConfig (String key, String defaultValue)
426 {
427 try
428 {
429 return _wm.getConfig(key);
430 }
431 catch (NotFoundException e)
432 {
433 return defaultValue;
434 }
435 }
436
437
438
439
440 public Context getContext ()
441 {
442 return _wm.getContext();
443 }
444
445
446
447
448 public WebContext getWebContext (HttpServletRequest req, HttpServletResponse res)
449 {
450 return _wm.getWebContext(req, res);
451 }
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 public void writeTemplate (String templateName, java.io.OutputStream out,
473 Context context)
474 throws java.io.IOException, ResourceException, PropertyException
475 {
476
477 writeTemplate(templateName, out,
478 getConfig(WMConstants.TEMPLATE_OUTPUT_ENCODING),
479 context);
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501 public void writeTemplate (String templateName, java.io.OutputStream out,
502 String encoding, Context context)
503 throws java.io.IOException, ResourceException, PropertyException
504 {
505
506 if (encoding == null)
507 encoding = getConfig(WMConstants.TEMPLATE_OUTPUT_ENCODING);
508
509 Template tmpl = getTemplate(templateName);
510 tmpl.write(out, encoding, context);
511 }
512
513
514
515
516
517
518
519
520
521 protected void execute (Template tmpl, WebContext c)
522 throws IOException
523 {
524 try
525 {
526 HttpServletResponse resp = c.getResponse();
527
528 Locale locale = (Locale) tmpl.getParam(
529 WMConstants.TEMPLATE_LOCALE);
530 if (_log.loggingDebug())
531 _log.debug("TemplateLocale=" + locale);
532 if (locale != null)
533 {
534 setLocale(resp, locale);
535 }
536
537 String encoding = (String) tmpl.getParam(
538 WMConstants.TEMPLATE_OUTPUT_ENCODING);
539 if (encoding == null)
540 {
541 encoding = resp.getCharacterEncoding();
542 }
543
544 if (_log.loggingDebug())
545 _log.debug("Using output encoding " + encoding);
546
547
548
549
550
551 byte[] bytes = tmpl.evaluateAsBytes(encoding, c);
552
553
554 writeResponseBytes(resp, bytes, encoding);
555 }
556 catch (UnsupportedEncodingException e)
557 {
558
559
560
561 _log.error("tried to use an unsupported encoding", e);
562 throw e;
563 }
564 catch (IOException e)
565 {
566
567 }
568 catch (Exception e)
569 {
570 String error =
571 "WebMacro encountered an error while executing a template:\n"
572 + ((tmpl != null) ? (tmpl + ": " + e + "\n") :
573 ("The template failed to load; double check the "
574 + "TemplatePath in your webmacro.properties file."));
575 _log.error(error, e);
576 try
577 {
578 Template errorTemplate = error(c,
579 "WebMacro encountered an error while executing a template:\n"
580 + ((tmpl != null) ? (tmpl + ": ")
581 : ("The template failed to load; double check the "
582 + "TemplatePath in your webmacro.properties file."))
583 + "\n<pre>" + e + "</pre>\n");
584
585 String err = errorTemplate.evaluateAsString(c);
586 c.getResponse().getWriter().write(err);
587 }
588 catch (Exception errExcept)
589 {
590 _log.error("Error writing error template!", errExcept);
591 }
592 }
593 }
594
595
596
597
598
599
600
601
602
603 private void writeResponseBytes (HttpServletResponse response, byte[] bytes, String encoding)
604 throws IOException
605 {
606 OutputStream out;
607
608 try
609 {
610 out = response.getOutputStream();
611 }
612 catch (IllegalStateException e)
613 {
614
615
616
617
618
619
620
621
622 out = null;
623 _log.debug("Using Writer instead of OutputStream");
624 }
625 response.setContentLength(bytes.length);
626 if (out != null)
627 {
628 out.write(bytes);
629 }
630 else
631 {
632 response.getWriter().write(new String(bytes, encoding));
633 }
634 }
635
636
637
638
639
640
641
642
643
644
645
646
647
648 public WebContext newContext (
649 HttpServletRequest req, HttpServletResponse resp)
650 throws HandlerException
651 {
652 return _wm.getWebContext(req, resp);
653
654 }
655
656
657
658
659
660
661
662
663
664 public abstract Template handle (WebContext context)
665 throws HandlerException;
666
667
668
669
670
671
672
673
674
675 public void destroyContext (WebContext wc)
676 throws HandlerException
677 {
678 }
679
680
681
682
683
684
685
686
687
688
689 protected void start () throws ServletException
690 {
691 }
692
693
694
695
696
697
698
699 protected void stop ()
700 {
701 }
702
703
704
705
706
707
708
709
710
711 public WebMacro initWebMacro () throws InitException
712 {
713 return new WM(this);
714 }
715
716
717
718
719
720
721
722 public final WebContext initWebContext () throws InitException
723 {
724 return null;
725 }
726
727 public WebContext newWebContext(HttpServletRequest req, HttpServletResponse resp) {
728 return new WebContext(_broker, req, resp);
729 }
730
731
732
733
734
735 protected void setLocale (HttpServletResponse resp, Locale locale)
736 {
737 try
738 {
739 Method m = HttpServletResponse.class.getMethod(
740 "setLocale",
741 new Class[]
742 {Locale.class});
743 m.invoke(resp, new Locale[]
744 {locale});
745 if (_log.loggingDebug())
746 _log.debug("Successfully set locale to " + locale);
747 }
748 catch (Exception e)
749 {
750 if (_log.loggingDebug())
751 _log.debug("Error set locale to " + locale + ": " + e.getClass());
752 }
753 }
754
755
756
757
758
759
760
761
762
763
764 public FastWriter getFastWriter (OutputStream out, String enctype)
765 throws UnsupportedEncodingException
766 {
767 return _wm.getFastWriter(out, enctype);
768 }
769
770
771 private void doPaneris(WebContext context) {
772
773 context.put("util", new ContextUtil());
774
775 String db = context.getForm("db");
776
777
778 if (db == null) db = "paneris";
779 HttpSession session = context.getSession();
780 org.paneris.user.model.User user =
781 (org.paneris.user.model.User)session.getAttribute(db + "user");
782
783
784
785 PageContent content = new PageContent(user);
786 context.put("user", user);
787 context.put("content", content);
788
789
790 if (session.getAttribute(db + "stylepath") == null) {
791 String stylepath = "";
792
793 DDRecord styling = null;
794 try {
795
796 if (user != null) {
797 styling = user.getField("style").getLookup();
798 }
799 if (styling == null || styling.getFieldValue("path").equals("")) {
800
801 String stylingid = "";
802 try {
803 SystemProperties sp = new SystemProperties(db);
804 stylingid = sp.getProperty("defaultstyle");
805 }
806 catch (Exception e) {
807 System.err.println("defaultstyle not set in " + db +"System Properties table");
808 }
809 if (!stylingid.equals("")) {
810 styling = new DDRecord(db, "styles", new Integer(stylingid));
811 }
812 }
813 }
814 catch (Exception e) {
815 ;
816 }
817
818 if (styling != null) {
819 stylepath = styling.getFieldValue("path");
820 }
821
822 session.setAttribute(db + "stylepath", stylepath);
823 }
824
825 context.put("stylepath", session.getAttribute(db + "stylepath"));
826 }
827
828
829 private static final String DEFAULT_ERROR_TEXT =
830 "<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n"
831 + "#set $Response.ContentType = \"text/html\"\n"
832 + "<BODY><H1>Error</H1>"
833 + "<HR>$error</BODY></HTML>";
834
835 private Template _errorTemplate = null;
836
837
838
839
840
841
842
843 public Template getErrorTemplate ()
844 {
845 String templateName = getErrorTemplateName();
846
847 try
848 {
849 _errorTemplate = (Template) _broker.get("template", templateName);
850 }
851 catch (ResourceException e)
852 {
853 _errorTemplate = new org.webmacro.engine.StringTemplate(_broker, DEFAULT_ERROR_TEXT,
854 "WebMacro default error template");
855 }
856 return _errorTemplate;
857 }
858
859 protected String getErrorTemplateName ()
860 {
861 String templateName;
862
863 try
864 {
865 templateName = (String) _broker.get("config", ERROR_TEMPLATE);
866 }
867 catch (ResourceException e)
868 {
869 templateName = ERROR_TEMPLATE_DEFAULT;
870 }
871 return templateName;
872 }
873
874
875 }