Search in sources :

Example 1 with PolicyPlugin

use of io.gravitee.plugin.policy.PolicyPlugin in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceImpl method convert.

private PolicyEntity convert(PolicyPlugin policyPlugin, boolean withPlugin) {
    PolicyEntity entity = new PolicyEntity();
    entity.setId(policyPlugin.id());
    entity.setDescription(policyPlugin.manifest().description());
    entity.setName(policyPlugin.manifest().name());
    entity.setVersion(policyPlugin.manifest().version());
    if (withPlugin) {
        // Plugin information
        Plugin plugin = policyPlugin;
        PluginEntity pluginEntity = new PluginEntity();
        pluginEntity.setPlugin(plugin.clazz());
        pluginEntity.setPath(plugin.path().toString());
        pluginEntity.setType(plugin.type().toString().toLowerCase());
        pluginEntity.setDependencies(plugin.dependencies());
        entity.setPlugin(pluginEntity);
        // Policy development information
        PolicyDevelopmentEntity developmentEntity = new PolicyDevelopmentEntity();
        developmentEntity.setClassName(policyPlugin.policy().getName());
        /*
            if (policy.configuration() != null) {
                developmentEntity.setConfiguration(policyDefinition.configuration().getName());
            }

            if (policyDefinition.onRequestMethod() != null) {
                developmentEntity.setOnRequestMethod(policyDefinition.onRequestMethod().toGenericString());
            }

            if (policyDefinition.onResponseMethod() != null) {
                developmentEntity.setOnResponseMethod(policyDefinition.onResponseMethod().toGenericString());
            }
            */
        entity.setDevelopment(developmentEntity);
    }
    return entity;
}
Also used : PluginEntity(io.gravitee.management.model.PluginEntity) PolicyEntity(io.gravitee.management.model.PolicyEntity) PolicyDevelopmentEntity(io.gravitee.management.model.PolicyDevelopmentEntity) Plugin(io.gravitee.plugin.core.api.Plugin) PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin)

Example 2 with PolicyPlugin

use of io.gravitee.plugin.policy.PolicyPlugin in project gravitee-management-rest-api by gravitee-io.

the class PolicyServiceImpl method findById.

@Override
public PolicyEntity findById(String policyId) {
    LOGGER.debug("Find policy by ID: {}", policyId);
    PolicyPlugin policyDefinition = policyManager.get(policyId);
    if (policyDefinition == null) {
        throw new PolicyNotFoundException(policyId);
    }
    return convert(policyDefinition, true);
}
Also used : PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin) PolicyNotFoundException(io.gravitee.management.service.exceptions.PolicyNotFoundException)

Example 3 with PolicyPlugin

use of io.gravitee.plugin.policy.PolicyPlugin in project gravitee-gateway by gravitee-io.

the class RequestResponseInvalidContentTest method register.

@Override
public void register(PolicyPluginManager policyPluginManager) {
    super.register(policyPluginManager);
    PolicyPlugin errorRequestStreamPolicy = PolicyBuilder.build("content-request-error", ValidateRequestPolicy.class);
    policyPluginManager.register(errorRequestStreamPolicy);
    PolicyPlugin errorResponseStreamPolicy = PolicyBuilder.build("content-response-error", ValidateResponsePolicy.class);
    policyPluginManager.register(errorResponseStreamPolicy);
}
Also used : PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin)

Example 4 with PolicyPlugin

use of io.gravitee.plugin.policy.PolicyPlugin in project gravitee-gateway by gravitee-io.

the class TransformRequestContentUsingBuilderGatewayTest method register.

@Override
public void register(PolicyPluginManager policyPluginManager) {
    super.register(policyPluginManager);
    PolicyPlugin transformRequestContentPolicy = PolicyBuilder.build("transform-request-content", TransformRequestContentUsingBuilderPolicy.class);
    policyPluginManager.register(transformRequestContentPolicy);
}
Also used : PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin)

Example 5 with PolicyPlugin

use of io.gravitee.plugin.policy.PolicyPlugin in project gravitee-gateway by gravitee-io.

the class DefaultPolicyManager method initialize.

