Search in sources :

Example 6 with ActionConfig

use of jodd.madvoc.ActionConfig in project jodd by oblac.

the class MadvocController method invoke.

// ---------------------------------------------------------------- invoke
/**
	 * Invokes action registered to provided action path, Provides action chaining, by invoking the next action request.
	 * Returns <code>null</code> if action path is consumed and has been invoked by this controller; otherwise
	 * the action path string is returned (it might be different than original one, provided in arguments).
	 * On first invoke, initializes the action configuration before further proceeding.
	 */
public String invoke(String actionPath, HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    ActionRequest actionRequest = null;
    boolean characterEncodingSet = false;
    while (actionPath != null) {
        if (log.isDebugEnabled()) {
            log.debug("Action path: " + actionPath);
        }
        // build action path
        String httpMethod = servletRequest.getMethod().toUpperCase();
        actionPath = actionPathRewriter.rewrite(servletRequest, actionPath, httpMethod);
        // resolve action configuration
        ActionConfig actionConfig = actionsManager.lookup(actionPath, httpMethod);
        if (actionConfig == null) {
            return actionPath;
        }
        if (log.isDebugEnabled()) {
            log.debug("Invoking action path '" + actionPath + "' using " + actionConfig.actionClass.getSimpleName());
        }
        // set character encoding
        if (!characterEncodingSet && madvocConfig.isApplyCharacterEncoding()) {
            String encoding = madvocConfig.getEncoding();
            if (encoding != null) {
                servletRequest.setCharacterEncoding(encoding);
                servletResponse.setCharacterEncoding(encoding);
            }
            characterEncodingSet = true;
        }
        // create action object
        Object action = createAction(actionConfig.actionClass);
        // create action request
        ActionRequest previousRequest = actionRequest;
        actionRequest = createActionRequest(actionPath, actionConfig, action, servletRequest, servletResponse);
        actionRequest.setPreviousActionRequest(previousRequest);
        // invoke and render
        if (actionConfig.isAsync()) {
            AsyncContext asyncContext = servletRequest.startAsync();
            executor.execute(new ActionRequestInvoker(asyncContext, actionRequest));
        } else {
            actionRequest.invoke();
        }
        actionPath = actionRequest.getNextActionPath();
    }
    return null;
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) ActionRequest(jodd.madvoc.ActionRequest) AsyncContext(javax.servlet.AsyncContext)

Example 7 with ActionConfig

use of jodd.madvoc.ActionConfig in project jodd by oblac.

the class ActionPathMacroInjector method inject.

public void inject(ActionRequest actionRequest) {
    ActionConfig config = actionRequest.getActionConfig();
    ActionConfigSet set = config.getActionConfigSet();
    if (set.actionPathMacros == null) {
        // no action path macros at all, just exit
        return;
    }
    ScopeData[] injectData = lookupScopeData(actionRequest);
    if (injectData == null) {
        return;
    }
    // inject
    Target[] targets = actionRequest.getTargets();
    String[] names = set.actionPathMacros.getNames();
    String[] values = set.actionPathMacros.extract(actionRequest.getActionPath());
    for (int ndx = 0; ndx < values.length; ndx++) {
        String value = values[ndx];
        if (StringUtil.isEmpty(value)) {
            continue;
        }
        String macroName = names[ndx];
        for (int i = 0; i < targets.length; i++) {
            Target target = targets[i];
            if (injectData[i] == null) {
                continue;
            }
            ScopeData.In[] scopes = injectData[i].in;
            if (scopes == null) {
                continue;
            }
            for (ScopeData.In in : scopes) {
                String name = getMatchedPropertyName(in, macroName);
                if (name != null) {
                    setTargetProperty(target, name, value);
                }
            }
        }
    }
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) ActionConfigSet(jodd.madvoc.ActionConfigSet) ScopeData(jodd.madvoc.ScopeData)

Example 8 with ActionConfig

use of jodd.madvoc.ActionConfig in project jodd by oblac.

the class ServletDispatcherResultTest method createActionRequest.

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();
    ActionConfig actionConfig = new ActionConfig(Action.class, ReflectUtil.findMethod(Action.class, "view"), null, null, new ActionDef(actionPath, "GET"), null, false, null, null);
    return new ActionRequest(madvocController, actionConfig.getActionPath(), actionConfig, action, servletRequest, servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionConfig(jodd.madvoc.ActionConfig) MadvocController(jodd.madvoc.component.MadvocController) ActionRequest(jodd.madvoc.ActionRequest) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) ActionDef(jodd.madvoc.ActionDef)

Example 9 with ActionConfig

use of jodd.madvoc.ActionConfig in project jodd by oblac.

the class ActionsManagerTest method testActionPathMacrosWildcard.

@Test
public void testActionPathMacrosWildcard() {
    WebApplication webapp = new WebApplication(true);
    webapp.registerMadvocComponents();
    ActionsManager actionsManager = webapp.getComponent(ActionsManager.class);
    MadvocConfig madvocConfig = webapp.getComponent(MadvocConfig.class);
    madvocConfig.setPathMacroClass(WildcardPathMacros.class);
    actionsManager.register(FooAction.class, "one", new ActionDef("/${one:a?a}"));
    ActionConfig actionConfig = actionsManager.lookup("/aaa", null);
    assertNotNull(actionConfig);
    actionConfig = actionsManager.lookup("/aab", null);
    assertNull(actionConfig);
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) WebApplication(jodd.madvoc.WebApplication) ActionDef(jodd.madvoc.ActionDef) Test(org.junit.Test)

Example 10 with ActionConfig

use of jodd.madvoc.ActionConfig in project jodd by oblac.

the class ActionsManagerTest method testActionPathMacros2.

@Test
public void testActionPathMacros2() {
    WebApplication webapp = new WebApplication(true);
    webapp.registerMadvocComponents();
    ActionsManager actionsManager = webapp.getComponent(ActionsManager.class);
    actionsManager.register(FooAction.class, "one", new ActionDef("/${one}"));
    actionsManager.register(FooAction.class, "two", new ActionDef("/xxx-${two}"));
    ActionConfig actionConfig = actionsManager.lookup("/foo", null);
    assertEquals("one", actionConfig.actionClassMethod.getName());
    actionConfig = actionsManager.lookup("/foo/boo", null);
    assertNull(actionConfig);
    actionConfig = actionsManager.lookup("/xxx-foo", null);
    // best match!
    assertEquals("two", actionConfig.actionClassMethod.getName());
}
Also used : ActionConfig(jodd.madvoc.ActionConfig) WebApplication(jodd.madvoc.WebApplication) ActionDef(jodd.madvoc.ActionDef) Test(org.junit.Test)

Aggregations

ActionConfig (jodd.madvoc.ActionConfig)11 ActionDef (jodd.madvoc.ActionDef)7 WebApplication (jodd.madvoc.WebApplication)6 Test (org.junit.Test)6 ActionConfigSet (jodd.madvoc.ActionConfigSet)2 ActionRequest (jodd.madvoc.ActionRequest)2 AsyncContext (javax.servlet.AsyncContext)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 MadvocException (jodd.madvoc.MadvocException)1 ScopeData (jodd.madvoc.ScopeData)1 MadvocController (jodd.madvoc.component.MadvocController)1 RenderWith (jodd.madvoc.meta.RenderWith)1 ActionResult (jodd.madvoc.result.ActionResult)1 Result (jodd.madvoc.result.Result)1