use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class PathResourceResolverTests method ignoreInvalidEscapeSequence.
// gh-23463
@Test
public void ignoreInvalidEscapeSequence() throws IOException {
UrlResource location = new UrlResource(getClass().getResource("./test/"));
Resource resource = location.createRelative("test%file.txt");
assertThat(this.resolver.checkResource(resource, location)).isTrue();
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class PathResourceResolverTests method checkRelativeLocation.
// SPR-12624
@Test
public void checkRelativeLocation() throws Exception {
String location = new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
location = location.replace("/test/org/springframework", "/test/org/../org/springframework");
Resource actual = this.resolver.resolveResource(null, "main.css", Collections.singletonList(new UrlResource(location)), null);
assertThat(actual).isNotNull();
}
use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method testInvalidPath.
@Test
public void testInvalidPath() throws Exception {
// Use mock ResourceResolver: i.e. we're only testing upfront validations...
Resource resource = mock(Resource.class);
given(resource.getFilename()).willThrow(new AssertionError("Resource should not be resolved"));
given(resource.getInputStream()).willThrow(new AssertionError("Resource should not be resolved"));
ResourceResolver resolver = mock(ResourceResolver.class);
given(resolver.resolveResource(any(), any(), any(), any())).willReturn(resource);
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass())));
handler.setResourceResolvers(Collections.singletonList(resolver));
handler.setServletContext(new TestServletContext());
handler.afterPropertiesSet();
testInvalidPath("../testsecret/secret.txt", handler);
testInvalidPath("test/../../testsecret/secret.txt", handler);
testInvalidPath(":/../../testsecret/secret.txt", handler);
Resource location = new UrlResource(getClass().getResource("./test/"));
this.handler.setLocations(Collections.singletonList(location));
Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt"));
String secretPath = secretResource.getURL().getPath();
testInvalidPath("file:" + secretPath, handler);
testInvalidPath("/file:" + secretPath, handler);
testInvalidPath("url:" + secretPath, handler);
testInvalidPath("/url:" + secretPath, handler);
testInvalidPath("/../.." + secretPath, handler);
testInvalidPath("/%2E%2E/testsecret/secret.txt", handler);
testInvalidPath("/%2E%2E/testsecret/secret.txt", handler);
testInvalidPath("%2F%2F%2E%2E%2F%2F%2E%2E" + secretPath, handler);
}
use of org.springframework.core.io.UrlResource in project cloudstack by apache.
the class DefaultModuleDefinitionSet method getDefaultsContext.
protected ApplicationContext getDefaultsContext() {
URL config = DefaultModuleDefinitionSet.class.getResource(DEFAULT_CONFIG_XML);
ResourceApplicationContext context = new ResourceApplicationContext(new UrlResource(config));
context.setApplicationName("/defaults");
context.refresh();
@SuppressWarnings("unchecked") final List<Resource> resources = (List<Resource>) context.getBean(DEFAULT_CONFIG_RESOURCES);
withModule(new WithModule() {
@Override
public void with(ModuleDefinition def, Stack<ModuleDefinition> parents) {
for (Resource defaults : def.getConfigLocations()) {
resources.add(defaults);
}
}
});
configProperties = (Properties) context.getBean(DEFAULT_CONFIG_PROPERTIES);
for (Resource resource : resources) {
load(resource, configProperties);
}
for (Resource resource : (Resource[]) context.getBean(MODULE_PROPERITES)) {
load(resource, configProperties);
}
parseExcludes();
return context;
}
use of org.springframework.core.io.UrlResource in project dhis2-core by dhis2.
the class JCloudsAppStorageService method getAppResource.
@Override
public Resource getAppResource(App app, String pageName) throws IOException {
if (app == null || !app.getAppStorageSource().equals(AppStorageSource.JCLOUDS)) {
log.warn("Can't look up resource " + pageName + ". The specified app was not found in JClouds storage.");
return null;
}
String key = (app.getFolderName() + ("/" + pageName)).replaceAll("//", "/");
URI uri = getSignedGetContentUri(key);
if (uri == null) {
String filepath = configurationProvider.getProperty(ConfigurationKey.FILESTORE_CONTAINER) + "/" + key;
filepath = filepath.replaceAll("//", "/");
File res;
try {
res = locationManager.getFileForReading(filepath);
} catch (LocationManagerException e) {
return null;
}
if (res.isDirectory()) {
String indexPath = pageName.replaceAll("/+$", "") + "/index.html";
log.info("Resource " + pageName + " (" + filepath + " is a directory, serving " + indexPath);
return getAppResource(app, indexPath);
} else if (res.exists()) {
return new FileSystemResource(res);
} else {
return null;
}
}
return new UrlResource(uri);
}
Aggregations