1 package org.saf.struts.util;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import org.apache.struts.Globals;
7 import org.apache.struts.action.Action;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.ActionServlet;
11 import org.apache.struts.action.PlugIn;
12 import org.apache.struts.config.ModuleConfig;
13 import org.apache.struts.config.PlugInConfig;
14 import org.apache.struts.tiles.TilesPlugin;
15
16 import org.saf.struts.SafPlugin;
17 import org.saf.struts.SafRequestProcessor;
18 import org.saf.struts.SafTilesRequestProcessor;
19 import org.saf.struts.bean.AuthorizationAction;
20 import org.saf.struts.bean.AuthorizationDefinition;
21 import org.saf.struts.bean.AuthorizationErrorForward;
22 import org.saf.struts.bean.AuthorizationMethod;
23 import org.saf.struts.config.StrutsAuthorizationConfig;
24
25 import org.xml.sax.InputSource;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 import java.net.MalformedURLException;
31 import java.net.URL;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import javax.servlet.ServletContext;
37 import javax.servlet.ServletException;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpSession;
40
41
42 /***
43 * Simple Utility class.
44 *
45 * @author Wim Tobback
46 * @version 1.0
47 *
48 * @since 1.0
49 */
50 public class SafPluginUtils {
51 private final static Log log = LogFactory.getLog(SafPluginUtils.class);
52
53 /***
54 * Returns the AuthorizationDefinition DTO.
55 *
56 * @param config The StrutsAuthorizationConfig DTO
57 * @param action The AuthorizationAction DTO
58 *
59 * @return AuthorizationDefinition The AuthorizationDefinition DTO.
60 */
61 public static AuthorizationDefinition getActionAuthorizationDefinitionMap(
62 StrutsAuthorizationConfig config, AuthorizationAction action) {
63 if (action.getAuthorizationDefinitionId() != null) {
64 Map authorizationDefinitionMap = config.getAuthorizationDefinitionMap();
65
66 return (AuthorizationDefinition) authorizationDefinitionMap.get(action.getAuthorizationDefinitionId());
67 }
68
69 return null;
70 }
71
72 /***
73 * Returns the AuthorizationAction defined in the configuration that
74 * belongs to the specified action.
75 *
76 * @param config The StrutsAuthorizationConfig DTO
77 * @param action A simple Struts Action .
78 *
79 * @return AuthorizationAction The AuthorizationAction DTO.
80 */
81 public static AuthorizationAction getAuthorizationAction(
82 StrutsAuthorizationConfig config, Action action) {
83 Map authorizationActionMap = config.getAuthorizationActionMap();
84
85 return (AuthorizationAction) authorizationActionMap.get(action.getClass()
86 .getName());
87 }
88
89 /***
90 * Return true if the cancel button is pressed!
91 *
92 * @param request The Http request currently being processed.
93 *
94 * @return true if the cancel button has been pressed, otherwise false;
95 */
96 public static boolean isCancelled(HttpServletRequest request) {
97 return (request.getAttribute(Globals.CANCEL_KEY) != null);
98 }
99
100 /***
101 * Returns the AuthorizationDefinition DTO.
102 *
103 * @param config The StrutsAuthorizationConfig DTO
104 * @param action The AuthorizationAction DTO
105 * @param methodName The name of the method.
106 *
107 * @return AuthorizationDefinition The AuthorizationDefinition DTO.
108 */
109 public static AuthorizationDefinition getDefinedAuthorizationDefinitionMap(
110 HttpServletRequest request) {
111 HttpSession session = request.getSession();
112
113 return (AuthorizationDefinition) session.getAttribute(org.saf.struts.util.Globals.SAF_AUTHORIZATION_DEFINITION_KEY);
114 }
115
116 public static ActionForward getErrorForward(
117 StrutsAuthorizationConfig config, ActionMapping mapping, Action action,
118 String methodName) throws ServletException {
119 if (config == null) {
120 log.debug("SAF not correctly loaded");
121 throw new ServletException("SAF not correctly loaded");
122 }
123
124 Map errorForwardMap = config.getAuthorizationErrorForwardMap();
125 AuthorizationErrorForward errorForward = config.getAuthorizationErrorForward();
126 AuthorizationAction authAction = getAuthorizationAction(config, action);
127
128 if (authAction != null) {
129 if (authAction.getAuthorizationErrorForwardId() != null) {
130 errorForward = (AuthorizationErrorForward) errorForwardMap.get(authAction.getAuthorizationErrorForwardId());
131 }
132
133 Map authorizationMethodMap = authAction.getAuthorizationMethodMap();
134
135 if ((authorizationMethodMap != null) && (methodName != null)) {
136 AuthorizationMethod authorizationMethod = (AuthorizationMethod) authorizationMethodMap.get(methodName);
137
138 if ((authorizationMethod != null) &&
139 (authorizationMethod.getAuthorizationErrorForwardId() != null)) {
140 errorForward = (AuthorizationErrorForward) errorForwardMap.get(authorizationMethod.getAuthorizationErrorForwardId());
141 }
142 }
143 }
144
145 return mapping.findForward(errorForward.getForwardName());
146 }
147
148 /***
149 * Returns the AuthorizationDefinition DTO.
150 *
151 * @param config The StrutsAuthorizationConfig DTO
152 * @param action The AuthorizationAction DTO
153 * @param methodName The name of the method.
154 *
155 * @return AuthorizationDefinition The AuthorizationDefinition DTO.
156 */
157 public static AuthorizationDefinition getMethodAuthorizationDefinitionMap(
158 StrutsAuthorizationConfig config, AuthorizationAction action,
159 String methodName) {
160 Map authorizationMethodMap = action.getAuthorizationMethodMap();
161
162 if (authorizationMethodMap != null) {
163 AuthorizationMethod authorizationMethod = (AuthorizationMethod) authorizationMethodMap.get(methodName);
164
165 if ((authorizationMethod != null) &&
166 (authorizationMethod.getAuthorizationDefinitionId() != null)) {
167 Map authorizationDefinitionMap = config.getAuthorizationDefinitionMap();
168
169 return (AuthorizationDefinition) authorizationDefinitionMap.get(authorizationMethod.getAuthorizationDefinitionId());
170 }
171 }
172
173 return null;
174 }
175
176 /***
177 * Return the name of the method to be executed.
178 *
179 * @param mapping The ActionMapping object.
180 * @param request The HTTP servlet being processing.
181 *
182 * @return String The name of the mehtod.
183 *
184 * @throws ServletException Occurs when an error has been thrown while
185 * trying to lookup the method name.
186 */
187 public static String getMethodToExecute(ActionMapping mapping,
188 HttpServletRequest request) throws ServletException {
189 String parameter = mapping.getParameter();
190
191 if (parameter == null) {
192 log.debug(
193 "No parameter name found in Action setup, please check struts-config.xml");
194 throw new ServletException(
195 "No parameter name found in Action setup, please check struts-config.xml");
196 }
197
198 String methodName = (String) request.getParameter(parameter);
199
200 if (methodName == null) {
201 log.debug("No method name defined!");
202 throw new ServletException("No method name defined!");
203 }
204
205 return methodName;
206 }
207
208 /***
209 * Returns true if the plugin is available in the current configuration.
210 *
211 * @param context The ServletContext object
212 * @param config The ModuleConfig object
213 * @param clazz The Class object to obtain the name of the class
214 *
215 * @return boolean True if the plugin is available in the configuration.
216 *
217 * @throws ServletException Occurs when an error has been thrown while
218 * trying to look-up the specified plugin.
219 */
220 public static boolean isPlugInAvailable(ServletContext context,
221 ModuleConfig config, Class clazz) throws ServletException {
222 log.info("isPlugInAvailable - begin");
223
224 boolean available = getPlugInMap(context, config).containsKey(clazz.getName());
225
226 log.info("isPlugInAvailable - end");
227
228 return available;
229 }
230
231 /***
232 * Returns the PlugInConfig object for a given PlugIn class object.
233 *
234 * @param context The ServletContext object.
235 * @param config The ModuleConfig object.
236 * @param clazz The Class object needed to lookup the PlugInConfig.
237 *
238 * @return PlugInConfig The PlugInConfig object for a specified plugin
239 * class.
240 *
241 * @throws ServletException Occurs when an error has been thrown while
242 * searching for the pluginConfig.
243 */
244 public static PlugInConfig getPlugInConfig(ServletContext context,
245 ModuleConfig config, Class clazz) throws ServletException {
246 log.info("getPlugInConfig - begin");
247
248 PlugInConfig plugInConfig = (PlugInConfig) getPlugInConfigMap(context,
249 config).get(clazz.getName());
250
251 if (plugInConfig == null) {
252 throw new ServletException(
253 "Plugin not available in configuration : " + clazz.getName() +
254 " ==> classname");
255 }
256
257 log.info("getPlugInConfig - end");
258
259 return plugInConfig;
260 }
261
262 /***
263 * Returns the Map containing as key the plugin class name and as value the
264 * plugIn config objects.
265 *
266 * @param context The ServletContext object.
267 * @param config The ModuleConfig object.
268 *
269 * @return Map A Map containing all available PlugInConfig objects.
270 */
271 public static Map getPlugInConfigMap(ServletContext context,
272 ModuleConfig config) {
273 log.info("getPlugInConfigMap - begin");
274
275 Map plugInConfigMap = new HashMap();
276 PlugInConfig[] plugInConfigs = config.findPlugInConfigs();
277
278 if (plugInConfigs != null) {
279 for (int i = 0; i < plugInConfigs.length; i++) {
280 plugInConfigMap.put(plugInConfigs[i].getClassName(),
281 plugInConfigs[i]);
282 }
283 }
284
285 log.info("getPlugInConfigMap - end");
286
287 return plugInConfigMap;
288 }
289
290 /***
291 * Returns a Map with key = plugIn name and value the plugin class.
292 *
293 * @param context The ServletContext to get the stored plugIns.
294 * @param config The ModuleConfig to get the prefix.
295 *
296 * @return Map A Map with key = plugin name and value the plugin class.
297 *
298 * @throws ServletException Occurs when an error has been thrown while
299 * trying to get all the available plugins.
300 */
301 public static Map getPlugInMap(ServletContext context, ModuleConfig config)
302 throws ServletException {
303 log.info("getPlugInMap - begin");
304
305 Map plugInMap = new HashMap();
306
307 PlugIn[] plugIns = (PlugIn[]) context.getAttribute(Globals.PLUG_INS_KEY +
308 config.getPrefix());
309
310 if (plugIns != null) {
311 for (int i = 0; i < plugIns.length; i++) {
312 plugInMap.put(plugIns[i].getClass().getName(), plugIns[i]);
313 }
314 }
315
316 log.info("getPlugInMap - end");
317
318 return plugInMap;
319 }
320
321 /***
322 * Returns the request processor
323 *
324 * @param context The ServletContext object.
325 * @param config The ModuleConfig object.
326 *
327 * @return Class The RequestProcessor class object.
328 *
329 * @throws ServletException Occurs when an error has been thrown while
330 * searching for a request processor.
331 */
332 public static Class getRequestProcessor(ServletContext context,
333 ModuleConfig config) throws ServletException {
334 if (SafPluginUtils.isPlugInAvailable(context, config, TilesPlugin.class)) {
335 log.debug("assign the SSF Tiles RequestProcessor, please wait ...");
336
337 return SafTilesRequestProcessor.class;
338 } else {
339 log.debug("assign the SSF RequestProcessor, please wait ...");
340
341 return SafRequestProcessor.class;
342 }
343 }
344
345 /***
346 * Read the saf configuration file.
347 *
348 * @param actionServlet The ActionServlet object.
349 * @param moduleConfig The ModuleConfig object.
350 *
351 * @return StrutsSecurityConfig The StrutsSecurityConfig transfer object.
352 *
353 * @throws ServletException Occurs when an error has been thrown while
354 * trying to read the configuration file.
355 */
356 public static StrutsAuthorizationConfig readConfiguration(
357 ActionServlet actionServlet, ModuleConfig moduleConfig)
358 throws ServletException {
359 StrutsAuthorizationConfig config = new StrutsAuthorizationConfig();
360 PlugInConfig plugInConfig = SafPluginUtils.getPlugInConfig(actionServlet.getServletContext(),
361 moduleConfig, SafPlugin.class);
362
363 Map propertyMap = plugInConfig.getProperties();
364
365 String pathToConfigXmlFile = Constants.STRUTS_SECURITY_CONFIG_DEFAULT_XML;
366
367 if (propertyMap.containsKey(
368 Constants.STRUTS_SECURITY_CONFIG_DEFAULT_KEY)) {
369 pathToConfigXmlFile = (String) propertyMap.get(Constants.STRUTS_SECURITY_CONFIG_DEFAULT_KEY);
370 }
371
372 try {
373 log.info("Loading security roles files from '" +
374 pathToConfigXmlFile + "'");
375
376 URL url = actionServlet.getServletContext().getResource(pathToConfigXmlFile);
377 InputSource inputSource = new InputSource(url.toExternalForm());
378 InputStream inputStream = actionServlet.getServletContext()
379 .getResourceAsStream(pathToConfigXmlFile);
380 inputSource.setByteStream(inputStream);
381 config.readConfig(inputSource);
382 inputStream.close();
383 } catch (MalformedURLException e) {
384 log.error(
385 "Error occured while reading the security configuration xml file");
386 throw new ServletException("Error occured while reading the security configuration xml file",
387 e);
388 } catch (IOException e) {
389 log.error("Error occured while trying to close the inputstream of the configuration xml file",
390 e);
391 throw new ServletException("Error occured while trying to close the inputstream of the configuration xml file",
392 e);
393 }
394
395 return config;
396 }
397 }