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);
}
}
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;
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations