Search in sources :

Example 76 with ResourceLoader

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

the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurerWithNonFileResourceLoaderPath() throws Exception {
    freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    freeMarkerConfigurer.setFreemarkerSettings(settings);
    freeMarkerConfigurer.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }
            return new ByteArrayResource("test".getBytes(), "test");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    freeMarkerConfigurer.afterPropertiesSet();
    assertThat(freeMarkerConfigurer.getConfiguration()).isInstanceOf(Configuration.class);
    Configuration fc = freeMarkerConfigurer.getConfiguration();
    Template ft = fc.getTemplate("test");
    assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 77 with ResourceLoader

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

the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurerWithNonFileResourceLoaderPath() throws Exception {
    freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    freeMarkerConfigurer.setFreemarkerSettings(settings);
    freeMarkerConfigurer.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }
            return new ByteArrayResource("test".getBytes(), "test");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    freeMarkerConfigurer.afterPropertiesSet();
    assertThat(freeMarkerConfigurer.getConfiguration()).isInstanceOf(Configuration.class);
    Configuration fc = freeMarkerConfigurer.getConfiguration();
    Template ft = fc.getTemplate("test");
    assertThat(FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())).isEqualTo("test");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test)

Example 78 with ResourceLoader

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

the class WebFluxConfigurationSupport method resourceHandlerMapping.

/**
 * Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
 * resource handlers. To configure resource handling, override
 * {@link #addResourceHandlers}.
 */
@Bean
public HandlerMapping resourceHandlerMapping(ResourceUrlProvider resourceUrlProvider) {
    ResourceLoader resourceLoader = this.applicationContext;
    if (resourceLoader == null) {
        resourceLoader = new DefaultResourceLoader();
    }
    ResourceHandlerRegistry registry = new ResourceHandlerRegistry(resourceLoader);
    registry.setResourceUrlProvider(resourceUrlProvider);
    addResourceHandlers(registry);
    AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
    if (handlerMapping != null) {
        configureAbstractHandlerMapping(handlerMapping, getPathMatchConfigurer());
    } else {
        handlerMapping = new EmptyHandlerMapping();
    }
    return handlerMapping;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) AbstractHandlerMapping(org.springframework.web.reactive.handler.AbstractHandlerMapping) Bean(org.springframework.context.annotation.Bean)

Example 79 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project dhis2-core by dhis2.

the class HelpAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    List<Locale> locales = localeManager.getLocalesOrderedByPriority();
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    for (Locale locale : locales) {
        String helpPage = helpPagePreLocale + locale.toString() + helpPagePostLocale;
        if (resourceLoader.getResource(helpPage) != null) {
            this.helpPage = helpPage;
            return SUCCESS;
        }
    }
    return SUCCESS;
}
Also used : Locale(java.util.Locale) ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 80 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-security by spring-projects.

the class WebXmlJ2eeDefinedRolesRetrieverTests method testGetZeroJ2eeRoles.

@Test
public void testGetZeroJ2eeRoles() throws Exception {
    final Resource webXml = new ClassPathResource("webxml/NoRoles.web.xml");
    WebXmlMappableAttributesRetriever rolesRetriever = new WebXmlMappableAttributesRetriever();
    rolesRetriever.setResourceLoader(new ResourceLoader() {

        @Override
        public ClassLoader getClassLoader() {
            return Thread.currentThread().getContextClassLoader();
        }

        @Override
        public Resource getResource(String location) {
            return webXml;
        }
    });
    rolesRetriever.afterPropertiesSet();
    Set<String> j2eeRoles = rolesRetriever.getMappableAttributes();
    assertThat(j2eeRoles).isEmpty();
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceLoader (org.springframework.core.io.ResourceLoader)90 Resource (org.springframework.core.io.Resource)51 Test (org.junit.Test)35 ClassRelativeResourceLoader (org.springframework.core.io.ClassRelativeResourceLoader)34 File (java.io.File)33 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)26 Test (org.junit.jupiter.api.Test)15 IOException (java.io.IOException)9 Environment (org.springframework.core.env.Environment)9 ByteArrayResource (org.springframework.core.io.ByteArrayResource)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 FileSystemResource (org.springframework.core.io.FileSystemResource)6 HashMap (java.util.HashMap)5 Configuration (freemarker.template.Configuration)4 Template (freemarker.template.Template)4 InputStream (java.io.InputStream)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 InputStreamReader (java.io.InputStreamReader)3 Properties (java.util.Properties)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3