-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: Http endpoint base class with request context access #18
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
akka-javasdk/src/main/java/akka/javasdk/http/AbstractHttpEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package akka.javasdk.http; | ||
|
||
import akka.annotation.InternalApi; | ||
|
||
/** | ||
* Optional base class for HTTP endpoints giving access to a request context without additional constructor parameters | ||
*/ | ||
abstract public class AbstractHttpEndpoint { | ||
|
||
volatile private RequestContext context; | ||
|
||
/** | ||
* INTERNAL API | ||
* | ||
* @hidden | ||
*/ | ||
@InternalApi | ||
final public void _internalSetRequestContext(RequestContext context) { | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* Always available from request handling methods, not available from the constructor. | ||
*/ | ||
protected final RequestContext requestContext() { | ||
if (context == null) { | ||
throw new IllegalStateException("The request context can only be accessed from the request handling methods of the endpoint."); | ||
} | ||
return context; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
samples/doc-snippets/src/main/java/com/example/jwt/HelloJwtAction.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
samples/doc-snippets/src/main/java/com/example/jwt/HelloJwtEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.jwt; | ||
|
||
import akka.javasdk.annotations.Acl; | ||
import akka.javasdk.annotations.http.HttpEndpoint; | ||
import akka.javasdk.http.AbstractHttpEndpoint; | ||
import akka.javasdk.annotations.JWT; | ||
|
||
// tag::bearer-token[] | ||
@HttpEndpoint("/example-jwt") // <1> | ||
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.ALL)) // <2> | ||
@JWT(validate = JWT.JwtMethodMode.BEARER_TOKEN, | ||
bearerTokenIssuers = "my-issuer") // <1> | ||
public class HelloJwtEndpoint extends AbstractHttpEndpoint { | ||
|
||
public String message(String msg) { | ||
//.. | ||
// end::bearer-token[] | ||
return "ok! Claims: " + String.join(",", requestContext().getJwtClaims().allClaimNames()); | ||
// tag::bearer-token[] | ||
} | ||
|
||
@JWT(validate = JWT.JwtMethodMode.BEARER_TOKEN, | ||
bearerTokenIssuers = "my-other-issuer") | ||
public String messageWithIssuer(String msg) { // <3> | ||
//.. | ||
// end::bearer-token[] | ||
return "ok! Claims: " + String.join(",", requestContext().getJwtClaims().allClaimNames()); | ||
// tag::bearer-token[] | ||
} | ||
} | ||
// end::bearer-token[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could call it
RequestContextSupport
.And maybe better if we make this a interface with default methods. As such users can use it as a mixin instead.
And have some other base classes on their own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a field so interface does not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, that's true.