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;
}
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());
}
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());
}
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());
}
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();
}
}
Aggregations