Search in sources :

Example 1 with Message

use of org.b3log.latke.mail.MailService.Message in project solo by b3log.

the class ArticleCommentReplyNotifier method action.

@Override
public void action(final Event<JSONObject> event) throws EventException {
    final JSONObject eventData = event.getData();
    final JSONObject comment = eventData.optJSONObject(Comment.COMMENT);
    final JSONObject article = eventData.optJSONObject(Article.ARTICLE);
    LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] { event.getType(), eventData, ArticleCommentReplyNotifier.class.getName() });
    final String originalCommentId = comment.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
    if (Strings.isEmptyOrNull(originalCommentId)) {
        LOGGER.log(Level.DEBUG, "This comment[id={0}] is not a reply", comment.optString(Keys.OBJECT_ID));
        return;
    }
    if (Latkes.getServePath().contains("localhost")) {
        LOGGER.log(Level.INFO, "Solo runs on local server, so should not send mail");
        return;
    }
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
    final CommentRepository commentRepository = beanManager.getReference(CommentRepositoryImpl.class);
    try {
        final String commentEmail = comment.getString(Comment.COMMENT_EMAIL);
        final JSONObject originalComment = commentRepository.get(originalCommentId);
        final String originalCommentEmail = originalComment.getString(Comment.COMMENT_EMAIL);
        if (originalCommentEmail.equalsIgnoreCase(commentEmail)) {
            return;
        }
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            throw new EventException("Not found preference");
        }
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String adminEmail = preference.getString(Option.ID_C_ADMIN_EMAIL);
        final String commentContent = comment.getString(Comment.COMMENT_CONTENT);
        final String commentSharpURL = comment.getString(Comment.COMMENT_SHARP_URL);
        final Message message = new Message();
        message.setFrom(adminEmail);
        message.addRecipient(originalCommentEmail);
        final JSONObject replyNotificationTemplate = preferenceQueryService.getReplyNotificationTemplate();
        final String articleTitle = article.getString(Article.ARTICLE_TITLE);
        final String articleLink = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
        final String commentName = comment.getString(Comment.COMMENT_NAME);
        final String commentURL = comment.getString(Comment.COMMENT_URL);
        String commenter;
        if (!"http://".equals(commentURL)) {
            commenter = "<a target=\"_blank\" " + "href=\"" + commentURL + "\">" + commentName + "</a>";
        } else {
            commenter = commentName;
        }
        final String mailSubject = replyNotificationTemplate.getString("subject").replace("${postLink}", articleLink).replace("${postTitle}", articleTitle).replace("${replier}", commenter).replace("${blogTitle}", blogTitle).replace("${replyURL}", Latkes.getServePath() + commentSharpURL).replace("${replyContent}", commentContent);
        message.setSubject(mailSubject);
        final String mailBody = replyNotificationTemplate.getString("body").replace("${postLink}", articleLink).replace("${postTitle}", articleTitle).replace("${replier}", commenter).replace("${blogTitle}", blogTitle).replace("${replyURL}", Latkes.getServePath() + commentSharpURL).replace("${replyContent}", commentContent);
        message.setHtmlBody(mailBody);
        LOGGER.log(Level.DEBUG, "Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]", new Object[] { mailSubject, mailBody, originalCommentEmail });
        mailService.send(message);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        throw new EventException("Reply notifier error!");
    }
}
Also used : PreferenceQueryService(org.b3log.solo.service.PreferenceQueryService) JSONObject(org.json.JSONObject) Message(org.b3log.latke.mail.MailService.Message) EventException(org.b3log.latke.event.EventException) CommentRepository(org.b3log.solo.repository.CommentRepository) EventException(org.b3log.latke.event.EventException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 2 with Message

use of org.b3log.latke.mail.MailService.Message in project solo by b3log.

the class RepairProcessor method restoreSigns.

/**
     * Restores the signs of preference to default.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/restore-signs.do", method = HTTPRequestMethod.GET)
public void restoreSigns(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        final String originalSigns = preference.getString(Option.ID_C_SIGNS);
        preference.put(Option.ID_C_SIGNS, Option.DefaultPreference.DEFAULT_SIGNS);
        preferenceMgmtService.updatePreference(preference);
        // Sends the sample signs to developer
        final Message msg = new MailService.Message();
        msg.setFrom(preference.getString(Option.ID_C_ADMIN_EMAIL));
        msg.addRecipient("DL88250@gmail.com");
        msg.setSubject("Restore signs");
        msg.setHtmlBody(originalSigns + "<p>Admin email: " + preference.getString(Option.ID_C_ADMIN_EMAIL) + "</p>");
        MAIL_SVC.send(msg);
        renderer.setContent("Restores signs succeeded.");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Restores signs failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) Message(org.b3log.latke.mail.MailService.Message) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 3 with Message

use of org.b3log.latke.mail.MailService.Message in project solo by b3log.

the class PageCommentReplyNotifier method action.

@Override
public void action(final Event<JSONObject> event) throws EventException {
    final JSONObject eventData = event.getData();
    final JSONObject comment = eventData.optJSONObject(Comment.COMMENT);
    final JSONObject page = eventData.optJSONObject(Page.PAGE);
    LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] { event.getType(), eventData, PageCommentReplyNotifier.class.getName() });
    final String originalCommentId = comment.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
    if (Strings.isEmptyOrNull(originalCommentId)) {
        LOGGER.log(Level.DEBUG, "This comment[id={0}] is not a reply", comment.optString(Keys.OBJECT_ID));
        return;
    }
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
    final CommentRepository commentRepository = beanManager.getReference(CommentRepositoryImpl.class);
    try {
        final String commentEmail = comment.getString(Comment.COMMENT_EMAIL);
        final JSONObject originalComment = commentRepository.get(originalCommentId);
        final String originalCommentEmail = originalComment.getString(Comment.COMMENT_EMAIL);
        if (originalCommentEmail.equalsIgnoreCase(commentEmail)) {
            return;
        }
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            throw new EventException("Not found preference");
        }
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String adminEmail = preference.getString(Option.ID_C_ADMIN_EMAIL);
        final String commentContent = comment.getString(Comment.COMMENT_CONTENT);
        final String commentSharpURL = comment.getString(Comment.COMMENT_SHARP_URL);
        final Message message = new Message();
        message.setFrom(adminEmail);
        message.addRecipient(originalCommentEmail);
        final JSONObject replyNotificationTemplate = preferenceQueryService.getReplyNotificationTemplate();
        final String mailSubject = replyNotificationTemplate.getString("subject").replace("${blogTitle}", blogTitle);
        message.setSubject(mailSubject);
        final String pageTitle = page.getString(Page.PAGE_TITLE);
        final String pageLink = Latkes.getServePath() + page.getString(Page.PAGE_PERMALINK);
        final String commentName = comment.getString(Comment.COMMENT_NAME);
        final String commentURL = comment.getString(Comment.COMMENT_URL);
        String commenter;
        if (!"http://".equals(commentURL)) {
            commenter = "<a target=\"_blank\" " + "href=\"" + commentURL + "\">" + commentName + "</a>";
        } else {
            commenter = commentName;
        }
        final String mailBody = replyNotificationTemplate.getString("body").replace("${postLink}", pageLink).replace("${postTitle}", pageTitle).replace("${replier}", commenter).replace("${replyURL}", Latkes.getServePath() + commentSharpURL).replace("${replyContent}", commentContent);
        message.setHtmlBody(mailBody);
        LOGGER.log(Level.DEBUG, "Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]", new Object[] { mailSubject, mailBody, originalCommentEmail });
        mailService.send(message);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        throw new EventException("Reply notifier error!");
    }
}
Also used : PreferenceQueryService(org.b3log.solo.service.PreferenceQueryService) JSONObject(org.json.JSONObject) Message(org.b3log.latke.mail.MailService.Message) EventException(org.b3log.latke.event.EventException) CommentRepository(org.b3log.solo.repository.CommentRepository) EventException(org.b3log.latke.event.EventException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 4 with Message

use of org.b3log.latke.mail.MailService.Message in project latke by b3log.

the class MailServiceTestCase method testSendMail.

/**
 * Tests mail sending.
 *
 * @throws IOException if error
 * @throws InterruptedException s
 */
@Test
public void testSendMail() throws IOException, InterruptedException {
    System.out.println("testSendMail");
    final MailService mailService = MailServiceFactory.getMailService();
    final Message message = new Message();
    message.setFrom("b3log.solo@gmail.com");
    message.setSubject("Latke Mail Service[local] Test");
    message.setHtmlBody("<htmL><body>测试</body><html>");
    message.addRecipient("jiangzezhou1989@yahoo.com.cn");
    message.addRecipient("d@b3log.org");
    mailService.send(message);
    // Waiting for sending....
    Thread.sleep(T);
}
Also used : Message(org.b3log.latke.mail.MailService.Message) Test(org.testng.annotations.Test)

Aggregations

Message (org.b3log.latke.mail.MailService.Message)4 JSONObject (org.json.JSONObject)3 EventException (org.b3log.latke.event.EventException)2 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)2 CommentRepository (org.b3log.solo.repository.CommentRepository)2 PreferenceQueryService (org.b3log.solo.service.PreferenceQueryService)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)1 Test (org.testng.annotations.Test)1