Search in sources :

Example 1 with PolicyConfiguration

use of io.gravitee.policy.api.PolicyConfiguration in project gravitee-gateway by gravitee-io.

the class DefaultPolicyManager method create.

@Override
public io.gravitee.gateway.policy.Policy create(StreamType streamType, String policy, String configuration) {
    PolicyMetadata policyMetadata = get(policy);
    if ((streamType == StreamType.ON_REQUEST && (policyMetadata.method(OnRequest.class) != null || policyMetadata.method(OnRequestContent.class) != null)) || (streamType == StreamType.ON_RESPONSE && (policyMetadata.method(OnResponse.class) != null || policyMetadata.method(OnResponseContent.class) != null))) {
        PolicyConfiguration policyConfiguration = policyConfigurationFactory.create(policyMetadata.configuration(), configuration);
        // TODO: this should be done only if policy is injectable
        Map<Class<?>, Object> injectables = new HashMap<>(2);
        injectables.put(policyMetadata.configuration(), policyConfiguration);
        if (policyMetadata.context() != null) {
            injectables.put(policyMetadata.context().getClass(), policyMetadata.context());
        }
        Object policyInst = policyFactory.create(policyMetadata, injectables);
        logger.debug("Policy {} has been added to the policy chain", policyMetadata.id());
        return PolicyImpl.target(policyInst).definition(policyMetadata).build();
    }
    return null;
}
Also used : HashMap(java.util.HashMap) PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration)

Example 2 with PolicyConfiguration

use of io.gravitee.policy.api.PolicyConfiguration in project gravitee-gateway by gravitee-io.

the class CachedPolicyConfigurationFactory method create.

@Override
public <T extends PolicyConfiguration> T create(Class<T> policyConfigurationClass, String configuration) {
    if (policyConfigurationClass == null) {
        return null;
    }
    String hash = hash(policyConfigurationClass, configuration);
    PolicyConfiguration config = cachedPolicyConfiguration.get(hash);
    if (config == null) {
        config = super.create(policyConfigurationClass, configuration);
        if (config != null) {
            cachedPolicyConfiguration.put(hash, config);
        }
    }
    return (T) config;
}
Also used : PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration)

Example 3 with PolicyConfiguration

use of io.gravitee.policy.api.PolicyConfiguration in project gravitee-gateway by gravitee-io.

the class CachedPolicyConfigurationFactoryTest method createNullPolicyClassFromCache.

@Test
public void createNullPolicyClassFromCache() {
    PolicyConfiguration policyConfiguration1 = policyConfigurationFactory.create(null, null);
    Assert.assertNull(policyConfiguration1);
}
Also used : PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration) Test(org.junit.Test)

Example 4 with PolicyConfiguration

use of io.gravitee.policy.api.PolicyConfiguration in project gravitee-gateway by gravitee-io.

the class PolicyConfigurationFactoryTest method createPolicyWithConfigurationAndWithoutConfigurationData.

@Test
public void createPolicyWithConfigurationAndWithoutConfigurationData() {
    PolicyConfiguration policyConfiguration = policyConfigurationFactory.create(DummyPolicyConfiguration.class, null);
    Assert.assertNull(policyConfiguration);
}
Also used : PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration) Test(org.junit.Test)

Example 5 with PolicyConfiguration

use of io.gravitee.policy.api.PolicyConfiguration in project gravitee-gateway by gravitee-io.

the class PolicyConfigurationFactoryTest method createPolicyWithConfigurationAndEmptyConfigurationData.

@Test
public void createPolicyWithConfigurationAndEmptyConfigurationData() {
    PolicyConfiguration policyConfiguration = policyConfigurationFactory.create(DummyPolicyConfiguration.class, "");
    Assert.assertNull(policyConfiguration);
}
Also used : PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration) Test(org.junit.Test)

Aggregations

PolicyConfiguration (io.gravitee.policy.api.PolicyConfiguration)5 Test (org.junit.Test)3 HashMap (java.util.HashMap)1