1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.app.client.web.servlet;
18
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.gwe.app.client.config.ClientConfig;
28 import org.gwe.app.client.web.GWEServletContext;
29 import org.gwe.app.client.web.request.PageModel;
30 import org.gwe.app.client.web.view.MainVelocityTemplate;
31 import org.gwe.utils.VelocityUtils;
32
33
34
35
36
37
38 public abstract class GWEServlet extends HttpServlet {
39
40 private String contextRelativePath;
41 private String template;
42
43 public GWEServlet(String context) {
44 this.contextRelativePath = context;
45 }
46
47 public void setMainTemplate(MainVelocityTemplate mainTemplate) throws IOException {
48 this.template = mainTemplate.createPageTemplate(getTemplateName());
49 }
50
51 protected ClientConfig getConfig() {
52 return (ClientConfig) getServletContext().getAttribute(GWEServletContext.GWE_CONFIGURATION);
53 }
54
55 protected String getTemplateName() {
56 return contextRelativePath + ".html";
57 }
58
59 public String getContextRelativePath() {
60 return contextRelativePath;
61 }
62
63 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64 response.setContentType("text/html");
65 response.setStatus(HttpServletResponse.SC_OK);
66 response.getWriter().println(createPage(request));
67 }
68
69 private String createPage(HttpServletRequest request) throws ServletException {
70 try {
71 return VelocityUtils.mergeThrowingExceptions(createPageModel(request), template);
72 } catch (Exception e) {
73 throw new ServletException("Problems parsing velocity template", e);
74 }
75 }
76
77
78
79
80
81
82
83 protected PageModel createPageModel(HttpServletRequest request) {
84 return new PageModel(request, getConfig());
85 }
86
87 public static void main(String[] args) throws IOException {
88 GWEServlet servlet = new GWEServlet("macro") {};
89 servlet.setMainTemplate(new MainVelocityTemplate());
90 String page = VelocityUtils.evaluate(servlet.template);
91 FileOutputStream fos = new FileOutputStream("/Users/admin/work/eclipse-ws/gwe-core/tmp/web/test.html");
92 fos.write(page.getBytes());
93 fos.close();
94 }
95 }
96
97
98