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