Update of 
/var/cvs/contributions/CMSContainer_Portlets/portlets-newsletter/src/java/com/finalist/newsletter/publisher
In directory james.mmbase.org:/tmp/cvs-serv9195

Modified Files:
        NewsletterPublisher.java 
Added Files:
        HtmlBodyParser.java 
Log Message:
CMSC-1084, Newsletter: sending test email contains empty body,forbid empty 
content email sended


See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer_Portlets/portlets-newsletter/src/java/com/finalist/newsletter/publisher
See also: http://www.mmbase.org/jira/browse/CMSC-1084


HtmlBodyParser.java is new



Index: NewsletterPublisher.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Portlets/portlets-newsletter/src/java/com/finalist/newsletter/publisher/NewsletterPublisher.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- NewsletterPublisher.java    12 Nov 2008 08:59:40 -0000      1.37
+++ NewsletterPublisher.java    13 Nov 2008 08:03:30 -0000      1.38
@@ -51,60 +51,63 @@
 
    public void deliver(Publication publication, Subscription subscription) {
       try {
+         //if needed to prompt user this validate will be remove to Action
+         String originalBody = getBody(publication, subscription);
+         if 
(!containAticle(publication)||StringUtils.isBlank(HtmlBodyParser.html2text(originalBody)))
 {
+            log.error("the mail does not contain any aticle, please check your 
term or article !");
+            return;
+         } else {
          NewsletterService service = (NewsletterService) 
ApplicationContextFactory.getBean("newsletterServices");
          // Newsletter newsletter = 
service.getNewsletterBySubscription(subscription.getId());
          Newsletter newsletter = publication.getNewsletter();
          String replyAddress = newsletter.getReplyAddress();
          String toEmail = subscription.getEmail();
          Message message = new MimeMessage(getMailSession(toEmail, 
replyAddress));
-
-         setSenderInfomation(message, newsletter.getFromAddress(), 
newsletter.getFromName(), replyAddress, newsletter.getReplyName());
-
-         setContent(message, publication, subscription);
+            setSenderInfomation(message, newsletter.getFromAddress(), 
newsletter.getFromName(), replyAddress,
+                  newsletter.getReplyName());
+            setContent(message, publication, subscription, originalBody);
          setRecipient(message, subscription.getEmail());
          // setBody(publication, subscription, message);
          setTitle(message, newsletter.getTitle());
          // setMIME(message, subscription.getMimeType());
-
          Transport.send(message);
-         log.debug(String.format(
-                  "mail send! publication %s to %s in %s format",
-                  publication.getId(), subscription.getId(), 
subscription.getMimeType())
-         );
       }
-      catch (MessagingException e) {
+
+         log.debug(String.format("mail send! publication %s to %s in %s 
format", publication.getId(), subscription
+               .getId(), subscription.getMimeType()));
+      } catch (MessagingException e) {
          log.error(e);
          throw new NewsletterSendFailException(e);
-      }
-      catch (UnsupportedEncodingException e) {
+      } catch (UnsupportedEncodingException e) {
          log.error(e);
          throw new NewsletterSendFailException(e);
       }
    }
 
-   private void setContent(Message message, Publication publication,
-                           Subscription subscription) {
+   private void setContent(Message message, Publication 
publication,Subscription subscription,String originalBody) {
       Cloud cloud = CloudProviderFactory.getCloudProvider().getCloud();
       Node newsletterPublicationNode = cloud.getNode(publication.getId());
       NodeList attachmentNodes = 
newsletterPublicationNode.getRelatedNodes("attachments");
       Multipart multipart = new MimeMultipart();
-      MimeBodyPart mdp = new MimeBodyPart();
+      setBody(publication, subscription, multipart,originalBody);
+      setAttachment(multipart, attachmentNodes, MimeType.attachment);
+      NodeList imageNodes = 
newsletterPublicationNode.getRelatedNodes("images");
+      setAttachment(multipart, imageNodes, MimeType.image);
       try {
-         String type=subscription.getMimeType();
-         String o=getBody(publication, subscription);
-         log.info("the content will be sended:"+o+"\n "+"his type is :"+type);
-         mdp.setContent(o, type);
-         multipart.addBodyPart(mdp);
+         message.setContent(multipart);
       }
       catch (MessagingException e) {
          log.error(e);
       }
+   }
 
-      setAttachment(multipart, attachmentNodes, MimeType.attachment);
-      NodeList imageNodes = 
newsletterPublicationNode.getRelatedNodes("images");
-      setAttachment(multipart, imageNodes, MimeType.image);
+   private void setBody(Publication publication, Subscription subscription, 
Multipart multipart,String originalBody) {
+      MimeBodyPart mdp = new MimeBodyPart();
       try {
-         message.setContent(multipart);
+         String type=subscription.getMimeType();
+         log.info("the content will be sended:"+originalBody+"\n "+"his type 
is :"+type);
+         mdp.setContent(originalBody, type);
+         multipart.addBodyPart(mdp);
       }
       catch (MessagingException e) {
          log.error(e);
@@ -142,8 +145,7 @@
       }
    }
 
-   private String getBody(Publication publication, Subscription subscription)
-            throws MessagingException {
+   private String getBody(Publication publication, Subscription subscription) 
throws MessagingException {
 
       String url = NewsletterUtil.getTermURL(publication.getUrl(), 
subscription.getTerms(), publication.getId());
       ICache cache = null;
@@ -155,8 +157,7 @@
       }
       String content = " ";
       if ((subscription.getTerms() == null) || (subscription.getTerms().size() 
== 0) || !cache.contains(url)) {
-         int articleCounts = 
NewsletterUtil.countArticlesByNewsletter(publication.getNewsletterId());
-         if (articleCounts == 0 ) {            
+         if (!containAticle(publication)) {
             content = publication.getNewsletter().getTxtempty();
             log.info("the newsletter use textEmpty" + content);
          } else {
@@ -165,16 +166,25 @@
          }
          if (null != getPersonalise()) {
             content = getPersonalise().personalise(content, subscription, 
publication);
-            log.info("the content sended is Personalised :"+content);
+            log.info("the content sended is Personalised :" + content);
          }
          cache.add(url, content);
       } else {
          content = (String) cache.get(url);
-         log.info("the content sended is from the cache"+content);
+         log.info("the content sended is from the cache" + content);
       }
       return content + "\n";
    }
 
+   private boolean containAticle(Publication publication) {
+      int articleCounts = 
NewsletterUtil.countArticlesByNewsletter(publication.getNewsletterId());
+      if (articleCounts == 0 ) {            
+         return false;
+      }else{
+         return true;
+      }
+   }
+
    private void setSenderInfomation(Message message, String fromAddress, 
String fromName, String replyAddress, String replyName)
             throws MessagingException, UnsupportedEncodingException {
 
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to