private void initialize() {
    PolicyPluginManager ppm = applicationContext.getBean(PolicyPluginManager.class);
    PolicyClassLoaderFactory pclf = applicationContext.getBean(PolicyClassLoaderFactory.class);
    ReactorHandler rh = applicationContext.getBean(ReactorHandler.class);
    ResourceLifecycleManager rm = applicationContext.getBean(ResourceLifecycleManager.class);
    Reactable reactable = rh.reactable();
    Set<Policy> requiredPlugins = reactable.dependencies(Policy.class);
    requiredPlugins.forEach(policy -> {
        final PolicyPlugin policyPlugin = ppm.get(policy.getName());
        if (policyPlugin == null) {
            logger.error("Policy [{}] can not be found in policy registry", policy.getName());
            throw new IllegalStateException("Policy [" + policy.getName() + "] can not be found in policy registry");
        }
        PluginClassLoader policyClassLoader = null;
        // Load dependant resources to enhance policy classloader
        Collection<? extends Resource> resources = rm.getResources();
        if (!resources.isEmpty()) {
            ClassLoader[] resourceClassLoaders = rm.getResources().stream().map(new Function<Resource, ClassLoader>() {

                @Override
                public ClassLoader apply(Resource resource) {
                    return resource.getClass().getClassLoader();
                }
            }).toArray(ClassLoader[]::new);
            DelegatingClassLoader parentClassLoader = new DelegatingClassLoader(rh.classloader(), resourceClassLoaders);
            policyClassLoader = pclf.getOrCreateClassLoader(policyPlugin, parentClassLoader);
        } else {
            policyClassLoader = pclf.getOrCreateClassLoader(policyPlugin, rh.classloader());
        }
        logger.debug("Loading policy {} for {}", policy.getName(), rh);
        PolicyMetadataBuilder builder = new PolicyMetadataBuilder();
        builder.setId(policyPlugin.id());
        try {
            // Prepare metadata
            Class<?> policyClass = ClassUtils.forName(policyPlugin.policy().getName(), policyClassLoader);
            builder.setPolicy(policyClass).setMethods(new PolicyMethodResolver().resolve(policyClass));
            if (policyPlugin.configuration() != null) {
                builder.setConfiguration((Class<? extends PolicyConfiguration>) ClassUtils.forName(policyPlugin.configuration().getName(), policyClassLoader));
            }
            // Prepare context if defined
            if (policyPlugin.context() != null) {
                Class<? extends PolicyContext> policyContextClass = (Class<? extends PolicyContext>) ClassUtils.forName(policyPlugin.context().getName(), policyClassLoader);
                // Create policy context instance and initialize context provider (if used)
                PolicyContext context = new PolicyContextFactory(reactable).create(policyContextClass);
                builder.setContext(context);
            }
            RegisteredPolicy registeredPolicy = new RegisteredPolicy();
            registeredPolicy.classLoader = policyClassLoader;
            registeredPolicy.metadata = builder.build();
            policies.put(policy.getName(), registeredPolicy);
        } catch (Exception ex) {
            logger.error("Unable to load policy metadata", ex);
            if (policyClassLoader != null) {
                try {
                    policyClassLoader.close();
                } catch (IOException ioe) {
                    logger.error("Unable to close classloader for policy", ioe);
                }
            }
        }
    });
}
Also used : Policy(io.gravitee.definition.model.Policy) Reactable(io.gravitee.gateway.reactor.Reactable) PolicyClassLoaderFactory(io.gravitee.plugin.policy.PolicyClassLoaderFactory) Function(java.util.function.Function) ReactorHandler(io.gravitee.gateway.reactor.handler.ReactorHandler) PolicyMethodResolver(io.gravitee.plugin.policy.internal.PolicyMethodResolver) PolicyContext(io.gravitee.policy.api.PolicyContext) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin) Resource(io.gravitee.resource.api.Resource) IOException(java.io.IOException) IOException(java.io.IOException) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) PolicyPluginManager(io.gravitee.plugin.policy.PolicyPluginManager)

Aggregations

PolicyPlugin (io.gravitee.plugin.policy.PolicyPlugin)15 PluginEntity (io.gravitee.management.model.PluginEntity)2 PolicyDevelopmentEntity (io.gravitee.management.model.PolicyDevelopmentEntity)2 PolicyEntity (io.gravitee.management.model.PolicyEntity)2 PolicyNotFoundException (io.gravitee.management.service.exceptions.PolicyNotFoundException)2 Plugin (io.gravitee.plugin.core.api.Plugin)2 PolicyPluginManager (io.gravitee.plugin.policy.PolicyPluginManager)2 IOException (java.io.IOException)2 Policy (io.gravitee.definition.model.Policy)1 Reactable (io.gravitee.gateway.reactor.Reactable)1 ReactorHandler (io.gravitee.gateway.reactor.handler.ReactorHandler)1 ResourceLifecycleManager (io.gravitee.gateway.resource.ResourceLifecycleManager)1 PolicyService (io.gravitee.management.service.PolicyService)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 PluginClassLoader (io.gravitee.plugin.core.api.PluginClassLoader)1 PolicyClassLoaderFactory (io.gravitee.plugin.policy.PolicyClassLoaderFactory)1 PolicyMethodResolver (io.gravitee.plugin.policy.internal.PolicyMethodResolver)1 PolicyContext (io.gravitee.policy.api.PolicyContext)1 Resource (io.gravitee.resource.api.Resource)1 Collection (java.util.Collection)1