Search in sources :

Example 76 with UrlResource

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();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) Test(org.junit.jupiter.api.Test)

Example 77 with UrlResource

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();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) Test(org.junit.jupiter.api.Test)

Example 78 with UrlResource

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);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 79 with UrlResource

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;
}
Also used : ModuleDefinition(org.apache.cloudstack.spring.module.model.ModuleDefinition) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) ResourceApplicationContext(org.apache.cloudstack.spring.module.context.ResourceApplicationContext) ArrayList(java.util.ArrayList) List(java.util.List) URL(java.net.URL)

Example 80 with UrlResource

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);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) FileSystemResource(org.springframework.core.io.FileSystemResource) URI(java.net.URI) ZipFile(java.util.zip.ZipFile) File(java.io.File) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)100 Resource (org.springframework.core.io.Resource)41 URL (java.net.URL)33 Test (org.junit.jupiter.api.Test)30 Test (org.junit.Test)24 ClassPathResource (org.springframework.core.io.ClassPathResource)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 FileSystemResource (org.springframework.core.io.FileSystemResource)13 MalformedURLException (java.net.MalformedURLException)11 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 lombok.val (lombok.val)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)9 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)9 File (java.io.File)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)8 InputStream (java.io.InputStream)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Properties (java.util.Properties)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5