View Javadoc

    1   /*
    2    * $Id: VelocityPageFilter.java 475637 2006-11-16 08:32:03Z mrdon $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.sitemesh;
    22
    23  import java.io.IOException;
    24  import java.io.PrintWriter;
    25
    26  import javax.servlet.ServletContext;
    27  import javax.servlet.ServletException;
    28  import javax.servlet.http.HttpServletRequest;
    29  import javax.servlet.http.HttpServletResponse;
    30
    31  import org.apache.commons.logging.Log;
    32  import org.apache.commons.logging.LogFactory;
    33  import org.apache.struts2.views.freemarker.FreemarkerManager;
    34  import org.apache.struts2.views.velocity.VelocityManager;
    35  import org.apache.velocity.Template;
    36  import org.apache.velocity.context.Context;
    37
    38  import com.opensymphony.module.sitemesh.Decorator;
    39  import com.opensymphony.module.sitemesh.HTMLPage;
    40  import com.opensymphony.module.sitemesh.Page;
    41  import com.opensymphony.xwork2.ActionContext;
    42  import com.opensymphony.xwork2.inject.Inject;
    43
    44
    45  /***
    46   *  Applies Velocity-based decorators
    47   *
    48   */
    49  public class VelocityPageFilter extends TemplatePageFilter {
    50      private static final Log LOG = LogFactory.getLog(VelocityPageFilter.class);
    51
    52      private static VelocityManager velocityManager;
    53
    54      @Inject(required=false)
    55      public static void setVelocityManager(VelocityManager mgr) {
    56          velocityManager = mgr;
    57      }
    58
    59      /***
    60       *  Applies the decorator, using the relevent contexts
    61       *
    62       * @param page The page
    63       * @param decorator The decorator
    64       * @param req The servlet request
    65       * @param res The servlet response
    66       * @param servletContext The servlet context
    67       * @param ctx The action context for this request, populated with the server state
    68       */
    69      protected void applyDecorator(Page page, Decorator decorator,
    70                                    HttpServletRequest req, HttpServletResponse res,
    71                                    ServletContext servletContext, ActionContext ctx)
    72              throws ServletException, IOException {
    73
    74          if (velocityManager == null) {
    75              throw new ServletException("Missing freemarker dependency");
    76          }
    77
    78          try {
    79
    80              // init (if needed)
    81              velocityManager.init(servletContext);
    82
    83              // get encoding
    84              String encoding = getEncoding();
    85
    86              // get the template and context
    87              Template template = velocityManager.getVelocityEngine().getTemplate(decorator.getPage(), encoding);
    88              Context context = velocityManager.createContext(ctx.getValueStack(), req, res);
    89
    90              // put the page in the context
    91              context.put("page", page);
    92              if (page instanceof HTMLPage) {
    93                  HTMLPage htmlPage = ((HTMLPage) page);
    94                  context.put("head", htmlPage.getHead());
    95              }
    96              context.put("title",page.getTitle());
    97              context.put("body",page.getBody());
    98
    99              // finally, render it
    100             PrintWriter writer = res.getWriter();
    101             template.merge(context, writer);
    102             writer.flush();
    103         } catch (Exception e) {
    104             String msg = "Error applying decorator: " + e.getMessage();
    105             LOG.error(msg, e);
    106             throw new ServletException(msg, e);
    107         }
    108     }
    109 }