Skip to content
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

Using init params in filters #1035

Closed
mppfiles opened this issue Jul 22, 2020 · 4 comments
Closed

Using init params in filters #1035

mppfiles opened this issue Jul 22, 2020 · 4 comments

Comments

@mppfiles
Copy link
Contributor

With plain Java Servlets, it's possible to define servlet filters with external params, both in declarative (look for "example 2") and programmatic way. This increases reusability of filter classes.
I'm wondering if something similar is possible with ActiveWeb filters... cannot find any doc or code related to this.

@ipolevoy
Copy link
Member

@mppfiles since you would write a filter yourself, you can pass any parameters to its constructor. Here is an example https://github.com/javalite/javalite/blob/master/activeweb/src/main/java/org/javalite/activeweb/controller_filters/DBConnectionFilter.java#L70

for instance, you would configure such a filter like this:

public class AppControllerConfig extends AbstractControllerConfig {
    public void init(AppContext context) {
       add(new DBConnectionFilter("default", true)).exceptFor(HomeController.class, IntegrationsController.class, KbController.class, BlogController.class, CacheController.class);
        add(new DBConnectionFilter("events", false)).to(
                StatusController.class, 
                MessagesController.class,
                PdfController.class
        );
        add(new DBConnectionFilter("events", false)).to(
                StatusController.class, 
                MessagesController.class,
                CampaignsController.class, 
                AutoRespondersController.class,
                ViewOnlineController.class,
                ContactDetailsController.class,
                ContactsController.class,
                ExportController.class,
                PdfController.class
        );

    }
}

The code is from a real production app and shows that you can hook the same filter multiple times with different configurations.
Basically, this is just plain Java . You can create an instance of a filter, initialize it however you want and then you can add it to the app. This way, you know what and when you are getting, and cane debug if you want to.

@mppfiles
Copy link
Contributor Author

mppfiles commented Jul 22, 2020

This is great, it should be documented!

@ipolevoy
Copy link
Member

I thought this is clear since a developer using the framework would be writing and initializing the filters. I guess what is clear to us, might not be clear to an end user. We will document this. I'm closing this issue, but you can continue commenting if you have further questions.

@ipolevoy
Copy link
Member

ipolevoy commented Jul 22, 2020

one other note, you can use AppConfig to initialize filters differently depending on environment (development, production, etc.)

import static org.javalite.app_config.AppConfig.p;
public class AppControllerConfig extends AbstractControllerConfig {
    public void init(AppContext context) {
       add(new MyControllerFilter(p("domain_name"))); 
    }
}

For more info: https://javalite.io/app-config

In any case, this is just code, no magic...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants