View Javadoc

1   /*
2    * $Id: StrutsVelocityServlet.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // initialize our VelocityManager
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         // save the old PageContext
105         PageContext oldPageContext = ServletActionContext.getPageContext();
106 
107         // create a new PageContext
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         // put the new PageContext into ActionContext
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             // perform cleanup
122             jspFactory.releasePageContext(pageContext);
123             actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext);
124         }
125     }
126 
127     private String getEncoding() {
128         // todo look into converting this to using XWork/Struts encoding rules
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 }