Search in sources :

Example 46 with ResourceLoader

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

the class Mailer method sendHtml.

public static void sendHtml(String to, String cc, String from, String subject, String title, String text) {
    logger.info("sendPlainText");
    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.html...");
        Resource resource = resourceLoader.getResource("email_template.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);
                } else if (line.contains("${title}")) {
                    line = line.replace("${title}", title);
                } else if (line.contains("${text}")) {
                    line = line.replace("${text}", text);
                }
                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);
        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)

Example 47 with ResourceLoader

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

the class ConfigurationUtils method getRequiredResourceLoader.

/**
 * Returns the {@link ResourceLoader} from the given {@link XmlReaderContext}.
 *
 * @param context must not be {@literal null}.
 * @return
 * @throws IllegalArgumentException if no {@link ResourceLoader} can be obtained from the {@link XmlReaderContext}.
 */
public static ResourceLoader getRequiredResourceLoader(XmlReaderContext context) {
    Assert.notNull(context, "XmlReaderContext must not be null!");
    ResourceLoader resourceLoader = context.getResourceLoader();
    if (resourceLoader == null) {
        throw new IllegalArgumentException("Could not obtain ResourceLoader from XmlReaderContext!");
    }
    return resourceLoader;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader)

Example 48 with ResourceLoader

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

the class CustomRepositoryImplementationDetector method findCandidateBeanDefinitions.

Set<BeanDefinition> findCandidateBeanDefinitions(String className, Iterable<String> basePackages, Iterable<TypeFilter> excludeFilters) {
    // Build pattern to lookup implementation class
    Pattern pattern = Pattern.compile(".*\\." + className);
    // Build classpath scanner and lookup bean definition
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment);
    provider.setResourceLoader(resourceLoader);
    provider.setResourcePattern(String.format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, className));
    provider.setMetadataReaderFactory(metadataReaderFactory);
    provider.addIncludeFilter(new RegexPatternTypeFilter(pattern));
    excludeFilters.forEach(it -> provider.addExcludeFilter(it));
    return // 
    Streamable.of(basePackages).stream().flatMap(// 
    it -> provider.findCandidateComponents(it).stream()).collect(Collectors.toSet());
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) TypeFilter(org.springframework.core.type.filter.TypeFilter) NonNull(lombok.NonNull) Collection(java.util.Collection) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Set(java.util.Set) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) Environment(org.springframework.core.env.Environment) Streamable(org.springframework.data.util.Streamable) Optional(java.util.Optional) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Pattern(java.util.regex.Pattern) Nullable(javax.annotation.Nullable) Assert(org.springframework.util.Assert) Pattern(java.util.regex.Pattern) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter)

Example 49 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project jeesuite-libs by vakinge.

the class MybatisMapperParser method doParse.

private static synchronized void doParse() {
    if (!caches.isEmpty())
        return;
    try {
        ResourceLoader resourceLoader = InstanceFactory.getInstance(ResourceLoader.class);
        if (resourceLoader == null)
            resourceLoader = new DefaultResourceLoader();
        Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(mapperLocations);
        for (Resource resource : resources) {
            log.info(">begin parse mapper file:" + resource);
            parseMapperFile(resource.getFilename(), resource.getInputStream());
        }
    } catch (Exception e) {
        log.error("解析mapper文件异常", e);
        throw new RuntimeException("解析mapper文件异常");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Resource(org.springframework.core.io.Resource) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 50 with ResourceLoader

use of org.springframework.core.io.ResourceLoader in project spring-cloud-deployer by spring-cloud.

the class DelegatingResourceLoaderTests method test.

@Test
public void test() {
    NullResource one = new NullResource("one");
    NullResource two = new NullResource("two");
    NullResource three = new NullResource("three");
    assertNotEquals(one, two);
    assertNotEquals(two, three);
    Map<String, ResourceLoader> map = new HashMap<>();
    map.put("one", new StubResourceLoader(one));
    map.put("two", new StubResourceLoader(two));
    map.put("three", new StubResourceLoader(three));
    DelegatingResourceLoader resourceLoader = new DelegatingResourceLoader(map);
    assertEquals(one, resourceLoader.getResource("one://one"));
    assertEquals(two, resourceLoader.getResource("two://two"));
    assertEquals(three, resourceLoader.getResource("three://three"));
}
Also used : StubResourceLoader(org.springframework.cloud.deployer.resource.StubResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) StubResourceLoader(org.springframework.cloud.deployer.resource.StubResourceLoader) HashMap(java.util.HashMap) Test(org.junit.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