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

How to enable Features based on Cookies or RequestParameters? #214

Closed
sivaprasadreddy opened this issue Apr 25, 2017 · 4 comments
Closed
Labels

Comments

@sivaprasadreddy
Copy link

I am looking to implement feature toggles based on some cookie value or request parameters. What would be the extension point to hookup the behaviour? Is providing a Cookie based StateRepository sufficient?

Any guidance would be helpful. Thanks in advance.

@neocotic
Copy link
Member

neocotic commented Apr 25, 2017

I imagine that you could create a custom ActivationStrategy (more info) that used HttpServletRequestHolder to get the current request. You'd need to depend on togglz-servlet and I believe then you just need to use the TogglzFilter or add some sort of filter to ensure that HttpServletRequestHolder has the correct request (whether using HttpServletRequestHolderListener or not).

@sivaprasadreddy
Copy link
Author

I tried to implement custom ActivationStrategy implementation. It seems the DefaultFeatureManager considers the Strategy only if the feature is enabled.

public boolean isActive(Feature feature) {

        Validate.notNull(feature, "feature is required");

        FeatureState state = stateRepository.getFeatureState(feature);

        if (state == null) {
            state = getMetaData(feature).getDefaultFeatureState();
        }

        if (state.isEnabled()) {

            // if no strategy is selected, the decision is simple
            String strategyId = state.getStrategyId();
            if (strategyId == null || strategyId.isEmpty()) {
                return true;
            }

            FeatureUser user = userProvider.getCurrentUser();

            // check the selected strategy
            for (ActivationStrategy strategy : strategyProvider.getActivationStrategies()) {
                if (strategy.getId().equalsIgnoreCase(strategyId)) {
                    return strategy.isActive(state, user);
                }
            }
        }

        // if the strategy was not found, the feature should be off
        return false;

    }

But there could be scenarios where the feature is disabled by default but i want to enable it based on cookie value. In this case it doesn't work. May be i need to provide custom implementation of FeatureManager too?

@chkal
Copy link
Member

chkal commented Apr 26, 2017

It is correct that activation strategies are only considered if the feature is on. So it works like this:

  • If the feature is disabled, the feature is off
  • If the feature is enabled and has no activation strategy, the feature is on
  • If the feature is enabled ans has an activation strategy, the strategy decides about on vs off.

You should be able to implement your use case with this. You just have to turn on the feature and select your strategy. Everything else (including the fallback behavior) is implemented in the strategy.

@gsteinacker
Copy link
Contributor

Seams that the question is answered, so I am closing the issue.

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

No branches or pull requests

4 participants