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