Search in sources :

Example 1 with RouteChunk

use of jodd.madvoc.config.RouteChunk 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 RouteChunk

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

the class ActionMethodParserTest method testMacrosDups.

@Test
void testMacrosDups() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class);
    webapp.madvocContainer().lookupComponent(RootPackages.class).addRootPackageOf(this.getClass());
    actionsManager.setPathMacroClass(RegExpPathMacros.class);
    actionsManager.registerAction(ReAction.class, "duplo2", null);
    actionsManager.registerAction(ReAction.class, "duplo1", null);
    ActionRuntime cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/duplo/123"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/duplo/{id:^[0-9]+}", cfg.getActionPath());
    RouteChunk chunk = cfg.getRouteChunk();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/duplo/aaa"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/duplo/{sid}", cfg.getActionPath());
    chunk = cfg.getRouteChunk();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("sid", chunk.pathMacros().names()[0]);
    assertEquals(2, actionsManager.getActionsCount());
}
Also used : ActionsManager(jodd.madvoc.component.ActionsManager) RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) RouteChunk(jodd.madvoc.config.RouteChunk) Test(org.junit.jupiter.api.Test)

Example 3 with RouteChunk

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

the class ActionMethodParserTest method testMacrosWildcards.

@Test
void testMacrosWildcards() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class);
    RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class);
    rootPackages.addRootPackageOf(this.getClass());
    actionsManager.registerAction(ReAction.class, "wild1", null);
    actionsManager.registerAction(ReAction.class, "wild2", null);
    ActionRuntime cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/ild123cat"));
    assertNull(cfg);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/wild123ca"));
    assertNull(cfg);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/wild123cat.html"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/wild{id}cat", cfg.getActionPath());
    RouteChunk chunk = cfg.getRouteChunk();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/wild123dog.html"));
    assertNull(cfg);
    cfg = actionsManager.lookup("POST", MadvocUtil.splitPathToChunks("/re/wild123dog.html"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/wild{id}dog", cfg.getActionPath());
    assertEquals("POST", cfg.getActionMethod());
    chunk = cfg.getRouteChunk();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    assertEquals(2, actionsManager.getActionsCount());
}
Also used : ActionsManager(jodd.madvoc.component.ActionsManager) RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) RouteChunk(jodd.madvoc.config.RouteChunk) Test(org.junit.jupiter.api.Test)

Example 4 with RouteChunk

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

the class ActionMethodParserTest method testMacros.

@Test
void testMacros() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class);
    RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class);
    rootPackages.addRootPackageOf(this.getClass());
    actionsManager.registerAction(ReAction.class, "macro", null);
    ActionRuntime cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/user/173/macro.html"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/user/{id}/macro", cfg.getActionPath());
    RouteChunk chunk = cfg.getRouteChunk();
    assertNull(chunk.pathMacros());
    chunk = chunk.parent();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    assertNull(chunk.pathMacros().patterns()[0]);
    actionsManager.registerAction(ReAction.class, "macro2", null);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/user/image/173/png/macro2.html"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/user/image/{id}/{fmt}/macro2", cfg.getActionPath());
    chunk = cfg.getRouteChunk();
    assertNull(chunk.pathMacros());
    chunk = chunk.parent();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("fmt", chunk.pathMacros().names()[0]);
    chunk = chunk.parent();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    chunk = cfg.getRouteChunk();
    assertNull(chunk.pathMacros());
    actionsManager.registerAction(ReAction.class, "macro3", null);
    cfg = actionsManager.lookup("POST", MadvocUtil.splitPathToChunks("/re/users/173/macro3"));
    assertNotNull(cfg);
    assertEquals(ReAction.class, cfg.getActionClass());
    assertEquals("/re/users/{id}/macro3", cfg.getActionPath());
    assertEquals("POST", cfg.getActionMethod());
    chunk = cfg.getRouteChunk();
    assertNull(chunk.pathMacros());
    chunk = chunk.parent();
    assertEquals(1, chunk.pathMacros().macrosCount());
    assertEquals("id", chunk.pathMacros().names()[0]);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/user/index.html"));
    assertNull(cfg);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/user/index/reindex/macro.html"));
    assertNull(cfg);
    cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/users/173/macro3"));
    assertNull(cfg);
    assertEquals(3, actionsManager.getActionsCount());
}
Also used : ActionsManager(jodd.madvoc.component.ActionsManager) RootPackages(jodd.madvoc.component.RootPackages) ActionRuntime(jodd.madvoc.config.ActionRuntime) RouteChunk(jodd.madvoc.config.RouteChunk) Test(org.junit.jupiter.api.Test)

Example 5 with RouteChunk

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

the class ActionPathMacroInjector method inject.

public void inject(final ActionRequest actionRequest, final Targets targets) {
    final ActionRuntime actionRuntime = actionRequest.getActionRuntime();
    final RouteChunk routeChunk = actionRuntime.getRouteChunk();
    if (!routeChunk.hasMacrosOnPath()) {
        // no action path macros at all, just exit
        return;
    }
    // inject
    final String[] actionPath = actionRequest.getActionPathChunks();
    int ndx = actionPath.length - 1;
    RouteChunk chunk = routeChunk;
    while (chunk.parent() != null) {
        final PathMacros pathMacros = chunk.pathMacros();
        if (pathMacros != null) {
            injectMacros(actionPath[ndx], pathMacros, targets);
        }
        ndx--;
        chunk = chunk.parent();
    }
}
Also used : PathMacros(jodd.madvoc.macro.PathMacros) ActionRuntime(jodd.madvoc.config.ActionRuntime) RouteChunk(jodd.madvoc.config.RouteChunk)

Aggregations

ActionRuntime (jodd.madvoc.config.ActionRuntime)5 RouteChunk (jodd.madvoc.config.RouteChunk)5 ActionsManager (jodd.madvoc.component.ActionsManager)3 RootPackages (jodd.madvoc.component.RootPackages)3 Test (org.junit.jupiter.api.Test)3 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ClassIntrospector (jodd.introspector.ClassIntrospector)1 MethodDescriptor (jodd.introspector.MethodDescriptor)1 MadvocException (jodd.madvoc.MadvocException)1 ActionDefinition (jodd.madvoc.config.ActionDefinition)1 Routes (jodd.madvoc.config.Routes)1 PathMacros (jodd.madvoc.macro.PathMacros)1 PetiteInject (jodd.petite.meta.PetiteInject)1 StringUtil.ifNotNull (jodd.util.StringUtil.ifNotNull)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1