Search in sources :

Example 41 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project webapp by elimu-ai.

the class EPubParagraphExtractionHelperTest method testExtractParagraphsFromChapterFile_FIL_LRA_7f877260_ch13.

@Test
public void testExtractParagraphsFromChapterFile_FIL_LRA_7f877260_ch13() throws IOException {
    ResourceLoader resourceLoader = new ClassRelativeResourceLoader(EPubParagraphExtractionHelper.class);
    Resource resource = resourceLoader.getResource("fil-lra-7f877260-ec7c-4970-b6e2-2ee41231d96d.epub_Page_13.xhtml");
    File xhtmlFile = resource.getFile();
    logger.debug("xhtmlFile: " + xhtmlFile);
    List<String> storyBookParagraphs = EPubParagraphExtractionHelper.extractParagraphsFromChapterFile(xhtmlFile);
    assertThat(storyBookParagraphs.size(), is(1));
    assertThat(storyBookParagraphs.get(0), is("\"Nagmumuni-muni lang,\" sabi niya."));
}
Also used : ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) Resource(org.springframework.core.io.Resource) File(java.io.File) Test(org.junit.Test)

Example 42 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method resolveBeanClass.

protected Class resolveBeanClass(AbstractBeanDefinition bd, String beanName) {
    if (bd.hasBeanClass()) {
        return bd.getBeanClass();
    }
    try {
        ResourceLoader rl = parserContext.getReaderContext().getResourceLoader();
        ClassLoader cl = rl != null ? rl.getClassLoader() : null;
        if (cl == null) {
            cl = parserContext.getReaderContext().getReader().getBeanClassLoader();
        }
        if (cl == null) {
            cl = Thread.currentThread().getContextClassLoader();
        }
        if (cl == null) {
            cl = getClass().getClassLoader();
        }
        return bd.resolveBeanClass(cl);
    } catch (ClassNotFoundException ex) {
        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Bean class [" + bd.getBeanClassName() + "] not found", ex);
    } catch (NoClassDefFoundError err) {
        throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Class that bean class [" + bd.getBeanClassName() + "] depends on not found", err);
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 43 with ResourceLoader

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

the class FreeMarkerConfigurerTests method freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    fcfb.setFreemarkerSettings(settings);
    fcfb.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();
        }
    });
    fcfb.afterPropertiesSet();
    assertThat(fcfb.getObject(), instanceOf(Configuration.class));
    Configuration fc = fcfb.getObject();
    Template ft = fc.getTemplate("test");
    assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) 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.Test)

Example 44 with ResourceLoader

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

the class ConfigFileApplicationListenerTests method loadCustomResource.

@Test
public void loadCustomResource() throws Exception {
    this.application.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(final String location) {
            if (location.equals("classpath:/custom.properties")) {
                return new ByteArrayResource("the.property: fromcustom".getBytes(), location) {

                    @Override
                    public String getFilename() {
                        return location;
                    }
                };
            }
            return null;
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    this.initializer.setSearchNames("custom");
    this.initializer.postProcessEnvironment(this.environment, this.application);
    String property = this.environment.getProperty("the.property");
    assertThat(property).isEqualTo("fromcustom");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 45 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project webapp by elimu-ai.

the class Mailer method sendHtmlWithButton.

public static void sendHtmlWithButton(String to, String cc, String from, String subject, String title, String text, String buttonText, String buttonUrl) {
    logger.info("sendHtmlWithButton");
    if (to.contains(",")) {
        to = to.replace(",", "");
    }
    if (to.contains(":")) {
        to = to.replace(":", "");
    }
    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    String smtpHost = ConfigHelper.getProperty("smtp.host");
    javaMailSenderImpl.setHost(smtpHost);
    MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotBlank(cc)) {
            mimeMessageHelper.setCc(cc);
        }
        mimeMessageHelper.setBcc(ADMIN_EMAIL);
        mimeMessageHelper.setSubject(subject);
        String html = "";
        ResourceLoader resourceLoader = new ClassRelativeResourceLoader(Mailer.class);
        logger.info("Loading file email_template_button.html...");
        Resource resource = resourceLoader.getResource("email_template_button.html");
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStream = resource.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("${subject}")) {
                    line = line.replace("${subject}", subject);
                }
                if (line.contains("${title}")) {
                    line = line.replace("${title}", title);
                }
                if (line.contains("${text}")) {
                    line = line.replace("${text}", text);
                }
                if (line.contains("${buttonText}")) {
                    line = line.replace("${buttonText}", buttonText);
                }
                if (line.contains("${buttonUrl}")) {
                    line = line.replace("${buttonUrl}", buttonUrl);
                }
                html += line;
            }
        } catch (IOException ex) {
            logger.error(null, ex);
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error(null, e);
                }
            }
        }
        mimeMessageHelper.setText(text, html);
        logger.info("Sending MIME message to " + to + " with subject \"" + subject + "\"...");
        logger.info("title: " + title);
        logger.info("text: " + text);
        logger.info("buttonText: " + buttonText);
        logger.info("buttonUrl: " + buttonUrl);
        if (EnvironmentContextLoaderListener.env != Environment.DEV) {
            javaMailSenderImpl.send(mimeMessage);
        }
    } catch (MessagingException ex) {
        logger.error(null, ex);
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) InputStreamReader(java.io.InputStreamReader) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

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