Search in sources :

Example 16 with ActionRuntime

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

the class ActionsManagerTest method testActionPathMacros3.

@Test
void testActionPathMacros3() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class);
    actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/yyy-{one}"));
    actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/xxx-{two}"));
    assertEquals(2, actionsManager.getActionsCount());
    ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo"));
    assertNull(actionRuntime);
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/yyy-111"));
    assertEquals("one", actionRuntime.getActionClassMethod().getName());
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/xxx-222"));
    assertEquals("two", actionRuntime.getActionClassMethod().getName());
    try {
        actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/xxx-{two}"));
        fail("error");
    } catch (Exception ex) {
    // ignore
    }
}
Also used : ActionRuntime(jodd.madvoc.config.ActionRuntime) ActionDefinition(jodd.madvoc.config.ActionDefinition) WebApp(jodd.madvoc.WebApp) Test(org.junit.jupiter.api.Test)

Example 17 with ActionRuntime

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

the class ActionsManagerTest method testActionPathMacros4.

@Test
void testActionPathMacros4() {
    WebApp webapp = new WebApp();
    webapp.start();
    ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class);
    // no macro
    actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/dummy"));
    actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one}"));
    actionsManager.registerAction(FooAction.class, "three", new ActionDefinition("/life/{three}"));
    actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/{two}/{three}"));
    ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo"));
    assertEquals("one", actionRuntime.getActionClassMethod().getName());
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/scott/ramonna"));
    assertEquals("two", actionRuntime.getActionClassMethod().getName());
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/life/universe"));
    assertEquals("three", actionRuntime.getActionClassMethod().getName());
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/scott/ramonna/envy"));
    assertNull(actionRuntime);
    actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/life/universe/else"));
    assertNull(actionRuntime);
}
Also used : ActionRuntime(jodd.madvoc.config.ActionRuntime) ActionDefinition(jodd.madvoc.config.ActionDefinition) WebApp(jodd.madvoc.WebApp) Test(org.junit.jupiter.api.Test)

Example 18 with ActionRuntime

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

the class JoyMadvoc method printRoutes.

// ---------------------------------------------------------------- print
/**
 * Prints routes to console.
 */
protected void printRoutes(final int width) {
    final ActionsManager actionsManager = webApp.madvocContainer().lookupComponent(ActionsManager.class);
    final List<ActionRuntime> actions = actionsManager.getAllActionRuntimes();
    final Map<String, String> aliases = actionsManager.getAllAliases();
    if (actions.isEmpty()) {
        return;
    }
    final Print print = new Print();
    print.line("Routes", width);
    actions.stream().sorted(Comparator.comparing(actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())).forEach(ar -> {
        final String actionMethod = ar.getActionMethod();
        print.out(Chalk256.chalk().yellow(), actionMethod == null ? "*" : actionMethod, 7);
        print.space();
        final String signature = ClassUtil.getShortClassName(ProxettaUtil.resolveTargetClass(ar.getActionClass()), 2) + '#' + ar.getActionClassMethod().getName();
        print.outLeftRightNewLine(Chalk256.chalk().green(), ar.getActionPath(), Chalk256.chalk().blue(), signature, width - 7 - 1);
    });
    if (!aliases.isEmpty()) {
        print.line("Aliases", width);
        actions.stream().sorted(Comparator.comparing(actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())).forEach(ar -> {
            final String actionPath = ar.getActionPath();
            for (final Map.Entry<String, String> entry : aliases.entrySet()) {
                if (entry.getValue().equals(actionPath)) {
                    print.space(8);
                    print.outLeftRightNewLine(Chalk256.chalk().green(), entry.getValue(), Chalk256.chalk().blue(), entry.getKey(), width - 8);
                }
            }
        });
    }
    print.line(width);
}
Also used : ActionsManager(jodd.madvoc.component.ActionsManager) ProxettaAwareActionsManager(jodd.madvoc.proxetta.ProxettaAwareActionsManager) ActionRuntime(jodd.madvoc.config.ActionRuntime) Map(java.util.Map)

Example 19 with ActionRuntime

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

the class ActionMethodParser method createActionRuntime.

// ---------------------------------------------------------------- create action runtime
/**
 * Creates new instance of action runtime configuration.
 * Initialize caches.
 */
