View Javadoc

    1   /*
    2    * $Id: TemplatePageFilter.java 486508 2006-12-13 06:19:43Z 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
    25  import javax.servlet.FilterConfig;
    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.struts2.ServletActionContext;
    32  import org.apache.struts2.StrutsConstants;
    33  import org.apache.struts2.dispatcher.Dispatcher;
    34
    35  import com.opensymphony.module.sitemesh.Decorator;
    36  import com.opensymphony.module.sitemesh.Page;
    37  import com.opensymphony.module.sitemesh.filter.PageFilter;
    38  import com.opensymphony.xwork2.ActionContext;
    39  import com.opensymphony.xwork2.ActionEventListener;
    40  import com.opensymphony.xwork2.ActionInvocation;
    41  import com.opensymphony.xwork2.ActionProxy;
    42  import com.opensymphony.xwork2.ActionSupport;
    43  import com.opensymphony.xwork2.Result;
    44  import com.opensymphony.xwork2.inject.Inject;
    45  import com.opensymphony.xwork2.interceptor.PreResultListener;
    46  import com.opensymphony.xwork2.util.OgnlValueStack;
    47
    48  /***
    49   *  An abstract template page filter that sets up the proper contexts for
    50   *  template processing.
    51   *
    52   */
    53  public abstract class TemplatePageFilter extends PageFilter {
    54
    55      private FilterConfig filterConfig;
    56
    57      private static String customEncoding;
    58
    59      @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
    60      public static void setCustomEncoding(String enc) {
    61          customEncoding = enc;
    62      }
    63
    64      public void init(FilterConfig filterConfig) {
    65          super.init(filterConfig);
    66          this.filterConfig = filterConfig;
    67      }
    68
    69      /***
    70       *  Applies the decorator, using the relevent contexts
    71       *
    72       * @param page The page
    73       * @param decorator The decorator
    74       * @param req The servlet request
    75       * @param res The servlet response
    76       * @param servletContext The servlet context
    77       * @param ctx The action context for this request, populated with the server state
    78       */
    79      protected abstract void applyDecorator(Page page, Decorator decorator,
    80                                    HttpServletRequest req, HttpServletResponse res,
    81                                    ServletContext servletContext, ActionContext ctx)
    82              throws ServletException, IOException;
    83
    84      /***
    85       *  Applies the decorator, creating the relevent contexts and delegating to
    86       *  the extended applyDecorator().
    87       *
    88       * @param page The page
    89       * @param decorator The decorator
    90       * @param req The servlet request
    91       * @param res The servlet response
    92       */
    93      protected void applyDecorator(Page page, Decorator decorator,
    94                                    HttpServletRequest req, HttpServletResponse res)
    95              throws ServletException, IOException {
    96
    97          ServletContext servletContext = filterConfig.getServletContext();
    98          ActionContext ctx = ServletActionContext.getActionContext(req);
    99          if (ctx == null) {
    100             // ok, one isn't associated with the request, so let's get a ThreadLocal one (which will create one if needed)
    101             OgnlValueStack vs = new OgnlValueStack();
    102             vs.getContext().putAll(Dispatcher.getInstance().createContextMap(req, res, null, servletContext));
    103             ctx = new ActionContext(vs.getContext());
    104             if (ctx.getActionInvocation() == null) {
    105                 // put in a dummy ActionSupport so basic functionality still works
    106                 ActionSupport action = new ActionSupport();
    107                 vs.push(action);
    108                 ctx.setActionInvocation(new DummyActionInvocation(action));
    109             }
    110         }
    111
    112         // delegate to the actual page decorator
    113         applyDecorator(page, decorator, req, res, servletContext, ctx);
    114     }
    115
    116
    117     /***
    118      *  Gets the L18N encoding of the system.  The default is UTF-8.
    119      */
    120     protected String getEncoding() {
    121         String encoding = customEncoding;
    122         if (encoding == null) {
    123             encoding = System.getProperty("file.encoding");
    124         }
    125         if (encoding == null) {
    126             encoding = "UTF-8";
    127         }
    128         return encoding;
    129     }
    130
    131     static class DummyActionInvocation implements ActionInvocation {
    132
    133         private static final long serialVersionUID = -4808072199157363028L;
    134
    135         ActionSupport action;
    136
    137         public DummyActionInvocation(ActionSupport action) {
    138             this.action = action;
    139         }
    140
    141         public Object getAction() {
    142             return action;
    143         }
    144
    145         public boolean isExecuted() {
    146             return false;
    147         }
    148
    149         public ActionContext getInvocationContext() {
    150             return null;
    151         }
    152
    153         public ActionProxy getProxy() {
    154             return null;
    155         }
    156
    157         public Result getResult() throws Exception {
    158             return null;
    159         }
    160
    161         public String getResultCode() {
    162             return null;
    163         }
    164
    165         public void setResultCode(String resultCode) {
    166         }
    167
    168         public OgnlValueStack getStack() {
    169             return null;
    170         }
    171
    172         public void addPreResultListener(PreResultListener listener) {
    173         }
    174
    175         public String invoke() throws Exception {
    176             return null;
    177         }
    178
    179         public String invokeActionOnly() throws Exception {
    180             return null;
    181         }
    182
    183         public void setActionEventListener(ActionEventListener listener) {
    184         }
    185     }
    186 }