Search in sources :

Example 21 with ClassRelativeResourceLoader

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

the class EPubMetadataExtractionHelperTest method testExtractTitleFromOpfFile_FIL_LETS_READ_ASIA_0acfe340.

@Test
public void testExtractTitleFromOpfFile_FIL_LETS_READ_ASIA_0acfe340() throws IOException {
    ResourceLoader resourceLoader = new ClassRelativeResourceLoader(EPubMetadataExtractionHelper.class);
    Resource resource = resourceLoader.getResource("fil-lra-0acfe340-6116-4f8a-a45d-c925c8a1fd0e.epub_content.opf");
    File opfFile = resource.getFile();
    logger.debug("opfFile: " + opfFile);
    String title = EPubMetadataExtractionHelper.extractTitleFromOpfFile(opfFile);
    assertThat(title, is("Hindi na Ako natatakot!"));
    String description = EPubMetadataExtractionHelper.extractDescriptionFromOpfFile(opfFile);
    assertThat(description, is("Ang batang si Marah,  mag-isang lumabas sa dilim.  Matatakot kaya siya?"));
}
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 22 with ClassRelativeResourceLoader

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

the class EPubMetadataExtractionHelperTest method testExtractTitleFromOpfFile_FIL_LETS_READ_ASIA_627f64f8.

@Test
public void testExtractTitleFromOpfFile_FIL_LETS_READ_ASIA_627f64f8() throws IOException {
    ResourceLoader resourceLoader = new ClassRelativeResourceLoader(EPubMetadataExtractionHelper.class);
    Resource resource = resourceLoader.getResource("fil-lra-627f64f8-a002-4c0f-8bb6-3701ce5fcf19.epub_content.opf");
    File opfFile = resource.getFile();
    logger.debug("opfFile: " + opfFile);
    String title = EPubMetadataExtractionHelper.extractTitleFromOpfFile(opfFile);
    assertThat(title, is("Si Pusa at si Aso at ang Sumbrero"));
    String description = EPubMetadataExtractionHelper.extractDescriptionFromOpfFile(opfFile);
    assertThat(description, is("Mayroong mga bagong sumbrero sina Pusa at Aso! Ngunit ano ang kanilang gagawin pagkatapos tangayin ng malakas  na hangin ang kani-kaniyang sumbrero sa tubig? Sana, mayroon silang kaibigan na nauuhaw."));
}
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 23 with ClassRelativeResourceLoader

use of org.springframework.core.io.ClassRelativeResourceLoader 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 24 with ClassRelativeResourceLoader

use of org.springframework.core.io.ClassRelativeResourceLoader 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)

Example 25 with ClassRelativeResourceLoader

use of org.springframework.core.io.ClassRelativeResourceLoader 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)

Aggregations

ClassRelativeResourceLoader (org.springframework.core.io.ClassRelativeResourceLoader)41 Resource (org.springframework.core.io.Resource)34 ResourceLoader (org.springframework.core.io.ResourceLoader)34 Test (org.junit.Test)33 File (java.io.File)32 Before (org.junit.Before)5 StreamSource (javax.xml.transform.stream.StreamSource)4 ResourceLoaderURIResolver (org.apereo.portal.xml.ResourceLoaderURIResolver)4 SimpleTransformErrorListener (org.springframework.util.xml.SimpleTransformErrorListener)4 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1