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