View Javadoc

1   /*
2    * $Id: StrutsTilesRequestContext.java 557352 2007-07-18 18:23:36Z apetrelli $
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.tiles;
22  
23  import com.opensymphony.xwork2.ActionInvocation;
24  import com.opensymphony.xwork2.inject.Container;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts2.ServletActionContext;
28  import org.apache.struts2.dispatcher.Dispatcher;
29  import org.apache.struts2.views.freemarker.FreemarkerResult;
30  import org.apache.tiles.context.TilesRequestContext;
31  import org.apache.tiles.context.TilesRequestContextWrapper;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.IOException;
36  
37  /***
38   * Default implementation of TilesUtil.
39   * This class contains default implementation of utilities. This implementation
40   * is intended to be used without Struts.
41   * <p/>
42   * TilesUtilImpl implementation used to intercept .ftl requests and
43   * ensure that they are setup properly to take advantage of the
44   * {@link FreemarkerResult}.
45   *
46   * @version $Id: StrutsTilesRequestContext.java 557352 2007-07-18 18:23:36Z apetrelli $
47   */
48  public class StrutsTilesRequestContext extends TilesRequestContextWrapper {
49  
50      private static final Log LOG =
51              LogFactory.getLog(StrutsTilesRequestContext.class);
52  
53  
54      /***
55       * The mask used to detect requests which should be intercepted.
56       */
57      private String mask;
58  
59      /***
60       * Default constructor.
61       * Sets the mask to '.ftl'
62       *
63       * @param context
64       */
65      public StrutsTilesRequestContext(TilesRequestContext context) {
66          this(context, ".ftl");
67      }
68  
69      /***
70       * Optional constructor used to specify a specific mask.
71       *
72       * @param mask
73       * @param context
74       */
75      public StrutsTilesRequestContext(TilesRequestContext context,
76                                       String mask) {
77          super(context);
78          this.mask = mask;
79      }
80  
81      public void dispatch(String include) throws IOException {
82      	if (include.endsWith(mask)) {
83              // FIXME This way FreeMarker results still don't have a content-type!
84      	    include(include);
85          } else {
86              super.dispatch(include);
87          }
88      }
89  
90      /***
91       * Enhancement of the default include which allows for freemarker
92       * templates to be intercepted so that the FreemarkerResult can
93       * be used in order to setup the appropriate model.
94       *
95       * @throws IOException
96       */
97      public void include(String include) throws IOException {
98          if (include.endsWith(mask)) {
99              if (LOG.isDebugEnabled()) {
100                 LOG.debug("Intercepting tiles include '" + include + "'. Processing as freemarker result.");
101             }
102             HttpServletRequest request = (HttpServletRequest) getRequest();
103             HttpServletResponse response = (HttpServletResponse) getResponse();
104 
105             ActionInvocation invocation =
106                     ServletActionContext.getActionContext(request).getActionInvocation();
107 
108             try {
109                 FreemarkerResult result = new FreemarkerResult();
110                 result.setWriter(response.getWriter());
111 
112                 Container container = Dispatcher.getInstance()
113                         .getConfigurationManager()
114                         .getConfiguration().getContainer();
115 
116                 container.inject(result);
117 
118                 result.doExecute(include, invocation);
119             } catch (Exception e) {
120                 LOG.error("Error invoking Freemarker template", e);
121                 throw new IOException("Error invoking Freemarker template." + e.getMessage());
122             }
123         } else {
124             super.include(include);
125         }
126     }
127 
128 }