Search in sources :

Example 6 with AbstractResource

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

the class GroovyRegisteredServiceUsernameProvider method resolveUsernameFromExternalGroovyScript.

private String resolveUsernameFromExternalGroovyScript(final Principal principal, final Service service, final String scriptFile) {
    try {
        LOGGER.debug("Found groovy script to execute");
        final AbstractResource resourceFrom = ResourceUtils.getResourceFrom(scriptFile);
        final String script = IOUtils.toString(resourceFrom.getInputStream(), StandardCharsets.UTF_8);
        final Object result = getGroovyAttributeValue(principal, script);
        if (result != null) {
            LOGGER.debug("Found username [{}] from script [{}]", result, scriptFile);
            return result.toString();
        }
    } catch (final IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    LOGGER.warn("Groovy script [{}] returned no value for username attribute. Fallback to default [{}]", this.groovyScript, principal.getId());
    return principal.getId();
}
Also used : AbstractResource(org.springframework.core.io.AbstractResource) IOException(java.io.IOException)

Example 7 with AbstractResource

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

the class UrlResourceMetadataResolver 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 backupFile = getMetadataBackupFile(metadataResource, service);
        final String canonicalPath = backupFile.getCanonicalPath();
        LOGGER.debug("Metadata backup file will be at [{}]", canonicalPath);
        FileUtils.forceMkdirParent(backupFile);
        cleanUpExpiredBackupMetadataFilesFor(metadataResource, service);
        final FileBackedHTTPMetadataResolver metadataProvider = new FileBackedHTTPMetadataResolver(this.httpClient.getWrappedHttpClient(), metadataResource.getURL().toExternalForm(), canonicalPath);
        configureAndInitializeSingleMetadataResolver(metadataProvider, service);
        return CollectionUtils.wrap(metadataProvider);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : FileBackedHTTPMetadataResolver(org.opensaml.saml.metadata.resolver.impl.FileBackedHTTPMetadataResolver) ArrayList(java.util.ArrayList) AbstractResource(org.springframework.core.io.AbstractResource) File(java.io.File) IOException(java.io.IOException)

Example 8 with AbstractResource

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

the class UrlResourceMetadataResolver method supports.

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

Example 9 with AbstractResource

use of org.springframework.core.io.AbstractResource in project spring-framework by spring-projects.

the class YamlMapFactoryBeanTests method testFirstFound.

@Test
public void testFirstFound() {
    this.factory.setResolutionMethod(YamlProcessor.ResolutionMethod.FIRST_FOUND);
    this.factory.setResources(new AbstractResource() {

        @Override
        public String getDescription() {
            return "non-existent";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            throw new IOException("planned");
        }
    }, new ByteArrayResource("foo:\n  spam: bar".getBytes()));
    assertThat(this.factory.getObject().size()).isEqualTo(1);
}
Also used : InputStream(java.io.InputStream) AbstractResource(org.springframework.core.io.AbstractResource) IOException(java.io.IOException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.jupiter.api.Test)

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