action = createAction(actionRuntime.getActionClass()); final ActionRequest actionRequest = createActionRequest( actionPath, actionPathChunks,
/** * Inject context into target. */ public void injectContext(final Object targetObject) { final Class targetType = targetObject.getClass(); final ScopeData scopeData = scopeDataInspector.inspectClassScopesWithCache(targetType); final Targets targets = new Targets(targetObject, scopeData); // inject no context scopeResolver.forEachScope(madvocScope -> madvocScope.inject(targets)); // inject special case scopeResolver.forScope(ParamsScope.class, scope -> scope.inject(targets)); // inject servlet context final ServletContext servletContext = madvocController.getApplicationContext(); if (servletContext != null) { scopeResolver.forEachScope(madvocScope -> madvocScope.inject(servletContext, targets)); } }
/** * Builds {@link ActionRequest} and invokes it. If action result is a chain, it repeats the process. */ @Override public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String actionPath = DispatcherUtil.getServletPath(request); try { MadvocResponseWrapper madvocResponse = new MadvocResponseWrapper(response); actionPath = madvocController.invoke(actionPath, request, madvocResponse); } catch (Exception ex) { log.error("Invoking action path failed: " + actionPath, ex); throw new ServletException(ex); } if (actionPath != null) { // action path is not consumed boolean pathProcessed = processUnhandledPath(actionPath, req, res); if (!pathProcessed) { chain.doFilter(request, response); } } }
ActionConfig actionConfig = resolveActionConfig(actionPath, httpMethod); if (actionConfig == null) { return actionPath; Object action = createAction(actionConfig.actionClass); request = createActionRequest(actionPath, actionConfig, action, servletRequest, servletResponse); request.setPreviousActionRequest(previousRequest); invokeAndRender(request);
Object actionResult = actionRequest.invoke(); ActionRequest.this.madvocController.render(ActionRequest.this, actionResult);
protected ActionRequest createActionRequest(String actionPath) { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); HttpSession httpSession = mock(HttpSession.class); ServletContext servletContext = mock(ServletContext.class); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getServletContext()).thenReturn(servletContext); MadvocController madvocController = new MadvocController(); Object action = new Object(); ActionRuntime actionRuntime = new ActionRuntime( null, Action.class, ClassUtil.findMethod(Action.class, "view"), null, null, new ActionDefinition(actionPath, "GET"), ServletDispatcherActionResult.class, null, false, false, null, null); return new ActionRequest(madvocController, actionRuntime.getActionPath(), MadvocUtil.splitPathToChunks(actionRuntime.getActionPath()), actionRuntime, action, servletRequest, servletResponse); }
/** * Resolves action config from action path and http method. Returns <code>null</code> * if action config not found. Performs initialization of founded action config, * if necessary. */ protected ActionConfig resolveActionConfig(String actionPath, String httpMethod) { ActionConfig actionConfig = actionPathMapper.resolveActionConfig(actionPath, httpMethod); if (actionConfig != null) { if (actionConfig.initialized == false) { initializeActionConfig(actionConfig); } } return actionConfig; }
/** * Initializes action configuration on first use. Resolves all interceptors and injects context parameters. */ protected void initializeActionConfig(ActionConfig cfg) { Class<? extends ActionInterceptor>[] interceptorClasses = cfg.interceptorClasses; if (interceptorClasses == null) { interceptorClasses = madvocConfig.getDefaultInterceptors(); } cfg.interceptors = interceptorsManager.resolveAll(interceptorClasses); for (ActionInterceptor interceptor : cfg.interceptors) { if (interceptor.isInitialized() == false) { initializeInterceptor(interceptor); } } cfg.initialized(); }
initializeResult(result, req);
throw new MadvocException("No Madvoc controller component found."); madvocController.init(servletContext);
/** * Invokes action request (interceptors and action method) and renders result. */ protected void invokeAndRender(ActionRequest request) throws Exception { Object resultValueObject = request.invoke(); render(request, resultValueObject); }
private InterceptorsManager createInterceptorManager() { PetiteContainer madpc = new PetiteContainer(); madpc.addSelf("madpc"); madpc.addBean("madvocEncoding", new MadvocEncoding()); InterceptorsManager im = new InterceptorsManager(); im.contextInjectorComponent = new ContextInjectorComponent(); im.contextInjectorComponent.madvocController = new MadvocController(); im.contextInjectorComponent.madvocController.servletContextProvider = new ServletContextProvider(null); im.contextInjectorComponent.scopeDataInspector = new ScopeDataInspector(); im.contextInjectorComponent.scopeResolver = new ScopeResolver(); im.contextInjectorComponent.scopeResolver.madpc = madpc; return im; }
@Override public ServletContext getServletContext() { return this.madvocController.getApplicationContext(); }
/** * Builds {@link ActionRequest} and invokes it. If action result is a chain, it repeats the process. */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String actionPath = DispatcherUtil.getServletPath(request); try { actionPath = madvocController.invoke(actionPath, request, response); } catch (Exception ex) { log.error("Exception while invoking action path: " + actionPath, ex); ex.printStackTrace(); throw new ServletException(ex); } if (actionPath != null) { // action path is not consumed actionPath = processUnhandledPath(actionPath, req, res); if (actionPath != null) { chain.doFilter(request, response); } } }