1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.views.velocity;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.io.Writer;
24 import java.util.Properties;
25
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.jsp.JspFactory;
31 import javax.servlet.jsp.PageContext;
32
33 import org.apache.struts2.RequestUtils;
34 import org.apache.struts2.ServletActionContext;
35 import org.apache.struts2.StrutsConstants;
36 import org.apache.struts2.config.Settings;
37 import org.apache.struts2.views.util.ContextUtil;
38 import org.apache.velocity.Template;
39 import org.apache.velocity.context.Context;
40 import org.apache.velocity.exception.MethodInvocationException;
41 import org.apache.velocity.exception.ParseErrorException;
42 import org.apache.velocity.exception.ResourceNotFoundException;
43 import org.apache.velocity.runtime.RuntimeSingleton;
44 import org.apache.velocity.servlet.VelocityServlet;
45
46 import com.opensymphony.xwork2.ActionContext;
47
48
49 /***
50 * @deprecated please use {@link org.apache.struts2.dispatcher.VelocityResult} instead of direct access
51 */
52 public class StrutsVelocityServlet extends VelocityServlet {
53 private static final long serialVersionUID = -2078492831396251182L;
54 private VelocityManager velocityManager;
55
56 public StrutsVelocityServlet() {
57 velocityManager = VelocityManager.getInstance();
58 }
59
60 public void init(ServletConfig servletConfig) throws ServletException {
61 super.init(servletConfig);
62
63
64 velocityManager.init(servletConfig.getServletContext());
65 }
66
67 protected Context createContext(HttpServletRequest request, HttpServletResponse response) {
68 return velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response);
69 }
70
71 protected Template handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Context context) throws Exception {
72 String servletPath = (String) httpServletRequest.getAttribute("javax.servlet.include.servlet_path");
73
74 if (servletPath == null) {
75 servletPath = RequestUtils.getServletPath(httpServletRequest);
76 }
77
78 return getTemplate(servletPath, getEncoding());
79 }
80
81 /***
82 * This method extends the VelocityServlet's loadConfiguration method by performing the following actions:
83 * <ul>
84 * <li>invokes VelocityServlet.loadConfiguration to create a properties object</li>
85 * <li>alters the RESOURCE_LOADER to include a class loader</li>
86 * <li>configures the class loader using the StrutsResourceLoader</li>
87 * </ul>
88 *
89 * @param servletConfig
90 * @throws IOException
91 * @throws FileNotFoundException
92 * @see org.apache.velocity.servlet.VelocityServlet#loadConfiguration
93 */
94 protected Properties loadConfiguration(ServletConfig servletConfig) throws IOException, FileNotFoundException {
95 return velocityManager.loadConfiguration(servletConfig.getServletContext());
96 }
97
98 /***
99 * create a PageContext and render the template to PageContext.getOut()
100 *
101 * @see VelocityServlet#mergeTemplate(Template, Context, HttpServletResponse) for additional documentation
102 */
103 protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, UnsupportedEncodingException, Exception {
104
105 PageContext oldPageContext = ServletActionContext.getPageContext();
106
107
108 JspFactory jspFactory = JspFactory.getDefaultFactory();
109 HttpServletRequest request = (HttpServletRequest) context.get(ContextUtil.REQUEST);
110 PageContext pageContext = jspFactory.getPageContext(this, request, response, null, true, 8192, true);
111
112
113 ActionContext actionContext = ActionContext.getContext();
114 actionContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
115
116 try {
117 Writer writer = pageContext.getOut();
118 template.merge(context, writer);
119 writer.flush();
120 } finally {
121
122 jspFactory.releasePageContext(pageContext);
123 actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext);
124 }
125 }
126
127 private String getEncoding() {
128
129 try {
130 return Settings.get(StrutsConstants.STRUTS_I18N_ENCODING);
131 } catch (IllegalArgumentException e) {
132 return RuntimeSingleton.getString(RuntimeSingleton.OUTPUT_ENCODING, DEFAULT_OUTPUT_ENCODING);
133 }
134 }
135 }