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);
}
}
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;
}
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());
}
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文件异常");
}
}
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"));
}
Aggregations