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.action.Action; 7 import org.apache.struts.action.ActionForm; 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.config.ModuleConfig; 12 13 import org.saf.struts.config.StrutsAuthorizationConfig; 14 import org.saf.struts.manager.AuthorizationManagerInterface; 15 import org.saf.struts.manager.BasicAuthorizationManager; 16 17 import java.lang.reflect.Constructor; 18 import java.lang.reflect.InvocationTargetException; 19 20 import javax.servlet.ServletException; 21 import javax.servlet.http.HttpServletRequest; 22 import javax.servlet.http.HttpServletResponse; 23 24 25 public class RequestProcessorUtils { 26 private final static Log log = LogFactory.getLog(RequestProcessorUtils.class); 27 private AuthorizationManagerInterface authorizationManagerInterface; 28 29 public RequestProcessorUtils(ActionServlet actionServlet, 30 ModuleConfig moduleConfig) throws ServletException { 31 SafContext context = SafContext.getInstance(); 32 StrutsAuthorizationConfig config = context.getConfig(); 33 34 Class authorizationManagerClass = BasicAuthorizationManager.class; 35 36 if (config.getAuthorizationManager() != null) { 37 try { 38 authorizationManagerClass = Class.forName(config.getAuthorizationManager() 39 .getType()); 40 } catch (ClassNotFoundException e) { 41 e.printStackTrace(); 42 } 43 } 44 45 try { 46 Constructor constructor = authorizationManagerClass.getConstructor(null); 47 authorizationManagerInterface = (AuthorizationManagerInterface) constructor.newInstance(null); 48 context.setAuthorizationManagerInterface(authorizationManagerInterface); 49 } catch (SecurityException e) { 50 throw new ServletException(e); 51 } catch (NoSuchMethodException e) { 52 throw new ServletException(e); 53 } catch (IllegalArgumentException e) { 54 throw new ServletException(e); 55 } catch (InstantiationException e) { 56 throw new ServletException(e); 57 } catch (IllegalAccessException e) { 58 throw new ServletException(e); 59 } catch (InvocationTargetException e) { 60 throw new ServletException(e); 61 } 62 } 63 64 public boolean isAuthorized(Action action, ActionMapping mapping, 65 ActionForm form, HttpServletRequest request, 66 HttpServletResponse response) throws ServletException { 67 log.info("isAuthorized - begin"); 68 69 boolean authorized = authorizationManagerInterface.isAuthorized(action, 70 mapping, form, request, response); 71 72 log.info("isAuthorized - end"); 73 74 return authorized; 75 } 76 77 public void doAfterAuthorization(Action action, ActionMapping mapping, 78 ActionForm form, HttpServletRequest request, 79 HttpServletResponse response) throws ServletException { 80 log.info("doAfterAuthorization - begin"); 81 82 authorizationManagerInterface.doAfterAuthorization(action, mapping, 83 form, request, response); 84 85 log.info("doAfterAuthorization - end"); 86 } 87 88 public ActionForward doAfterAuthorizationFailed(Action action, 89 ActionMapping mapping, ActionForm form, HttpServletRequest request, 90 HttpServletResponse response) throws ServletException { 91 log.info("doAfterAuthorizationFailed - begin"); 92 93 ActionForward forward = authorizationManagerInterface.doAfterAuthorizationFailed(action, 94 mapping, form, request, response); 95 96 log.info("doAfterAuthorizationFailed - end"); 97 98 return forward; 99 } 100 101 public void doBeforeAuthorization(Action action, ActionMapping mapping, 102 ActionForm form, HttpServletRequest request, 103 HttpServletResponse response) throws ServletException { 104 log.info("doBeforeAuthorization - begin"); 105 106 authorizationManagerInterface.doBeforeAuthorization(action, mapping, 107 form, request, response); 108 109 log.info("doBeforeAuthorization - end"); 110 } 111 }