Reputation: 25
I'm new to Play Framework. I'm working on a basic project and i'm working now on the authentication function. I want to redirect the unauthorized user to the /login route.
I discover the Global.java class that allows me to control actions accross my project, in particular with onRequest function. I'm planning of using it to do the redirection.
I search several solutions on the web but I couldn't find a working one.
My class:
import play.*;
import play.mvc.Action;
import play.mvc.*;
import play.mvc.Http.*;
import play.mvc.Result.*;
import play.libs.F.*;
import static play.mvc.Results.*;
import play.mvc.Http.Request;
import java.lang.reflect.Method;
public class Global extends GlobalSettings {
@Override
public Action onRequest(Request request, Method actionMethod) {
//Check if the user is connected
if (request.cookie("PLAY_SESSION") == null && !request.path().startsWith("/login")) {
System.out.println("UNAUTHORIZED");
return new Action.Simple() {
@Override
public Result call(Context ctx) throws Throwable {
return redirect(controllers.routes.Application.index());
}
};
}
return super.onRequest(request, actionMethod);
}
}
I found this and i don't understand why Play! doesn't want to compile :
error: <anonymous Global$1> is not abstract and does not override abstract method call(Context) in Action
error: method does not override or implement a method from a supertype
I'm not casual with Play and i don't really understand the problem. Can someone help me please ? Thanks !
Upvotes: 2
Views: 812
Reputation: 1582
F.Promise.pure() can be used to convert a Result (or anything that implements it, like Results.Status for example) to a Promise.
Example, where ok() returns play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));
Upvotes: 0
Reputation: 3031
I haven't used Play Framework for a while now but I think that the problem is that in 2.2 they made Action to return Promise and not just Result. Hence there is your problem.
Check your version of Action.Simple.call() that it matches
Result call(Context ctx) throws Throwable
See the difference between
https://www.playframework.com/documentation/2.2.x/api/java/index.html https://www.playframework.com/documentation/2.1.x/api/java/index.html
(look at the return type of the call method)
EDIT
I am not sure whether this is the best approach but it should work.
@Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
Upvotes: 3