Search in sources :

Example 31 with UrlResource

use of org.springframework.core.io.UrlResource 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 32 with UrlResource

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

the class DynamicMetadataResolverAdapter method getResourceInputStream.

@Override
protected InputStream getResourceInputStream(final Resource resource, final String entityId) throws IOException {
    if (resource instanceof UrlResource && resource.getURL().toExternalForm().toLowerCase().endsWith("/entities/")) {
        final String encodedId = EncodingUtils.urlEncode(entityId);
        final URL url = new URL(resource.getURL().toExternalForm().concat(encodedId));
        LOGGER.debug("Locating metadata input stream for [{}] via [{}]", encodedId, url);
        final HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
        httpcon.setDoOutput(true);
        httpcon.addRequestProperty("Accept", "*/*");
        httpcon.setRequestMethod("GET");
        httpcon.connect();
        return httpcon.getInputStream();
    }
    return ClosedInputStream.CLOSED_INPUT_STREAM;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UrlResource(org.springframework.core.io.UrlResource) URL(java.net.URL)

Example 33 with UrlResource

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

the class CandidateComponentsIndexLoader method doLoadIndex.

private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {
    if (shouldIgnoreIndex) {
        return null;
    }
    try {
        Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
        if (!urls.hasMoreElements()) {
            return null;
        }
        List<Properties> result = new ArrayList<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
            result.add(properties);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + result.size() + "] index(es)");
        }
        int totalCount = result.stream().mapToInt(Properties::size).sum();
        return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
    } catch (IOException ex) {
        throw new IllegalStateException("Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex);
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) SpringProperties(org.springframework.core.SpringProperties) URL(java.net.URL)

Example 34 with UrlResource

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

the class ServletContextResourcePatternResolver method doRetrieveMatchingJarEntries.

/**
	 * Extract entries from the given jar by pattern.
	 * @param jarFilePath the path to the jar file
	 * @param entryPattern the pattern for jar entries to match
	 * @param result the Set of matching Resources to add to
	 */
private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching jar file [" + jarFilePath + "] for entries matching [" + entryPattern + "]");
    }
    try {
        JarFile jarFile = new JarFile(jarFilePath);
        try {
            for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
                JarEntry entry = entries.nextElement();
                String entryPath = entry.getName();
                if (getPathMatcher().match(entryPattern, entryPath)) {
                    result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR, ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
                }
            }
        } finally {
            jarFile.close();
        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Cannot search for matching resources in jar file [" + jarFilePath + "] because the jar cannot be opened through the file system", ex);
        }
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 35 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-boot by spring-projects.

the class AutoConfigurationMetadataLoader method loadMetadata.

static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) {
    try {
        Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(path) : ClassLoader.getSystemResources(path));
        Properties properties = new Properties();
        while (urls.hasMoreElements()) {
            properties.putAll(PropertiesLoaderUtils.loadProperties(new UrlResource(urls.nextElement())));
        }
        return loadMetadata(properties);
    } catch (IOException ex) {
        throw new IllegalArgumentException("Unable to load @ConditionalOnClass location [" + path + "]", ex);
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)68 Test (org.junit.Test)36 Resource (org.springframework.core.io.Resource)31 URL (java.net.URL)25 ArrayList (java.util.ArrayList)10 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 ClassPathResource (org.springframework.core.io.ClassPathResource)10 GenericBean (org.springframework.tests.sample.beans.GenericBean)9 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)8 InputStream (java.io.InputStream)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 JUnitDNSServer (org.opennms.core.test.dns.annotations.JUnitDNSServer)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 JAXBContext (javax.xml.bind.JAXBContext)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 BeansException (org.springframework.beans.BeansException)4