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

Modified Files:
        NewsletterGenerator.java 
Removed Files:
        NewsletterGeneratorPlain.java NewsletterGeneratorHtml.java 
        NewsletterGeneratorFactory.java 
Log Message:
CMSC-725 service classes,some class refactory.


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


Index: NewsletterGenerator.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Portlets/portlets-newsletter/src/java/com/finalist/newsletter/generator/NewsletterGenerator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- NewsletterGenerator.java    2 Apr 2008 09:40:01 -0000       1.3
+++ NewsletterGenerator.java    15 Apr 2008 06:18:33 -0000      1.4
@@ -9,133 +9,53 @@
 
 import javax.mail.Message;
 import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.mail.internet.MimeMessage;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import net.sf.mmapps.modules.cloudprovider.CloudProviderFactory;
-
-import org.mmbase.bridge.Cloud;
-import org.mmbase.bridge.Node;
-import org.mmbase.module.Module;
+
 import org.mmbase.util.logging.Logger;
 import org.mmbase.util.logging.Logging;
 
 import com.finalist.cmsc.mmbase.PropertiesUtil;
-import com.finalist.cmsc.navigation.NavigationUtil;
+import com.finalist.newsletter.domain.Subscription;
+import com.finalist.newsletter.domain.Publication;
 
 public abstract class NewsletterGenerator {
 
    private static Logger log = 
Logging.getLoggerInstance(NewsletterGenerator.class.getName());
 
-   public static Session getMailSession() {
-      Module sendmailModule = Module.getModule("sendmail");
-      if (sendmailModule == null) {
-         log.fatal("Sendmail module not installed which is required for 
newsletter generation");
-         return null;
-      } else {
-         String context = sendmailModule.getInitParameter("context");
-         if (context == null) {
-            context = "java:comp/env";
-            log.warn("The property 'context' is missing, taking default " + 
context);
-         }
-         String dataSource = sendmailModule.getInitParameter("datasource");
-         if (dataSource == null) {
-            dataSource = "mail/Session";
-            log.warn("The property 'datasource' is missing, taking default " + 
dataSource);
-         }
-
-         Session session = null;
-         try {
-            Context initCtx = new InitialContext();
-            Context envCtx = (Context) initCtx.lookup(context);
-            Object o = envCtx.lookup(dataSource);
-            if (o instanceof Session) {
-               session = (javax.mail.Session) o;
-            } else {
-               log.fatal("Configured dataSource '" + dataSource + "' of 
context '" + context + "' is not a Session but "
-                     + (o == null ? "NULL" : "a " + o.getClass().getName()));
-               return null;
-            }
-         } catch (NamingException e) {
-            log.fatal("Configured dataSource '" + dataSource + "' of context 
'" + context + "' is not a Session ");
-            return null;
-         }
-         return session;
-      }
-   }
-
-   private int  publicationNumber;
-
-   public NewsletterGenerator(int  publicationNumber) {
-      this.publicationNumber = publicationNumber;
+   public static void generate(Message message, Publication publication, 
Subscription subscription) throws MessagingException {
+      String rawHtmlContent = getContent(publication, 
subscription.getMimeType());
+      message.setText(rawHtmlContent + "\n");
    }
 
-   protected String checkUrls(String input) {
+   protected static String checkUrls(String input) {
       String hostUrl = getLiveHostUrl();
       String appName = getApplicationName(hostUrl);
 
       String output = input;
       output = output.replaceAll("\"/" + appName, "\"/");
       output = output.replaceAll("\"/", hostUrl);
-      return (output);
-   }
-
-   public  Message generateNewsletterMessage(String userName) {
-      Session session = getMailSession();
-      Message message = new MimeMessage(session);
-      String rawHtmlContent = getContent(userName,getType());
-
-      if (rawHtmlContent != null) {
-         // BodyPart htmlBodyPart = new MimeBodyPart();
-         // Multipart content = new MimeMultipart();
-         try {
-            // content.addBodyPart(htmlBodyPart);
-            // message.setContent(content);
-            message.setText(rawHtmlContent + "\n");
-            message.setHeader("Content-type", getType());
-         } catch (MessagingException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+      return output;
          }
-      }
-
-      return (message);
-   }
-   
-   protected abstract String getType();
 
-   private String getApplicationName(String hostUrl) {
+   private static String getApplicationName(String hostUrl) {
       String[] hostUrlParts = hostUrl.split("/");
       String appName = hostUrlParts[hostUrlParts.length - 1];
       return (appName);
    }
 
-   protected String getContent(String userName,String type) {
-      Cloud cloud = CloudProviderFactory.getCloudProvider().getCloud();
-      Node publicationNode = cloud.getNode(publicationNumber);
+   protected static String getContent(Publication publication, String type) {
 
-      String hostUrl = getLiveHostUrl();
-      String newsletterPath = 
NavigationUtil.getPathToRootString(publicationNode, true);
-      String newsletterUrl = "".concat(hostUrl).concat(newsletterPath);
 
-      if (newsletterUrl != null && newsletterUrl.startsWith("http")) {
+      String inputString = "";
          try {
-            URL url = new URL(newsletterUrl);
-            URLConnection connection = url.openConnection();
-            ((HttpURLConnection) connection).setRequestMethod("GET");
+         URL url = new URL(publication.getUrl());
+         HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
+
+         connection.setRequestMethod("GET");
             connection.setDoInput(true);
-            if(type == null || type.trim().length() == 0) {
-               connection.setRequestProperty("Content-Type", "text/html");
-            }
-            else {
                connection.setRequestProperty("Content-Type", type);  
-            }
-            connection.setRequestProperty("username", userName);
-            InputStream input = connection.getInputStream();
-            Reader reader = new InputStreamReader(input);
+
+         Reader reader = new InputStreamReader(connection.getInputStream());
+
             StringBuffer buffer = new StringBuffer();
 
             int c;
@@ -143,25 +63,25 @@
                char character = (char) c;
                buffer.append("" + character);
             }
+
             reader.close();
-            String inputString = buffer.toString();
-            inputString = inputString.trim();
+
+         inputString = buffer.toString().trim();
             inputString = checkUrls(inputString);
             return (inputString);
          } catch (Exception e) {
             log.debug("Error");
          }
-      }
-      return (null);
+      return inputString;
    }
 
-   private String getLiveHostUrl() {
+   private static String getLiveHostUrl() {
       String hostUrl = PropertiesUtil.getProperty("host.live");
-      if (hostUrl != null) {
-         if (!hostUrl.endsWith("/")) {
+      if (hostUrl != null && !hostUrl.endsWith("/")) {
             hostUrl += "/";
          }
-      }
       return hostUrl;
    }
+
+
 }
\ No newline at end of file






_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to