玩! Framework 2.5如何在异步回调中返回delegate.call(ctx)

时间:2016-03-15 12:49:36

标签: java playframework frameworks delegates

我使用Action进行授权,我需要发出令牌验证请求,但是如何在验证成功后返回delegate.call(ctx)。

public class JWTSecureAction extends Action.Simple {

@Inject
WSClient ws;

public CompletionStage<Result> call(Http.Context ctx) {
    Result unauthorized = Results.unauthorized("unauthorized");
    String token = getTokenFromHeader(ctx);
    if (token != null) {
        WSRequest request = ws.url("")
                .setHeader(Http.HeaderNames.AUTHORIZATION, token);
        CompletionStage<JsonNode> jsonResponse = request.get()
                .thenApply(WSResponse::asJson);

        CompletionStage<Result> ret = jsonResponse.thenApply(jsonNode -> {
            if (jsonNode.get("success").equals("true")) {
                return delegate.call(ctx); //Error! CompletionStage<Result> cannot be Converted to Result
            } else {
                return unauthorized;
            }
        });
        return ret;
    }
    return CompletableFuture.completedFuture(unauthorized);
}

1 个答案:

答案 0 :(得分:2)

解决!

CompletionStage<Result> ret = jsonResponse.thenCompose(jsonNode -> {
            if (jsonNode.get("success").equals("true")) {
                return delegate.call(ctx);
            } else {
                return CompletableFuture.completedFuture(unauthorized);
            }
        });