Search in sources :

Example 11 with UrlResource

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

the class ResourceWebHandlerTests method testInvalidPath.

private void testInvalidPath(HttpMethod httpMethod) throws Exception {
    Resource location = new ClassPathResource("test/", getClass());
    this.handler.setLocations(Collections.singletonList(location));
    testInvalidPath(httpMethod, "../testsecret/secret.txt", location);
    testInvalidPath(httpMethod, "test/../../testsecret/secret.txt", location);
    testInvalidPath(httpMethod, ":/../../testsecret/secret.txt", location);
    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(httpMethod, "file:" + secretPath, location);
    testInvalidPath(httpMethod, "/file:" + secretPath, location);
    testInvalidPath(httpMethod, "url:" + secretPath, location);
    testInvalidPath(httpMethod, "/url:" + secretPath, location);
    testInvalidPath(httpMethod, "////../.." + secretPath, location);
    testInvalidPath(httpMethod, "/%2E%2E/testsecret/secret.txt", location);
    testInvalidPath(httpMethod, "url:" + secretPath, location);
// The following tests fail with a MalformedURLException on Windows
// testInvalidPath(location, "/" + secretPath);
// testInvalidPath(location, "/  " + secretPath);
}
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) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 12 with UrlResource

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

the class PathResourceResolverTests method checkResource.

@Test
public void checkResource() throws IOException {
    Resource location = new ClassPathResource("test/", PathResourceResolver.class);
    testCheckResource(location, "../testsecret/secret.txt");
    testCheckResource(location, "test/../../testsecret/secret.txt");
    location = new UrlResource(getClass().getResource("./test/"));
    String secretPath = new UrlResource(getClass().getResource("testsecret/secret.txt")).getURL().getPath();
    testCheckResource(location, "file:" + secretPath);
    testCheckResource(location, "/file:" + secretPath);
    testCheckResource(location, "/" + secretPath);
    testCheckResource(location, "////../.." + secretPath);
    testCheckResource(location, "/%2E%2E/testsecret/secret.txt");
    testCheckResource(location, "/%2e%2e/testsecret/secret.txt");
    testCheckResource(location, " " + secretPath);
    testCheckResource(location, "/  " + secretPath);
    testCheckResource(location, "url:" + secretPath);
}
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) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 13 with UrlResource

use of org.springframework.core.io.UrlResource in project grails-core by grails.

the class StaticResourceLoaderTests method testGetResource.

public void testGetResource() throws Exception {
    StaticResourceLoader srl = new StaticResourceLoader();
    srl.setBaseResource(new UrlResource("http://grails.org"));
    Resource r = srl.getResource("/Home");
    assertEquals("http://grails.org/Home", r.getURL().toString());
}
Also used : StaticResourceLoader(org.grails.core.io.StaticResourceLoader) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource)

Example 14 with UrlResource

use of org.springframework.core.io.UrlResource in project grails-core by grails.

the class BinaryGrailsPlugin method initializeViewMap.

protected void initializeViewMap(BinaryGrailsPluginDescriptor descriptor) {
    final Resource descriptorResource = descriptor.getResource();
    Resource viewsPropertiesResource = null;
    try {
        viewsPropertiesResource = descriptorResource.createRelative(VIEWS_PROPERTIES);
    } catch (IOException e) {
    // ignore
    }
    if (viewsPropertiesResource == null || !viewsPropertiesResource.exists()) {
        try {
            String urlString = descriptorResource.getURL().toString();
            if (urlString.endsWith(PLUGIN_DESCRIPTOR_PATH)) {
                urlString = urlString.substring(0, urlString.length() - PLUGIN_DESCRIPTOR_PATH.length());
                URL newUrl = new URL(urlString + RELATIVE_VIEWS_PROPERTIES);
                viewsPropertiesResource = new UrlResource(newUrl);
            }
        } catch (IOException e) {
        // ignore
        }
    }
    if (viewsPropertiesResource == null || !viewsPropertiesResource.exists()) {
        return;
    }
    Properties viewsProperties = new Properties();
    InputStream input = null;
    try {
        input = viewsPropertiesResource.getInputStream();
        viewsProperties.load(input);
        for (Object view : viewsProperties.keySet()) {
            String viewName = view.toString();
            final String viewClassName = viewsProperties.getProperty(viewName);
            try {
                final Class<?> viewClass = grailsApplication.getClassLoader().loadClass(viewClassName);
                precompiledViewMap.put(viewName, viewClass);
            } catch (Throwable e) {
                throw new PluginException("Failed to initialize view [" + viewName + "] from plugin [" + getName() + "] : " + e.getMessage(), e);
            }
        }
    } catch (IOException e) {
        LOG.error("Error loading views for binary plugin [" + this + "]: " + e.getMessage(), e);
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) InputStream(java.io.InputStream) PluginException(grails.plugins.exceptions.PluginException) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) URL(java.net.URL)

Example 15 with UrlResource

use of org.springframework.core.io.UrlResource in project grails-core by grails.

the class CorePluginFinder method resolvePluginResources.

private Resource[] resolvePluginResources() throws IOException {
    Enumeration<URL> resources = application.getClassLoader().getResources(CORE_PLUGIN_PATTERN);
    List<Resource> resourceList = new ArrayList<>();
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        resourceList.add(new UrlResource(url));
    }
    return resourceList.toArray(new Resource[resourceList.size()]);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) URL(java.net.URL)

Aggregations

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