1 3 package jodd.madvoc; 4 5 import jodd.madvoc.config.AutomagicMadvocConfig; 6 import jodd.madvoc.config.MadvocConfig; 7 import jodd.util.ClassLoaderUtil; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 import javax.servlet.*; 12 import java.io.IOException ; 13 import java.lang.reflect.Constructor ; 14 15 18 public class MadvocDispatcher implements Filter { 19 20 protected FilterConfig filterConfig; 21 protected WebApplication webapp; 22 23 26 public void init(FilterConfig filterConfig) throws ServletException { 27 this.filterConfig = filterConfig; 28 webapp = loadWebApplication(filterConfig.getInitParameter("madvoc.webapp")); 29 MadvocConfig config = loadMadvocConfig(filterConfig.getInitParameter("madvoc.config")); 30 config.configure(webapp); 31 } 32 33 public void destroy() { 34 } 35 36 38 42 protected WebApplication loadWebApplication(String className) throws ServletException { 43 if (className == null) { 44 return new WebApplication(filterConfig.getServletContext()); 45 } 46 47 WebApplication webApp; 48 try { 49 Class clazz = ClassLoaderUtil.loadClass(className, this.getClass()); 50 Constructor ctor = clazz.getConstructor(ServletContext.class); 51 webApp = (WebApplication) ctor.newInstance(filterConfig.getServletContext()); 52 } catch (ClassCastException ccex) { 53 throw new ServletException("Class '" + className + "' is not a Madvoc WebApplication."); 54 } catch (Exception ex) { 55 throw new ServletException("Unable to load class '" + className + "'."); 56 } 57 return webApp; 58 } 59 60 64 protected MadvocConfig loadMadvocConfig(String className) throws ServletException { 65 if (className == null) { 66 return new AutomagicMadvocConfig(); 67 } 68 MadvocConfig config; 69 try { 70 Class clazz = ClassLoaderUtil.loadClass(className, this.getClass()); 71 config = (MadvocConfig) clazz.newInstance(); 72 } catch (ClassCastException ccex) { 73 throw new ServletException("Class '" + className + "' is not a Madvoc configuration."); 74 } catch (Exception ex) { 75 throw new ServletException("Unable to load class '" + className + "'."); 76 } 77 return config; 78 } 79 80 81 83 84 87 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException , ServletException { 88 HttpServletRequest request = (HttpServletRequest ) req; 89 HttpServletResponse response = (HttpServletResponse ) res; 90 91 ActionRequest actionRequest = webapp.buildActionRequest(request, response); 92 if (actionRequest == null) { 93 chain.doFilter(request, response); 95 return; 96 } 97 try { 98 String result = actionRequest.invoke(); 99 if (actionRequest.isRendered() == false) { 100 actionRequest.render(result); 101 } 102 } catch (Exception ex) { 103 throw new ServletException(ex); 104 } 105 } 106 107 } 108 | Popular Tags |