-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Http endpoint base class with request context access (#18)
* feat: Http endpoint base class with request context access # Conflicts: # akka-javasdk/src/main/java/akka/javasdk/http/RequestContext.java # akka-javasdk/src/main/scala/akka/javasdk/impl/SdkRunner.scala * post rebase fixes * formatting...
- Loading branch information
1 parent
6cf5087
commit 1e6842d
Showing
13 changed files
with
119 additions
and
91 deletions.
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