1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.util;
22
23 import jakarta.servlet.ServletContext;
24 import jakarta.servlet.http.HttpServletRequest;
25
26 import org.apache.struts.Globals;
27 import org.apache.struts.action.RequestProcessor;
28 import org.apache.struts.config.MessageResourcesConfig;
29 import org.apache.struts.config.ModuleConfig;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 public class ModuleUtils {
40
41
42
43 private static final ModuleUtils instance = new ModuleUtils();
44
45
46
47
48 private final Logger log =
49 LoggerFactory.getLogger(ModuleUtils.class);
50
51
52
53
54 protected ModuleUtils() {
55 super();
56 }
57
58
59
60
61 public static ModuleUtils getInstance() {
62 return instance;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public ModuleConfig getModuleConfig(HttpServletRequest request) {
78 return (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
79 }
80
81
82
83
84
85
86
87
88
89
90 public ModuleConfig getModuleConfig(String prefix, ServletContext context) {
91 if ((prefix == null) || "/".equals(prefix)) {
92 return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY);
93 } else {
94 return (ModuleConfig) context.getAttribute(Globals.MODULE_KEY
95 + prefix);
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109 public ModuleConfig getModuleConfig(String prefix,
110 HttpServletRequest request, ServletContext context) {
111 ModuleConfig moduleConfig = null;
112
113 if (prefix != null) {
114
115 moduleConfig = this.getModuleConfig(prefix, context);
116 } else {
117
118 moduleConfig = this.getModuleConfig(request, context);
119 }
120
121 return moduleConfig;
122 }
123
124
125
126
127
128
129
130
131 public ModuleConfig getModuleConfig(HttpServletRequest request,
132 ServletContext context) {
133 ModuleConfig moduleConfig = this.getModuleConfig(request);
134
135 if (moduleConfig == null) {
136 moduleConfig = this.getModuleConfig("", context);
137 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
138 }
139
140 return moduleConfig;
141 }
142
143
144
145
146
147
148
149
150 public String getModuleName(HttpServletRequest request,
151 ServletContext context) {
152
153 String matchPath =
154 (String) request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);
155
156 if (matchPath == null) {
157 matchPath = request.getServletPath();
158 }
159
160 return this.getModuleName(matchPath, context);
161 }
162
163
164
165
166
167
168
169
170 public String getModuleName(String matchPath, ServletContext context) {
171 log.debug("Get module name for path {}", matchPath);
172
173 String prefix = "";
174 String[] prefixes = getModulePrefixes(context);
175
176
177 int lastSlash = 0;
178
179 while (prefix.isEmpty()
180 && ((lastSlash = matchPath.lastIndexOf("/")) > 0)) {
181
182 matchPath = matchPath.substring(0, lastSlash);
183
184
185 for (int i = 0; i < prefixes.length; i++) {
186 if (matchPath.equals(prefixes[i])) {
187 prefix = prefixes[i];
188
189 break;
190 }
191 }
192 }
193
194 if (log.isDebugEnabled()) {
195 log.debug("Module name found: {}",
196 prefix.isEmpty() ? "default" : prefix);
197 }
198
199 return prefix;
200 }
201
202
203
204
205
206
207
208
209
210 public String[] getModulePrefixes(ServletContext context) {
211 return (String[]) context.getAttribute(Globals.MODULE_PREFIXES_KEY);
212 }
213
214
215
216
217
218
219
220
221 public void selectModule(HttpServletRequest request, ServletContext context) {
222
223 String prefix = getModuleName(request, context);
224
225
226 this.selectModule(prefix, request, context);
227 }
228
229
230
231
232
233
234
235
236
237 public void selectModule(String prefix, HttpServletRequest request,
238 ServletContext context) {
239
240 ModuleConfig config = getModuleConfig(prefix, context);
241
242 if (config != null) {
243 request.setAttribute(Globals.MODULE_KEY, config);
244
245 MessageResourcesConfig[] mrConfig =
246 config.findMessageResourcesConfigs();
247
248 for (int i = 0; i < mrConfig.length; i++) {
249 String key = mrConfig[i].getKey();
250 MessageResources resources =
251 (MessageResources) context.getAttribute(key + prefix);
252
253 if (resources != null) {
254 request.setAttribute(key, resources);
255 } else {
256 request.removeAttribute(key);
257 }
258 }
259 } else {
260 request.removeAttribute(Globals.MODULE_KEY);
261 }
262 }
263 }