Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the book (F.Promise to CompletionStage) #27

Closed
eximius313 opened this issue Aug 29, 2016 · 7 comments
Closed

Update the book (F.Promise to CompletionStage) #27

eximius313 opened this issue Aug 29, 2016 · 7 comments
Assignees

Comments

@eximius313
Copy link

Here: https://leanpub.com/deadbolt-2/read it would be good to replace F.Promise<Optional<Result>> with CompletionStage<Optional<Result>>

@schaloner
Copy link
Collaborator

I'm in the process of updating everything to be Play 2.5 compliant. I'm aiming to publish the latest version in the next two weeks.

Thanks!

@schaloner schaloner self-assigned this Aug 29, 2016
@skiotterjeffa
Copy link

Hi Steve

I've used your auth0-integration example for Java from the book and am updating it for Deadbolt 2 2.5.1 and CompletionStage. I'm struggling with onAuthFailure, but everything else seems to compile. I haven't developed anything in over a decade, so I'm having to learn Lambdas, Futures, etc. I'm sure it is something simple. I'm happy to contribute the updates to this example if that is of value to you once I have it working.

Here is what I have.

public CompletionStage<Result> onAuthFailure(final Http.Context context,
                                           final Optional<String> s) {
        return getSubject(context)
                .thenApplyAsync(maybeSubject ->
                             maybeSubject.thenApplyAsync(subject -> Optional.of((User)subject))
                                         .thenApplyAsync(user -> new F.Tuple<>(true,
                                                                    denied.render(user)))
                                         .orElse(() -> new F.Tuple<>(false,  //orElseGet?
                                                                        login.render(clientId,
                                                                                     domain,
                                                                                     redirectUri))))
                .thenApplyAsync(subjectPresentAndContent -> subjectPresentAndContent._1
                                                 ? Results.forbidden(subjectPresentAndContent._2)
                                                 : Results.unauthorized(subjectPresentAndContent._2));
    }

Eclipse is telling me Type mismatch: cannot convert from CompletionStage to .

@schaloner
Copy link
Collaborator

Hi,

if it's been a while since you developed anything, it might be easier to use anonymous classes to get started. When using lambdas and chained calls, it can be really easy to lose what you're doing.

Alternatively, use local variables instead of chaining the calls. This will make it far easier to read.

For example, maybeSubject is an Optional, which does not have thenApplyAsync, etc, method - those belong to futures.

Here's the corrected example:

public CompletionStage<Result> onAuthFailure(final Http.Context context,
                                             final Optional<String> s) {
    return getSubject(context)
            .thenApplyAsync(maybeSubject ->
                                    maybeSubject.map(subject -> new F.Tuple<>(true,
                                                                              denied.render(subject)))
                                                .orElseGet(() -> new F.Tuple<>(false,
                                                                               login.render(clientId,
                                                                                            domain,
                                                                                            redirectUri))))
            .thenApplyAsync(subjectPresentAndContent -> subjectPresentAndContent._1
                                                        ? Results.forbidden(subjectPresentAndContent._2)
                                                        : Results.unauthorized(subjectPresentAndContent._2));
}

I didn't include the cast of subject to User to keep it simple, but let me know if you have issues with that.

I'm (slowly) updating the book for Play 2.5, including CompletionStage instead of Play's Future, Java's Optional instead of Play's F.Option, etc, and hope to have a new version out shortly.

Regards,
Steve

@skiotterjeffa
Copy link

Thanks Steve. Your suggestion of using local variables helped.

@eximius313
Copy link
Author

@schaloner, I see that in most sections there is CompletionStage already, but in section 7 there are still F.Promise<Boolean>. Is it on purpose?

@schaloner
Copy link
Collaborator

No, the revision is an ongoing process so there are a few hold-overs. I need to start working on the book again.

@mkurz
Copy link
Owner

mkurz commented Feb 28, 2018

We are tracking all documentation related issues in #89 now.

@mkurz mkurz closed this as completed Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants