Search in sources :

Example 1 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime in project jodd by oblac.

the class ActionsManager method registerActionRuntime.

/**
 * Registers manually created {@link ActionRuntime action runtime configurations}.
 * Optionally, if action path with the same name already exist,
 * exception will be thrown.
 */
public ActionRuntime registerActionRuntime(final ActionRuntime actionRuntime) {
    final String actionPath = actionRuntime.getActionPath();
    final String method = actionRuntime.getActionMethod();
    log.debug("Madvoc action: " + ifNotNull(method, m -> m + " ") + actionRuntime.getActionPath() + " => " + actionRuntime.createActionString());
    final RouteChunk routeChunk = routes.registerPath(method, actionPath);
    if (routeChunk.value() != null) {
        // existing chunk
        if (detectDuplicatePathsEnabled) {
            throw new MadvocException("Duplicate action path for [" + actionRuntime + "] occupied by: [" + routeChunk.value() + "]");
        }
    } else {
        actionsCount++;
    }
    routeChunk.bind(actionRuntime);
    // finally
    runtimes.put(actionRuntime.createActionString(), actionRuntime);
    // async check
    if (actionRuntime.isAsync()) {
        asyncMode = true;
    }
    return actionRuntime;
}
Also used : ActionRuntime(jodd.madvoc.config.ActionRuntime) MadvocException(jodd.madvoc.MadvocException) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) MethodDescriptor(jodd.introspector.MethodDescriptor) ClassIntrospector(jodd.introspector.ClassIntrospector) ArrayList(java.util.ArrayList) List(java.util.List) Routes(jodd.madvoc.config.Routes) Map(java.util.Map) ActionDefinition(jodd.madvoc.config.ActionDefinition) Method(java.lang.reflect.Method) RouteChunk(jodd.madvoc.config.RouteChunk) StringUtil.ifNotNull(jodd.util.StringUtil.ifNotNull) PetiteInject(jodd.petite.meta.PetiteInject) RouteChunk(jodd.madvoc.config.RouteChunk) MadvocException(jodd.madvoc.MadvocException)

Example 2 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime in project jodd by oblac.

the class ResultsManager method lookup.

/**
 * Lookups for {@link jodd.madvoc.result.ActionResult action result handler}
 * based on current {@link jodd.madvoc.ActionRequest action request} and action method
 * result object.
 */
public ActionResult lookup(final ActionRequest actionRequest, final Object resultObject) {
    ActionResult actionResultHandler = null;
    // + read @RenderWith value on method
    {
        final ActionRuntime actionRuntime = actionRequest.getActionRuntime();
        final Class<? extends ActionResult> actionResultClass = actionRuntime.getActionResult();
        if (actionResultClass != null) {
            actionResultHandler = lookupAndRegisterIfMissing(actionResultClass);
        }
    }
    // + use @RenderWith value on resulting object if exist
    if (actionResultHandler == null && resultObject != null) {
        final RenderWith renderWith = resultObject.getClass().getAnnotation(RenderWith.class);
        if (renderWith != null) {
            actionResultHandler = lookupAndRegisterIfMissing(renderWith.value());
        } else if (resultObject instanceof ActionResult) {
            // special case - returned value is already the ActionResult
            actionResultHandler = (ActionResult) resultObject;
        }
    }
    // + use action configuration
    if (actionResultHandler == null) {
        final ActionRuntime actionRuntime = actionRequest.getActionRuntime();
        final Class<? extends ActionResult> actionResultClass = actionRuntime.getDefaultActionResult();
        if (actionResultClass != null) {
            actionResultHandler = lookupAndRegisterIfMissing(actionResultClass);
        }
    }
    if (actionResultHandler == null) {
        throw new MadvocException("ActionResult not found for: " + resultObject);
    }
    // set action result object into action request!
    actionRequest.bindActionResult(resultObject);
    return actionResultHandler;
}
Also used : RenderWith(jodd.madvoc.meta.RenderWith) ActionResult(jodd.madvoc.result.ActionResult) ActionRuntime(jodd.madvoc.config.ActionRuntime) MadvocException(jodd.madvoc.MadvocException)

Example 3 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime in project jodd by oblac.

the class ActionMethodParserTest method testClassesWithoutPackage.

@Test
void testClassesWithoutPackage() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class);
    RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class);
    rootPackages.addRootPackageOf(this.getClass());
    ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo");
    assertNotNull(cfg);
    assertEquals(Boo3Action.class, cfg.getActionClass());
    assertEquals("/bbb.foo", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo1");
    assertEquals("/bbb.xxx", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo2");
    assertEquals("/bbb.foo2.xxx", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo3");
    assertEquals("/bbb", cfg.getActionPath());
}
Also used : RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) ActionMethodParser(jodd.madvoc.component.ActionMethodParser) Test(org.junit.jupiter.api.Test)

Example 4 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime in project jodd by oblac.

the class ActionMethodParserTest method testEndSlashClassName.

@Test
void testEndSlashClassName() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class);
    RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class);
    rootPackages.addRootPackageOf(this.getClass());
    ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst2.ReAction#hello");
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/hello", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst2.ReAction#macro");
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/user/{id}/macro", cfg.getActionPath());
}
Also used : RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) ActionMethodParser(jodd.madvoc.component.ActionMethodParser) Test(org.junit.jupiter.api.Test)

Example 5 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime in project jodd by oblac.

the class ActionMethodParserTest method testClassesWithPackage.

@Test
void testClassesWithPackage() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class);
    RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class);
    rootPackages.addRootPackageOf(this.getClass());
    ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.Boo1Action#foo");
    assertNotNull(cfg);
    assertEquals(Boo1Action.class, cfg.getActionClass());
    assertEquals("/fixtures/tst/boo1.foo", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo");
    assertNotNull(cfg);
    assertEquals(Boo2Action.class, cfg.getActionClass());
    assertEquals("/fixtures/tst/bbb.foo", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo1");
    assertEquals("/fixtures/tst/bbb.xxx", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo2");
    assertEquals("/fixtures/tst/bbb.foo2.xxx", cfg.getActionPath());
    cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo3");
    assertEquals("/fixtures/tst/bbb", cfg.getActionPath());
}
Also used : RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) ActionMethodParser(jodd.madvoc.component.ActionMethodParser) Test(org.junit.jupiter.api.Test)

Aggregations

ActionRuntime (jodd.madvoc.config.ActionRuntime)32 Test (org.junit.jupiter.api.Test)24 ActionMethodParser (jodd.madvoc.component.ActionMethodParser)11 RootPackages (jodd.madvoc.component.RootPackages)10 ActionDefinition (jodd.madvoc.config.ActionDefinition)10 ActionsManager (jodd.madvoc.component.ActionsManager)9 WebApp (jodd.madvoc.WebApp)7 RouteChunk (jodd.madvoc.config.RouteChunk)5 Map (java.util.Map)2 ActionRequest (jodd.madvoc.ActionRequest)2 MadvocException (jodd.madvoc.MadvocException)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1