1 package org.apache.turbine.modules.layouts;
2
3
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.apache.logging.log4j.LogManager;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import org.apache.logging.log4j.Logger;
29 import org.apache.turbine.Turbine;
30 import org.apache.turbine.annotation.TurbineService;
31 import org.apache.turbine.modules.Layout;
32 import org.apache.turbine.pipeline.PipelineData;
33 import org.apache.turbine.services.velocity.VelocityService;
34 import org.apache.turbine.util.RunData;
35 import org.apache.velocity.context.Context;
36
37
38
39
40
41
42
43 public abstract class VelocityLayout implements Layout
44 {
45
46 protected final Logger log = LogManager.getLogger(this.getClass());
47
48
49 protected static final String prefix = PREFIX + "/";
50
51
52 @TurbineService
53 protected VelocityService velocityService;
54
55
56
57
58
59
60
61
62 @Override
63 public void doBuild(PipelineData pipelineData)
64 throws Exception
65 {
66 RunData data = pipelineData.getRunData();
67
68 Context context = velocityService.getContext(pipelineData);
69
70
71 populateContext(pipelineData, context);
72
73
74
75
76 String templateName = data.getTemplateInfo().getLayoutTemplate();
77
78
79 data.getResponse().setLocale(data.getLocale());
80 data.getResponse().setContentType(data.getContentType());
81
82 log.debug("Now trying to render layout {}", templateName);
83
84
85 render(pipelineData, context, templateName);
86 }
87
88
89
90
91
92
93
94
95
96 protected abstract void populateContext(PipelineData pipelineData, Context context)
97 throws Exception;
98
99
100
101
102
103
104
105
106
107
108 protected void render(PipelineData pipelineData, Context context, String templateName)
109 throws Exception
110 {
111 velocityService.handleRequest(context,
112 prefix + templateName,
113 pipelineData.get(Turbine.class, HttpServletResponse.class)
114 .getOutputStream());
115 }
116 }