Search in sources :

Example 1 with AbstractResource

use of org.springframework.core.io.AbstractResource in project cas by apereo.

the class FileSystemResourceMetadataResolver method supports.

@Override
public boolean supports(final SamlRegisteredService service) {
    try {
        final String metadataLocation = service.getMetadataLocation();
        final AbstractResource metadataResource = ResourceUtils.getResourceFrom(metadataLocation);
        return metadataResource instanceof FileSystemResource;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : AbstractResource(org.springframework.core.io.AbstractResource) FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 2 with AbstractResource

use of org.springframework.core.io.AbstractResource in project cas by apereo.

the class FileSystemResourceMetadataResolver method resolve.

@Override
public Collection<MetadataResolver> resolve(final SamlRegisteredService service) {
    try {
        final String metadataLocation = service.getMetadataLocation();
        LOGGER.info("Loading SAML metadata from [{}]", metadataLocation);
        final AbstractResource metadataResource = ResourceUtils.getResourceFrom(metadataLocation);
        final File metadataFile = metadataResource.getFile();
        final AbstractMetadataResolver metadataResolver;
        if (metadataFile.isDirectory()) {
            metadataResolver = new LocalDynamicMetadataResolver(new FilesystemLoadSaveManager<>(metadataFile, configBean.getParserPool()));
        } else {
            metadataResolver = new ResourceBackedMetadataResolver(ResourceHelper.of(metadataResource));
        }
        configureAndInitializeSingleMetadataResolver(metadataResolver, service);
        return CollectionUtils.wrap(metadataResolver);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : ResourceBackedMetadataResolver(org.opensaml.saml.metadata.resolver.impl.ResourceBackedMetadataResolver) AbstractMetadataResolver(org.opensaml.saml.metadata.resolver.impl.AbstractMetadataResolver) LocalDynamicMetadataResolver(org.opensaml.saml.metadata.resolver.impl.LocalDynamicMetadataResolver) ArrayList(java.util.ArrayList) FilesystemLoadSaveManager(org.opensaml.core.xml.persist.FilesystemLoadSaveManager) AbstractResource(org.springframework.core.io.AbstractResource) File(java.io.File)

Example 3 with AbstractResource

use of org.springframework.core.io.AbstractResource in project cas by apereo.

the class GroovyResourceMetadataResolver method resolve.

@Override
public Collection<MetadataResolver> resolve(final SamlRegisteredService service) {
    try {
        final String metadataLocation = service.getMetadataLocation();
        LOGGER.info("Loading SAML metadata via [{}]", metadataLocation);
        final AbstractResource metadataResource = ResourceUtils.getResourceFrom(metadataLocation);
        final Object[] args = { service, this.configBean, this.samlIdPProperties, LOGGER };
        final MetadataResolver metadataResolver = ScriptingUtils.executeGroovyScript(metadataResource, args, MetadataResolver.class);
        if (metadataResolver != null) {
            return CollectionUtils.wrap(metadataResolver);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : ArrayList(java.util.ArrayList) AbstractResource(org.springframework.core.io.AbstractResource) MetadataResolver(org.opensaml.saml.metadata.resolver.MetadataResolver)

Example 4 with AbstractResource

use of org.springframework.core.io.AbstractResource in project cas by apereo.

the class ChainingMetadataResolverCacheLoader method resolveMetadataFromResource.

/**
     * Resolve metadata from resource.
     *
     * @param service           the service
     * @param metadataResolvers the metadata resolvers
     * @throws Exception the io exception
     */
protected void resolveMetadataFromResource(final SamlRegisteredService service, final List<MetadataResolver> metadataResolvers) throws Exception {
    final String metadataLocation = service.getMetadataLocation();
    LOGGER.info("Loading SAML metadata from [{}]", metadataLocation);
    final AbstractResource metadataResource = ResourceUtils.getResourceFrom(metadataLocation);
    if (metadataResource instanceof FileSystemResource) {
        resolveFileSystemBasedMetadataResource(service, metadataResolvers, metadataResource);
    }
    if (metadataResource instanceof UrlResource) {
        resolveUrlBasedMetadataResource(service, metadataResolvers, metadataResource);
    }
    if (metadataResource instanceof ClassPathResource) {
        resolveClasspathBasedMetadataResource(service, metadataResolvers, metadataLocation, metadataResource);
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) AbstractResource(org.springframework.core.io.AbstractResource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 5 with AbstractResource

use of org.springframework.core.io.AbstractResource in project cas by apereo.

the class ScriptingUtils method executeGroovyScriptEngine.

/**
 * Execute groovy script engine t.
 *
 * @param <T>        the type parameter
 * @param scriptFile the script file
 * @param args       the args
 * @param clazz      the clazz
 * @return the t
 */
public static <T> T executeGroovyScriptEngine(final String scriptFile, final Object[] args, final Class<T> clazz) {
    try {
        final String engineName = getScriptEngineName(scriptFile);
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName(engineName);
        if (engine == null || StringUtils.isBlank(engineName)) {
            LOGGER.warn("Script engine is not available for [{}]", engineName);
            return null;
        }
        final AbstractResource resourceFrom = ResourceUtils.getResourceFrom(scriptFile);
        final File theScriptFile = resourceFrom.getFile();
        if (theScriptFile.exists()) {
            LOGGER.debug("Created object instance from class [{}]", theScriptFile.getCanonicalPath());
            engine.eval(Files.newBufferedReader(theScriptFile.toPath(), StandardCharsets.UTF_8));
            final Invocable invocable = (Invocable) engine;
            LOGGER.debug("Executing script's run method, with parameters [{}]", args);
            final Object result = invocable.invokeFunction("run", args);
            LOGGER.debug("Groovy script result is [{}]", result);
            if (result != null && !clazz.isAssignableFrom(result.getClass())) {
                throw new ClassCastException("Result [" + result + " is of type " + result.getClass() + " when we were expecting " + clazz);
            }
            return (T) result;
        }
        LOGGER.warn("[{}] script [{}] does not exist, or cannot be loaded", StringUtils.capitalize(engineName), scriptFile);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) GroovyObject(groovy.lang.GroovyObject) AbstractResource(org.springframework.core.io.AbstractResource) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Aggregations

AbstractResource (org.springframework.core.io.AbstractResource)9 IOException (java.io.IOException)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 FileSystemResource (org.springframework.core.io.FileSystemResource)2 UrlResource (org.springframework.core.io.UrlResource)2 GroovyObject (groovy.lang.GroovyObject)1 InputStream (java.io.InputStream)1 Invocable (javax.script.Invocable)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 Test (org.junit.jupiter.api.Test)1 FilesystemLoadSaveManager (org.opensaml.core.xml.persist.FilesystemLoadSaveManager)1 MetadataResolver (org.opensaml.saml.metadata.resolver.MetadataResolver)1 AbstractMetadataResolver (org.opensaml.saml.metadata.resolver.impl.AbstractMetadataResolver)1 FileBackedHTTPMetadataResolver (org.opensaml.saml.metadata.resolver.impl.FileBackedHTTPMetadataResolver)1 LocalDynamicMetadataResolver (org.opensaml.saml.metadata.resolver.impl.LocalDynamicMetadataResolver)1 ResourceBackedMetadataResolver (org.opensaml.saml.metadata.resolver.impl.ResourceBackedMetadataResolver)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1