This project has retired. For details please refer to its
Attic page.
TilesDispatchServlet xref
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 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 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 super.init();
74
75 containerKey = getServletConfig().getInitParameter(
76 CONTAINER_KEY_INIT_PARAMETER);
77
78 String temp = getInitParameter("mutator");
79 if (temp != null) {
80 try {
81 mutator = (AttributeContextMutator) ClassUtil.instantiate(temp);
82 } catch (Exception e) {
83 throw new ServletException("Unable to instantiate specified context mutator.", e);
84 }
85 } else {
86 mutator = new DefaultMutator();
87 }
88 }
89
90
91 @Override
92 protected void doGet(HttpServletRequest req, HttpServletResponse res) {
93
94 ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil
95 .getApplicationContext(getServletContext());
96 Request request = new ServletRequest(applicationContext,
97 req, res);
98 TilesContainer container = TilesAccess.getContainer(applicationContext,
99 containerKey);
100 mutator.mutate(container.getAttributeContext(request), req);
101 String definition = getDefinitionName(req);
102 if (log.isDebugEnabled()) {
103 log.info("Dispatching to tile '" + definition + "'");
104 }
105 container.render(definition, request);
106 }
107
108
109
110
111
112
113
114 protected String getDefinitionName(HttpServletRequest request) {
115 String path = (String) request.getAttribute("javax.servlet.include.servlet_path");
116 if (path == null) {
117 path = request.getServletPath();
118 }
119
120 int start = path.startsWith("/") ? 1 : 0;
121 int end = path.endsWith(".tiles") ? path.indexOf(".tiles") : path.length();
122
123 return path.substring(start, end);
124 }
125
126
127 @Override
128 protected void doPost(HttpServletRequest req, HttpServletResponse res) {
129 log.info("Tiles dispatch request received. Redirecting POST to GET.");
130 doGet(req, res);
131 }
132
133
134
135
136 class DefaultMutator implements AttributeContextMutator {
137
138
139 public void mutate(AttributeContext context, javax.servlet.ServletRequest request) {
140
141 }
142 }
143 }