Skip to content

Add RequestContextResolver Feature #204

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

Merged
merged 10 commits into from
Apr 18, 2023
Merged

Add RequestContextResolver Feature #204

merged 10 commits into from
Apr 18, 2023

Conversation

SentryMan
Copy link
Collaborator

@SentryMan SentryMan commented Apr 16, 2023

Adds a RequestContextResolver interface that lets you retrieve a javalin/helidon request object wherever it is wired. Useful for AOP and the like.

The default implementation uses ThreadLocal like a poor man's version of the upcoming ScopedValue. Since SV are in preview, we cannot add an implementation using them directly, so users will have to provide their own implementation of RequestContextResolver using SVs.

when enabled, the controller will generate like this.

@Generated("avaje-javalin-generator")
@Component
public class TestController2$Route implements WebRoutes {

  private final TestController2 controller;
  private final RequestContextResolver resolver;
  private final JsonType<String> stringJsonType;

  public TestController2$Route(
      TestController2 controller, Jsonb jsonb, RequestContextResolver resolver) {
    this.controller = controller;
    this.resolver = resolver;
    this.stringJsonType = jsonb.type(String.class);
  }

  @Override
  public void registerRoutes() {

    ApiBuilder.post("/test/strBody", ctx -> {
      ctx.status(201);
      var body = ctx.body();
      //for the execution of the supplier, the context is bound to the thread, and can be retrieved by the resolver class implementation.
      var result = resolver.supplyWith(new ServerContext(ctx,ctx), () -> controller.strBody(body));
      stringJsonType.toJson(result, ctx.contentType("application/json").outputStream());
    });
  }

}
  • adds ServerContext class for getting server request and response
  • adds RequestContextResolver interface for instrumenting server context inside a controller request
  • adds a default thread-local implementation of RequestContextResolver
  • adds an inject plugin to add a default resolver for inject applications
  • adds new property to HTTP annotations to enable request context instrumentation
  • modifies generators to instrument request context when enabled

Big thanks to @LoonyRules for helping me figure out how to implement this.

@SentryMan SentryMan changed the title Add HttpRequest Feature Add RequestContextResolver Feature Apr 16, 2023
@SentryMan SentryMan marked this pull request as ready for review April 16, 2023 03:18
@rob-bygrave
Copy link
Contributor

How does this API work for servers that use request + response rather than the single context parameter? resolver.supplyWith(ctx, ... is oriented to servers like Javalin that have a single context parameter.

@SentryMan
Copy link
Collaborator Author

Hmm, so it is

@SentryMan
Copy link
Collaborator Author

I'll modify the API to handle it

@SentryMan
Copy link
Collaborator Author

SentryMan commented Apr 17, 2023

I added a ServerContext class so that both can be retrieved

@SentryMan
Copy link
Collaborator Author

I'm good with most of this, but not sure about adding the inject plugin given that whole module issue. Do you think I should keep or remove it?

@rob-bygrave
Copy link
Contributor

Lets keep it and see how things evolve over the next week.

@rbygrave rbygrave merged commit 4928979 into avaje:master Apr 18, 2023
@rbygrave rbygrave added this to the 1.36 milestone Apr 18, 2023
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

Successfully merging this pull request may close these issues.

4 participants