View Javadoc

1   /*
2    * $Id: VelocityPageFilter.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.sitemesh;
19  
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  
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.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.struts2.views.velocity.VelocityManager;
31  import org.apache.velocity.Template;
32  import org.apache.velocity.context.Context;
33  
34  import com.opensymphony.module.sitemesh.Decorator;
35  import com.opensymphony.module.sitemesh.HTMLPage;
36  import com.opensymphony.module.sitemesh.Page;
37  import com.opensymphony.xwork2.ActionContext;
38  
39  
40  /***
41   *  Applies Velocity-based decorators
42   * 
43   */
44  public class VelocityPageFilter extends TemplatePageFilter {
45      private static final Log LOG = LogFactory.getLog(VelocityPageFilter.class);
46  
47      /***
48       *  Applies the decorator, using the relevent contexts
49       * 
50       * @param page The page
51       * @param decorator The decorator
52       * @param req The servlet request
53       * @param res The servlet response
54       * @param servletContext The servlet context
55       * @param ctx The action context for this request, populated with the server state
56       */
57      protected void applyDecorator(Page page, Decorator decorator,
58                                    HttpServletRequest req, HttpServletResponse res,
59                                    ServletContext servletContext, ActionContext ctx)
60              throws ServletException, IOException {
61          try {
62              VelocityManager vm = VelocityManager.getInstance();
63  
64              // init (if needed)
65              vm.init(servletContext);
66  
67              // get encoding
68              String encoding = getEncoding();
69  
70              // get the template and context
71              Template template = vm.getVelocityEngine().getTemplate(decorator.getPage(), encoding);
72              Context context = vm.createContext(ctx.getValueStack(), req, res);
73  
74              // put the page in the context
75              context.put("page", page);
76              if (page instanceof HTMLPage) {
77                  HTMLPage htmlPage = ((HTMLPage) page);
78                  context.put("head", htmlPage.getHead());
79              }
80              context.put("title",page.getTitle());
81              context.put("body",page.getBody());
82  
83              // finally, render it
84              PrintWriter writer = res.getWriter();
85              template.merge(context, writer);
86              writer.flush();
87          } catch (Exception e) {
88              String msg = "Error applying decorator: " + e.getMessage();
89              LOG.error(msg, e);
90              throw new ServletException(msg, e);
91          }
92      }
93  }