public ActionRuntime createActionRuntime(final ActionHandler actionHandler, final Class actionClass, final Method actionClassMethod, final Class<? extends ActionResult> actionResult, final Class<? extends ActionResult> defaultActionResult, final ActionFilter[] filters, final ActionInterceptor[] interceptors, final ActionDefinition actionDefinition, final boolean async, final boolean auth) {
    if (actionHandler != null) {
        return new ActionRuntime(actionHandler, actionClass, actionClassMethod, filters, interceptors, actionDefinition, NoneActionResult.class, NoneActionResult.class, async, auth, null, null);
    }
    final ScopeData scopeData = scopeDataInspector.inspectClassScopes(actionClass);
    // find ins and outs
    final Class[] paramTypes = actionClassMethod.getParameterTypes();
    final MethodParam[] params = new MethodParam[paramTypes.length];
    final Annotation[][] paramAnns = actionClassMethod.getParameterAnnotations();
    String[] methodParamNames = null;
    // for all elements: action and method arguments...
    for (int ndx = 0; ndx < paramTypes.length; ndx++) {
        final Class paramType = paramTypes[ndx];
        // lazy init to postpone bytecode usage, when method has no arguments
        if (methodParamNames == null) {
            methodParamNames = actionMethodParamNameResolver.resolveParamNames(actionClassMethod);
        }
        final String paramName = methodParamNames[ndx];
        final Annotation[] parameterAnnotations = paramAnns[ndx];
        final ScopeData paramsScopeData = scopeDataInspector.inspectMethodParameterScopes(paramName, paramType, parameterAnnotations);
        MapperFunction mapperFunction = null;
        for (final Annotation annotation : parameterAnnotations) {
            if (annotation instanceof Mapper) {
                mapperFunction = MapperFunctionInstances.get().lookup(((Mapper) annotation).value());
                break;
            }
        }
        params[ndx] = new MethodParam(paramTypes[ndx], paramName, scopeDataInspector.detectAnnotationType(parameterAnnotations), paramsScopeData, mapperFunction);
    }
    return new ActionRuntime(null, actionClass, actionClassMethod, filters, interceptors, actionDefinition, actionResult, defaultActionResult, async, auth, scopeData, params);
}
Also used : MethodParam(jodd.madvoc.config.MethodParam) MapperFunction(jodd.introspector.MapperFunction) ActionRuntime(jodd.madvoc.config.ActionRuntime) Annotation(java.lang.annotation.Annotation) Mapper(jodd.introspector.Mapper) ScopeData(jodd.madvoc.config.ScopeData)

Example 20 with ActionRuntime

use of jodd.madvoc.config.ActionRuntime 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 runtime before further proceeding.
 */
public String invoke(String actionPath, final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) throws Exception {
    final String originalActionPath = actionPath;
    boolean characterEncodingSet = false;
    while (actionPath != null) {
        // build action path
        final String httpMethod = servletRequest.getMethod().toUpperCase();
        if (log.isDebugEnabled()) {
            log.debug("Action path: " + httpMethod + " " + actionPath);
        }
        actionPath = actionPathRewriter.rewrite(servletRequest, actionPath, httpMethod);
        String[] actionPathChunks = MadvocUtil.splitPathToChunks(actionPath);
        // resolve action runtime
        ActionRuntime actionRuntime = actionsManager.lookup(httpMethod, actionPathChunks);
        if (actionRuntime == null) {
            // special case!
            if (actionPath.endsWith(welcomeFile)) {
                actionPath = actionPath.substring(0, actionPath.length() - (welcomeFile.length() - 1));
                actionPathChunks = MadvocUtil.splitPathToChunks(actionPath);
                actionRuntime = actionsManager.lookup(httpMethod, actionPathChunks);
            }
            if (actionRuntime == null) {
                return originalActionPath;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Invoke action for '" + actionPath + "' using " + actionRuntime.createActionString());
        }
        // set character encoding
        if (!characterEncodingSet && applyCharacterEncoding) {
            final String encoding = madvocEncoding.getEncoding();
            if (encoding != null) {
                servletRequest.setCharacterEncoding(encoding);
                servletResponse.setCharacterEncoding(encoding);
            }
            characterEncodingSet = true;
        }
        // create action object
        final Object action;
        if (actionRuntime.isActionHandlerDefined()) {
            action = actionRuntime.getActionHandler();
        } else {
            action = createAction(actionRuntime.getActionClass());
        }
        final ActionRequest actionRequest = createActionRequest(actionPath, actionPathChunks, actionRuntime, action, servletRequest, servletResponse);
        // invoke and render
        if (actionRuntime.isAsync()) {
            asyncActionExecutor.invoke(actionRequest);
        } else {
            actionRequest.invoke();
        }
        actionPath = actionRequest.getNextActionPath();
    }
    return null;
}
Also used : ActionRequest(jodd.madvoc.ActionRequest) ActionRuntime(jodd.madvoc.config.ActionRuntime)

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