1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.web.util; |
22 | |
|
23 | |
import javax.servlet.ServletException; |
24 | |
import javax.servlet.http.HttpServlet; |
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
import javax.servlet.http.HttpServletResponse; |
27 | |
|
28 | |
import org.apache.tiles.AttributeContext; |
29 | |
import org.apache.tiles.TilesContainer; |
30 | |
import org.apache.tiles.access.TilesAccess; |
31 | |
import org.apache.tiles.request.ApplicationContext; |
32 | |
import org.apache.tiles.request.Request; |
33 | |
import org.apache.tiles.request.reflect.ClassUtil; |
34 | |
import org.apache.tiles.request.servlet.ServletRequest; |
35 | |
import org.slf4j.Logger; |
36 | |
import org.slf4j.LoggerFactory; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class TilesDispatchServlet extends HttpServlet { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public static final String CONTAINER_KEY_INIT_PARAMETER = |
50 | |
"org.apache.tiles.web.util.TilesDispatchServlet.CONTAINER_KEY"; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | private final Logger log = LoggerFactory |
56 | |
.getLogger(TilesDispatchServlet.class); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private String containerKey; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private AttributeContextMutator mutator; |
68 | |
|
69 | |
|
70 | |
|
71 | |
@Override |
72 | |
public void init() throws ServletException { |
73 | 0 | super.init(); |
74 | |
|
75 | 0 | containerKey = getServletConfig().getInitParameter( |
76 | |
CONTAINER_KEY_INIT_PARAMETER); |
77 | |
|
78 | 0 | String temp = getInitParameter("mutator"); |
79 | 0 | if (temp != null) { |
80 | |
try { |
81 | 0 | mutator = (AttributeContextMutator) ClassUtil.instantiate(temp); |
82 | 0 | } catch (Exception e) { |
83 | 0 | throw new ServletException("Unable to instantiate specified context mutator.", e); |
84 | 0 | } |
85 | |
} else { |
86 | 0 | mutator = new DefaultMutator(); |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
@Override |
92 | |
protected void doGet(HttpServletRequest req, HttpServletResponse res) { |
93 | |
|
94 | 0 | ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil |
95 | |
.getApplicationContext(getServletContext()); |
96 | 0 | Request request = new ServletRequest(applicationContext, |
97 | |
req, res); |
98 | 0 | TilesContainer container = TilesAccess.getContainer(applicationContext, |
99 | |
containerKey); |
100 | 0 | mutator.mutate(container.getAttributeContext(request), req); |
101 | 0 | String definition = getDefinitionName(req); |
102 | 0 | if (log.isDebugEnabled()) { |
103 | 0 | log.info("Dispatching to tile '" + definition + "'"); |
104 | |
} |
105 | 0 | container.render(definition, request); |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
protected String getDefinitionName(HttpServletRequest request) { |
115 | 0 | String path = (String) request.getAttribute("javax.servlet.include.servlet_path"); |
116 | 0 | if (path == null) { |
117 | 0 | path = request.getServletPath(); |
118 | |
} |
119 | |
|
120 | 0 | int start = path.startsWith("/") ? 1 : 0; |
121 | 0 | int end = path.endsWith(".tiles") ? path.indexOf(".tiles") : path.length(); |
122 | |
|
123 | 0 | return path.substring(start, end); |
124 | |
} |
125 | |
|
126 | |
|
127 | |
@Override |
128 | |
protected void doPost(HttpServletRequest req, HttpServletResponse res) { |
129 | 0 | log.info("Tiles dispatch request received. Redirecting POST to GET."); |
130 | 0 | doGet(req, res); |
131 | 0 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 0 | class DefaultMutator implements AttributeContextMutator { |
137 | |
|
138 | |
|
139 | |
public void mutate(AttributeContext context, javax.servlet.ServletRequest request) { |
140 | |
|
141 | 0 | } |
142 | |
} |
143 | |
} |