Question

From what I understand of Action composition, we can wrap multiple action classes around an action class so that the wrapped classes are called first before the action action class

so, for eg:

@With(a.class, b.class)
public static Result index() {
  return ok("It works!");
}

public static Result logout(){
  session().clear();
}

Any call to index will first go through the call() methods of a & b. The key thing here is that at some point of time, the control needs to be delegated to the index() method.

My question is; is it possible to change the call from index to logout in either a or b.

I tried calling controller.Application.logout() instead of delegate.call but the logout method doesnt get a handle on the context. Any ideas?

Was it helpful?

Solution

I don't believe you can change the method the call gets delegate back to, but you can just do a redirect to your logout route.

if (...)
   return delegate.call(ctx);
else
   return redirect(routes.Controller.logout()); //reverse-route to your logout() method

Alternatively, I think you could do something like this where you pass the Context to your logout method.

// -- Action.class
return Controller.doLogout(ctx);

// -- Controller.java
public static Result logout()
{
  // defined in your routes file but it just calls doLogout
  return doLogout(ctx());
}

public static Result doLogout(Context ctx)
{
  // this method does your actual "logout" process
  ctx.session().clear();
  return ...;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top