Add ContextFactory to extend Context with custom methods
See original GitHub issueTo 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:
- Created 3 years ago
- Reactions:1
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In my case, for a personal project:
context.getCurrentUser()
returns the current user, rehydrated from cookiescontext.getUserLocale()
returns the locale of the current user, or the preferred locale of the browser if noneContext
in the constructor could be created from theContext
instead, like aMustacheViewBuilder
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.
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 😃