question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ContextFactory to extend Context with custom methods

See original GitHub issue

To extend a Context with application-specific methods, we could add the following to the Javalin config:

public interface ContextFactory {

    /** Create a new {@link io.javalin.http.Context} */
    public Context createContext(HttpServletRequest request, HttpServletResponse response, Map<Class<?>, Object> appAttributes);

}

The lambdas could directly receive the desired Context subclass by introducing generics to the Handler class:

public interface Handler<T extends Context> {
  void handle(@NotNull T ctx) throws Exception;
}

With this change, Handler lambdas can use directly a subclass of Context as needed. This is an example of use:

private static class MyContext extends Context {
  public String foo() {
      return "bar";
  }
}

private static class MyContextFactory implements ContextFactory {
  @Override
  public Context createContext(HttpServletRequest request, HttpServletResponse response, Map<Class<?>, Object> appAttributes) {
      return new MyContext(request, response, appAttributes);
  }
}

var app = Javalin.create(config -> config.contextFactory(new MyContextFactory())),
app.get("/test", (MyContext ctx) -> ctx.foo());

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
icolomacommented, Jul 17, 2020

In my case, for a personal project:

  • context.getCurrentUser() returns the current user, rehydrated from cookies
  • context.getUserLocale() returns the locale of the current user, or the preferred locale of the browser if none
  • Other classes that need to receive a Context in the constructor could be created from the Context instead, like a MustacheViewBuilder that automatically includes both current locale and current user.

I have seen other issues on GitHub addressing parameter conversion and validation (get an int parameter, throw 400 if needed) which would fit better as a context extension IMHO (the format of JSON error messages are quite often application-specific). Not my case, though.

0reactions
tipsycommented, Jun 15, 2021

I’m afraid I also fall on the side of not seeing enough value in this to implement it. Thanks for the issue and PR though 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add ContextFactory to extend Context with custom methods
To extend a Context with application-specific methods, we could add the following to the Javalin config: public interface ContextFactory ...
Read more >
Use both AddDbContextFactory() and AddDbContext ...
If you use the AddDbContext() extension method with it's default settings ... Transient); // add context factory, this uses the same options ...
Read more >
ContextFactory
Returns the set of custom resource IDs that the current JavaScript context is using. Methods inherited from class java.lang.Object · clone, equals, finalize, ......
Read more >
ContextFactory (Rhino) - Mozilla
Factory class that Rhino runtime uses to create new Context instances. ... class MyFactory extends ContextFactory { // Custom Context to store execution ......
Read more >
ContextFactory (Rhino) - javadoc.io
Factory class that Rhino runtime use to create new Context instances or to notify ... class MyFactory extends ContextFactory { // Custom Context...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found