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

Modified Files:
        NewsletterGenerator.java 
Log Message:
CMSC-1187 - Improve sending and retrieving HTML part. Better and faster code.


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-1187


Index: NewsletterGenerator.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer_Portlets/portlets-newsletter/src/java/com/finalist/newsletter/publisher/NewsletterGenerator.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -b -r1.17 -r1.18
--- NewsletterGenerator.java    24 Oct 2008 07:59:37 -0000      1.17
+++ NewsletterGenerator.java    4 Mar 2009 17:21:15 -0000       1.18
@@ -1,10 +1,11 @@
 package com.finalist.newsletter.publisher;
 
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStreamReader;
-import java.io.Reader;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import javax.mail.MessagingException;
 
 import org.htmlparser.Parser;
 import org.htmlparser.util.ParserException;
@@ -19,19 +20,19 @@
    private static Logger log = 
Logging.getLoggerInstance(NewsletterGenerator.class.getName());
 
 
-   public static String generate(String urlPath, String mimeType) throws 
MessagingException {
+   public static String generate(String urlPath, String mimeType) {
 
       log.debug("generate newsletter from url:" + urlPath);
 
       String inputString = "";
-      String errorInfo="please check the system live-path variable, ";
-      try {
+      String errorInfo = "please check the system live-path variable, ";
 
+      try {
          log.debug("Try to get content from URL:" + urlPath);
 
          URL url = new URL(urlPath);
          if (urlPath.indexOf("http") < 0) {
-            errorInfo += "the path does not start with http:// ";
+            errorInfo += "the path does not start with http: ";
          }
          HttpURLConnection connection = (HttpURLConnection) 
url.openConnection();
 
@@ -39,28 +40,31 @@
          connection.setDoInput(true);
          connection.setRequestProperty("Content-Type", mimeType);
 
-         Reader reader = new InputStreamReader(connection.getInputStream());
+         BufferedReader in = new BufferedReader(new 
InputStreamReader(connection.getInputStream()));
 
-         StringBuffer buffer = new StringBuffer();
+         StringBuilder buffer = new StringBuilder();
 
-         int c;
-         while ((c = reader.read()) != -1) {
-            char character = (char) c;
-            buffer.append("").append(character);
+         String line;
+         while ((line = in.readLine()) != null) {
+            buffer.append(line);
          }
-
-         reader.close();
+         in.close();
 
          inputString = buffer.toString().trim();
 
-         if ("text/plain".equals(mimeType)) {
+         if ("text/plain".equalsIgnoreCase(mimeType)) {
             inputString = getContentFromPage(inputString);
          }
          inputString = NewsletterUtil.calibrateRelativeURL(inputString);
 
          return (inputString);
-      } catch (Exception e) {
-         log.error("Error when try to get content from" + urlPath+errorInfo, 
e);
+         
+      } catch (FileNotFoundException e) {
+         log.error("Error when try to get content from:" + urlPath+errorInfo, 
e);
+         
+      } catch (IOException e) {
+         e.printStackTrace();
+         log.error("Error when reading input data from URL:" + 
urlPath+errorInfo, e);
       }
 
       return inputString;
@@ -83,7 +87,6 @@
       inputString = inputString.replaceAll("(?m)\r\n+", 
"").replaceAll("(?m)\r+", "").replaceAll("(?m)\n+", "");
       inputString = inputString.replaceAll("<br/>", "\r\n");
 
-
       return inputString;
    }
 
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to