[Schmitzm-commits] r2211 - in trunk/schmitzm-core/src: main/java/de/schmitzm/io test/java/de/schmitzm/net/mail

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Jan 31 15:21:30 CET 2013


Author: mojays
Date: 2013-01-31 15:21:30 +0100 (Thu, 31 Jan 2013)
New Revision: 2211

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java
Log:
IOUtil: global (application wide) settings for Proxy use



Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2013-01-31 13:29:55 UTC (rev 2210)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2013-01-31 14:21:30 UTC (rev 2211)
@@ -100,7 +100,17 @@
 	static {
 	  LangUtil.initializeDefaultLogger(Level.DEBUG, null);
 	}
+	
 	/**
+	 * Global {@link ConnectionSettings} which are used, when no explicit
+	 * {@link Proxy} is given for several methods. Can also be used for 
+	 * application wide settings.<br>
+	 * The default is a direct internet connection (without {@link Proxy}).
+	 * @see #downloadUrlToFile(URL, Proxy, File, int, int, int) 
+	 */
+	private static ConnectionSettings CONNECTION_SETTINGS = new ConnectionSettings();
+
+    /**
 	 * Liefert den Index des Dateinames, an der die Dateinamen-Erweiterung
 	 * (inkl. Punkt) beginnt.
 	 * 
@@ -588,19 +598,23 @@
     /**
      * Downloads an {@link URL} to local file.
      * @param url   remote URL
-     * @param proxy proxy server used for url connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      * @param file  local file (if {@code null} a temporary file is created, which will be
      *              deleted after JVM exit!)
      * @param timeout timeout in ms used for connection timeout and read timeout
      */
     public static File downloadUrlToFile(URL url, Proxy proxy, File file, int timeout) throws IOException {
-     return downloadUrlToFile(url, proxy, file, timeout, timeout, 0);
+      return downloadUrlToFile(url, proxy, file, timeout, timeout, 0);
     }
     
     /**
      * Downloads an {@link URL} to local file.
      * @param url   remote URL
-     * @param proxy proxy server used for url connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      * @param file  local file (if {@code null} a temporary file is created, which will be
      *              deleted after JVM exit!)
      * @param timeout timeout in ms used for connection timeout and read timeout
@@ -630,7 +644,9 @@
     /**
      * Downloads an {@link URL} to local file.
      * @param url   remote URL
-     * @param proxy proxy server used for url connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      * @param file  local file (if {@code null} a temporary file is created, which will be
      *              deleted after JVM exit!)
      * @param connTimeout connection timeout in ms
@@ -641,7 +657,7 @@
       if ( file == null )
         file = createTemporaryFile("java-schmitzm-",null,true);
       if ( proxy == null )
-        proxy = Proxy.NO_PROXY;
+        proxy = getGlobalConnectionSettings().getProxyForConnection();
 
       LOGGER.debug("Download: "+url+" to "+file);
       
@@ -1545,14 +1561,16 @@
 	/**
 	 * Extracts all links (href tags) from website document.
 	 * @param url URL of a website
-	 * @param proxy proxy to use for connection
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
 	 * @return an empty list, if {@code null} URL is given
 	 */
 	public static List<String> extractLinksFromURL(URL url, Proxy proxy) throws IOException {
 	  if ( url == null )
 	    return new ArrayList<String>();
 	  if ( proxy == null )
-	    proxy = Proxy.NO_PROXY;
+        proxy = getGlobalConnectionSettings().getProxyForConnection();
 	  // load document from URL
 	  URLConnection conn = url.openConnection(proxy);
 	  conn.setConnectTimeout(1000);
@@ -1585,7 +1603,9 @@
      * web server without any index.html (or something like that).
      * Otherwise (e.g. if URL specifies a website) this method might have an unexpected result!
      * @param url URL of a webserver directory
-     * @param proxy proxy server to use for connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      */
     public static List<URL> listFilesFromURL(URL url, Proxy proxy) throws IOException {
       // #### TODO: improve this workaround method! ####
@@ -1602,7 +1622,9 @@
      * web server without any index.html (or something like that).
      * Otherwise (e.g. if URL specifies a website) this method might have an unexpected result!
      * @param url URL of a webserver directory
-     * @param proxy proxy server to use for connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      */
     public static List<URL> listDirectoriesFromURL(URL url, Proxy proxy) throws IOException {
       // #### TODO: improve this workaround method! ####
@@ -1619,7 +1641,9 @@
      * web server without any index.html (or something like that).
      * Otherwise (e.g. if URL specifies a website) this method might have an unexpected result!
      * @param url URL of a webserver directory
-     * @param proxy proxy server to use for connection (can be {@code null})
+     * @param proxy proxy server used for url connection (if {@code null} the
+     *              {@linkplain #getGlobalConnectionSettings() global connection settings}
+     *              are used)
      * @param files destination list to store the file urls in
      * @param dirs  destination list to store the directory urls in
      */
@@ -2150,11 +2174,13 @@
     }
     
     /**
-     * Tests whether an internet connection is available.
+     * Tests whether an internet connection is available. 
+     * If no proxy is given, the global connection settings are used.
+     * @see #getGlobalConnectionSettings()
      */
 	public static boolean isInetConnectionAvailable(Proxy proxy) {
 	  if ( proxy == null )
-	    proxy = Proxy.NO_PROXY;
+	    proxy = getGlobalConnectionSettings().getProxyForConnection();
 	  final int      timeoutMillis  = 300;
 	  final String[] addressesToTry = new String[] {"google.com",
 	                                                "heise.de"
@@ -2186,8 +2212,32 @@
         }
 	  return false;
 	}
-	
+
     /**
+     * Returns the global {@link ConnectionSettings} which are used, when
+     * no explicit {@link Proxy} is given for several methods. Can also be used for 
+     * application wide settings.<br>
+     * The default is a direct internet connection (without {@link Proxy}).
+     * @see #downloadUrlToFile(URL, Proxy, File, int, int, int) 
+     */
+    public static ConnectionSettings getGlobalConnectionSettings() {
+      return CONNECTION_SETTINGS;
+    }
+    
+    /**
+     * Sets the global {@link ConnectionSettings} which are used, when
+     * no explicit {@link Proxy} is given for several methods. Can also be used for 
+     * application wide settings.<br>
+     * The default is a direct internet connection (without {@link Proxy}).
+     * @see #downloadUrlToFile(URL, Proxy, File, int, int, int) 
+     */
+    public static void setGlobalConnectionSettings(ConnectionSettings settings) {
+      if ( settings == null )
+        settings = new ConnectionSettings();
+      CONNECTION_SETTINGS = settings;
+    }
+
+    /**
      * Calls {@link Desktop#open(File)} if {@link Desktop} is available. Exceptions are ignored.
      * @param file file or folder to open
      */

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java	2013-01-31 13:29:55 UTC (rev 2210)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/net/mail/MailUtilTest.java	2013-01-31 14:21:30 UTC (rev 2211)
@@ -6,8 +6,14 @@
 
 import java.io.File;
 import java.net.URL;
+import java.util.Date;
 import java.util.regex.Pattern;
 
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.lang.StringUtils;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -16,7 +22,7 @@
 import de.schmitzm.testing.TestingClass;
 
 public class MailUtilTest extends TestingClass {
-
+//    @Ignore
 	@Test
 	public void testEmailRegEx() {
 		Pattern p = MailUtil.EMAIL_ADDRESS_REGEX;
@@ -27,6 +33,7 @@
 
 	}
 
+//    @Ignore
 	@Test
 	public void testSendSmtpMail_BugReport() {
 		String recipientsAddress = "bugreport at wikisquare.de";
@@ -98,4 +105,62 @@
                                   attachment1,attachment2);
 
     }
+
+    @Ignore
+    @Test
+    public void testPop3() throws Throwable {
+      String server = "pop.1und1.de";
+      int    port = 110;
+      String user = "";
+      String pw = "";
+      
+      Pop3Client pop3 = new Pop3Client(server,port);
+      pop3.connect(user, pw, Folder.READ_WRITE);
+      
+//      for (;pop3.getInboxFolder().getNewMessageCount() == 0;) {
+      for(;pop3!=null;) {
+        do {
+          Thread.sleep(3000);
+          pop3.updateInboxFolder();
+          System.out.println( "Message Count: "+pop3.getMessageCount() );
+          System.out.println( "New Messages : "+pop3.getInboxFolder().getNewMessageCount() );
+          System.out.println( "Unread Mess. : "+pop3.getInboxFolder().getUnreadMessageCount() );
+          System.out.println();
+        } while ( pop3.getInboxFolder().getUnreadMessageCount() == 0 ); 
+        Message[] messages = null;
+        messages = pop3.fetchMessages(10);
+
+        // Display message.
+        for (int i = 0; messages != null && i < messages.length; i++) {
+  //        String subj = messages[i].getSubject();
+  //        if ( subj == null || !subj.contains("Unterstützung") )
+  //          continue;
+          System.out.println("------------ Message " + (i + 1) + " ------------");
+          // Here's the big change...
+          int messNo = messages[i].getMessageNumber();
+          System.out.println("ID: " + messNo);
+          String from = InternetAddress.toString(messages[i].getFrom());
+          System.out.println("From: " + StringUtils.trimToEmpty(from));
+          String replyTo = InternetAddress.toString(messages[i].getReplyTo());
+          System.out.println("Reply-to: " + StringUtils.trimToEmpty(replyTo));
+          String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
+          System.out.println("To: " + StringUtils.trimToEmpty(to));
+          String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
+          System.out.println("Cc: " + StringUtils.trimToEmpty(cc));
+          String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
+          System.out.println("Bcc: " + StringUtils.trimToEmpty(to));
+          String subject = messages[i].getSubject();
+          System.out.println("Subject: " + StringUtils.trimToEmpty(subject));
+          Date sent = messages[i].getSentDate();
+          System.out.println("Sent: " + sent);
+          Date received = messages[i].getReceivedDate();
+          System.out.println("Received: " + received);
+          Object content = messages[i].getContent();
+          System.out.println("Content:\n" + content);
+          System.out.println();
+//          messages[i].setFlag(Flag.DELETED, true);
+        }
+      }
+      pop3.disconnect();
+    }
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list