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