001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.velocity;
006:
007: import com.opensymphony.webwork.RequestUtils;
008: import com.opensymphony.webwork.ServletActionContext;
009: import com.opensymphony.webwork.WebWorkConstants;
010: import com.opensymphony.webwork.config.Configuration;
011: import com.opensymphony.webwork.views.util.ContextUtil;
012: import com.opensymphony.xwork.ActionContext;
013: import org.apache.velocity.Template;
014: import org.apache.velocity.context.Context;
015: import org.apache.velocity.exception.MethodInvocationException;
016: import org.apache.velocity.exception.ParseErrorException;
017: import org.apache.velocity.exception.ResourceNotFoundException;
018: import org.apache.velocity.runtime.RuntimeSingleton;
019: import org.apache.velocity.servlet.VelocityServlet;
020:
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.jsp.JspFactory;
026: import javax.servlet.jsp.PageContext;
027: import java.io.FileNotFoundException;
028: import java.io.IOException;
029: import java.io.UnsupportedEncodingException;
030: import java.io.Writer;
031: import java.util.Properties;
032:
033: /**
034: * @author $Author: the_mindstorm $
035: * @version $Revision: 2052 $
036: * @deprecated please use {@link com.opensymphony.webwork.dispatcher.VelocityResult} instead of direct access
037: */
038: public class WebWorkVelocityServlet extends VelocityServlet {
039: private VelocityManager velocityManager;
040:
041: public WebWorkVelocityServlet() {
042: velocityManager = VelocityManager.getInstance();
043: }
044:
045: public void init(ServletConfig servletConfig)
046: throws ServletException {
047: super .init(servletConfig);
048:
049: // initialize our VelocityManager
050: velocityManager.init(servletConfig.getServletContext());
051: }
052:
053: protected Context createContext(HttpServletRequest request,
054: HttpServletResponse response) {
055: return velocityManager.createContext(ActionContext.getContext()
056: .getValueStack(), request, response);
057: }
058:
059: protected Template handleRequest(
060: HttpServletRequest httpServletRequest,
061: HttpServletResponse httpServletResponse, Context context)
062: throws Exception {
063: String servletPath = (String) httpServletRequest
064: .getAttribute("javax.servlet.include.servlet_path");
065:
066: if (servletPath == null) {
067: servletPath = RequestUtils
068: .getServletPath(httpServletRequest);
069: }
070:
071: return getTemplate(servletPath, getEncoding());
072: }
073:
074: /**
075: * This method extends the VelocityServlet's loadConfiguration method by performing the following actions:
076: * <ul>
077: * <li>invokes VelocityServlet.loadConfiguration to create a properties object</li>
078: * <li>alters the RESOURCE_LOADER to include a class loader</li>
079: * <li>configures the class loader using the WebWorkResourceLoader</li>
080: * </ul>
081: *
082: * @param servletConfig
083: * @throws IOException
084: * @throws FileNotFoundException
085: * @see org.apache.velocity.servlet.VelocityServlet#loadConfiguration
086: */
087: protected Properties loadConfiguration(ServletConfig servletConfig)
088: throws IOException, FileNotFoundException {
089: return velocityManager.loadConfiguration(servletConfig
090: .getServletContext());
091: }
092:
093: /**
094: * create a PageContext and render the template to PageContext.getOut()
095: *
096: * @see VelocityServlet#mergeTemplate(Template, Context, HttpServletResponse) for additional documentation
097: */
098: protected void mergeTemplate(Template template, Context context,
099: HttpServletResponse response)
100: throws ResourceNotFoundException, ParseErrorException,
101: MethodInvocationException, IOException,
102: UnsupportedEncodingException, Exception {
103: // save the old PageContext
104: PageContext oldPageContext = ServletActionContext
105: .getPageContext();
106:
107: // create a new PageContext
108: JspFactory jspFactory = JspFactory.getDefaultFactory();
109: HttpServletRequest request = (HttpServletRequest) context
110: .get(ContextUtil.REQUEST);
111: PageContext pageContext = jspFactory.getPageContext(this ,
112: request, response, null, true, 8192, true);
113:
114: // put the new PageContext into ActionContext
115: ActionContext actionContext = ActionContext.getContext();
116: actionContext.put(ServletActionContext.PAGE_CONTEXT,
117: pageContext);
118:
119: try {
120: Writer writer = pageContext.getOut();
121: template.merge(context, writer);
122: writer.flush();
123: } finally {
124: // perform cleanup
125: jspFactory.releasePageContext(pageContext);
126: actionContext.put(ServletActionContext.PAGE_CONTEXT,
127: oldPageContext);
128: }
129: }
130:
131: private String getEncoding() {
132: // todo look into converting this to using XWork/WebWork2 encoding rules
133: try {
134: return Configuration
135: .getString(WebWorkConstants.WEBWORK_I18N_ENCODING);
136: } catch (IllegalArgumentException e) {
137: return RuntimeSingleton.getString(
138: RuntimeSingleton.OUTPUT_ENCODING,
139: DEFAULT_OUTPUT_ENCODING);
140: }
141: }
142: }
|