1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.sitemesh;
23
24 import java.io.IOException;
25
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.struts2.ServletActionContext;
33 import org.apache.struts2.StrutsConstants;
34 import org.apache.struts2.dispatcher.Dispatcher;
35
36 import com.opensymphony.module.sitemesh.Decorator;
37 import com.opensymphony.module.sitemesh.Page;
38 import com.opensymphony.module.sitemesh.filter.PageFilter;
39 import com.opensymphony.xwork2.ActionContext;
40 import com.opensymphony.xwork2.ActionEventListener;
41 import com.opensymphony.xwork2.ActionInvocation;
42 import com.opensymphony.xwork2.ActionProxy;
43 import com.opensymphony.xwork2.ActionSupport;
44 import com.opensymphony.xwork2.Result;
45 import com.opensymphony.xwork2.inject.Inject;
46 import com.opensymphony.xwork2.interceptor.PreResultListener;
47 import com.opensymphony.xwork2.ognl.OgnlValueStack;
48 import com.opensymphony.xwork2.util.ValueStack;
49 import com.opensymphony.xwork2.util.ValueStackFactory;
50
51 /***
52 * An abstract template page filter that sets up the proper contexts for
53 * template processing.
54 *
55 */
56 public abstract class TemplatePageFilter extends PageFilter {
57
58 private FilterConfig filterConfig;
59
60 private static String customEncoding;
61
62 @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
63 public static void setCustomEncoding(String enc) {
64 customEncoding = enc;
65 }
66
67 public void init(FilterConfig filterConfig) {
68 super.init(filterConfig);
69 this.filterConfig = filterConfig;
70 }
71
72 /***
73 * Applies the decorator, using the relevent contexts
74 *
75 * @param page The page
76 * @param decorator The decorator
77 * @param req The servlet request
78 * @param res The servlet response
79 * @param servletContext The servlet context
80 * @param ctx The action context for this request, populated with the server state
81 */
82 protected abstract void applyDecorator(Page page, Decorator decorator,
83 HttpServletRequest req, HttpServletResponse res,
84 ServletContext servletContext, ActionContext ctx)
85 throws ServletException, IOException;
86
87 /***
88 * Applies the decorator, creating the relevent contexts and delegating to
89 * the extended applyDecorator().
90 *
91 * @param page The page
92 * @param decorator The decorator
93 * @param req The servlet request
94 * @param res The servlet response
95 */
96 protected void applyDecorator(Page page, Decorator decorator,
97 HttpServletRequest req, HttpServletResponse res)
98 throws ServletException, IOException {
99
100 ServletContext servletContext = filterConfig.getServletContext();
101 ActionContext ctx = ServletActionContext.getActionContext(req);
102 if (ctx == null) {
103
104 ValueStack vs = Dispatcher.getInstance().getContainer().getInstance(ValueStackFactory.class).createValueStack();
105 vs.getContext().putAll(Dispatcher.getInstance().createContextMap(req, res, null, servletContext));
106 ctx = new ActionContext(vs.getContext());
107 if (ctx.getActionInvocation() == null) {
108
109 ActionSupport action = new ActionSupport();
110 vs.push(action);
111 ctx.setActionInvocation(new DummyActionInvocation(action));
112 }
113 }
114
115
116 applyDecorator(page, decorator, req, res, servletContext, ctx);
117 }
118
119
120 /***
121 * Gets the L18N encoding of the system. The default is UTF-8.
122 */
123 protected String getEncoding() {
124 String encoding = customEncoding;
125 if (encoding == null) {
126 encoding = System.getProperty("file.encoding");
127 }
128 if (encoding == null) {
129 encoding = "UTF-8";
130 }
131 return encoding;
132 }
133
134 static class DummyActionInvocation implements ActionInvocation {
135
136 private static final long serialVersionUID = -4808072199157363028L;
137
138 ActionSupport action;
139
140 public DummyActionInvocation(ActionSupport action) {
141 this.action = action;
142 }
143
144 public Object getAction() {
145 return action;
146 }
147
148 public boolean isExecuted() {
149 return false;
150 }
151
152 public ActionContext getInvocationContext() {
153 return null;
154 }
155
156 public ActionProxy getProxy() {
157 return null;
158 }
159
160 public Result getResult() throws Exception {
161 return null;
162 }
163
164 public String getResultCode() {
165 return null;
166 }
167
168 public void setResultCode(String resultCode) {
169 }
170
171 public ValueStack getStack() {
172 return null;
173 }
174
175 public void addPreResultListener(PreResultListener listener) {
176 }
177
178 public String invoke() throws Exception {
179 return null;
180 }
181
182 public String invokeActionOnly() throws Exception {
183 return null;
184 }
185
186 public void setActionEventListener(ActionEventListener listener) {
187 }
188
189 public void init(ActionProxy proxy) {
190 }
191 }
192 }