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.struts.tiles;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28
29 import jakarta.servlet.ServletContext;
30 import jakarta.servlet.ServletException;
31 import jakarta.servlet.ServletRequest;
32 import jakarta.servlet.http.HttpServletRequest;
33 import jakarta.servlet.http.HttpServletResponse;
34 import jakarta.servlet.jsp.PageContext;
35
36 import org.apache.struts.util.RequestUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45 public class TilesUtilImpl implements Serializable {
46 private static final long serialVersionUID = -3811960942326759160L;
47
48
49
50
51 private final static Logger LOG =
52 LoggerFactory.getLogger(TilesUtilImpl.class);
53 private transient final Logger log =
54 LoggerFactory.getLogger(TilesUtilImpl.class);
55
56
57 public static final String DEFINITIONS_FACTORY =
58 "org.apache.struts.tiles.DEFINITIONS_FACTORY";
59
60
61
62
63 private static Method include = null;
64
65
66
67
68
69 static {
70
71 try {
72
73 include = PageContext.class.getMethod("include", String.class, boolean.class);
74 } catch (NoSuchMethodException e) {
75 LOG.debug("Could not find JSP 2.0 include method. Using old one that doesn't support " +
76 "configurable flushing.", e);
77 }
78 }
79
80
81
82
83
84
85
86
87
88 public void doForward(
89 String uri,
90 HttpServletRequest request,
91 HttpServletResponse response,
92 ServletContext servletContext)
93 throws IOException, ServletException {
94
95 request.getRequestDispatcher(uri).forward(request, response);
96 }
97
98
99
100
101
102
103
104
105
106
107
108 public void doInclude(
109 String uri,
110 HttpServletRequest request,
111 HttpServletResponse response,
112 ServletContext servletContext)
113 throws IOException, ServletException {
114
115 request.getRequestDispatcher(uri).include(request, response);
116 }
117
118
119
120
121
122
123
124
125
126
127 public void doInclude(String uri, PageContext pageContext, boolean flush)
128 throws IOException, ServletException {
129 try {
130
131 if (include != null) {
132 include.invoke(pageContext, new Object[]{uri, Boolean.valueOf(flush)});
133 return;
134 }
135 } catch (IllegalAccessException e) {
136 log.debug("Could not find JSP 2.0 include method. Using old one.", e);
137 } catch (InvocationTargetException e) {
138 if (e.getCause() instanceof ServletException){
139 throw ((ServletException)e.getCause());
140 } else if (e.getCause() instanceof IOException){
141 throw ((IOException)e.getCause());
142 } else {
143 throw new ServletException(e);
144 }
145 }
146
147 pageContext.include(uri);
148 }
149
150
151
152
153
154 public DefinitionsFactory getDefinitionsFactory(
155 ServletRequest request,
156 ServletContext servletContext) {
157
158 return (DefinitionsFactory) servletContext.getAttribute(DEFINITIONS_FACTORY);
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public DefinitionsFactory createDefinitionsFactory(
176 ServletContext servletContext,
177 DefinitionsFactoryConfig factoryConfig)
178 throws DefinitionsFactoryException {
179
180
181 DefinitionsFactory factory =
182 createDefinitionFactoryInstance(factoryConfig.getFactoryClassname());
183
184 factory.init(factoryConfig, servletContext);
185
186
187 makeDefinitionsFactoryAccessible(factory, servletContext);
188 return factory;
189 }
190
191
192
193
194
195
196
197
198
199
200
201 @SuppressWarnings("deprecation")
202 protected DefinitionsFactory createDefinitionFactoryInstance(String classname)
203 throws DefinitionsFactoryException {
204
205 try {
206 Class<?> factoryClass = RequestUtils.applicationClass(classname);
207 Object factory = factoryClass.getDeclaredConstructor().newInstance();
208
209
210
211 if (factory instanceof ComponentDefinitionsFactory) {
212 factory =
213 new org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper(
214 (ComponentDefinitionsFactory) factory);
215 }
216 return (DefinitionsFactory) factory;
217
218 } catch (ClassCastException ex) {
219 throw new DefinitionsFactoryException(
220 "Error - createDefinitionsFactory : Factory class '"
221 + classname
222 + " must implement 'TilesDefinitionsFactory'.",
223 ex);
224
225 } catch (ClassNotFoundException ex) {
226 throw new DefinitionsFactoryException(
227 "Error - createDefinitionsFactory : Bad class name '"
228 + classname
229 + "'.",
230 ex);
231
232 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
233 | InvocationTargetException | NoSuchMethodException
234 | SecurityException ex) {
235 throw new DefinitionsFactoryException(ex);
236 }
237 }
238
239
240
241
242
243
244
245 protected void makeDefinitionsFactoryAccessible(
246 DefinitionsFactory factory,
247 ServletContext servletContext) {
248
249 servletContext.setAttribute(DEFINITIONS_FACTORY, factory);
250 }
251 }