You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2014/04/13 01:24:15 UTC
svn commit: r1586909 - in /commons/proper/net/trunk/src/main/java/examples:
Main.java mail/IMAPImportMbox.java mail/IMAPMail.java mail/IMAPUtils.java
Author: sebb
Date: Sat Apr 12 23:24:14 2014
New Revision: 1586909
URL: http://svn.apache.org/r1586909
Log:
Simplify by standardising on URI for server and login details; use common utility method
Added:
commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java (with props)
Modified:
commons/proper/net/trunk/src/main/java/examples/Main.java
commons/proper/net/trunk/src/main/java/examples/mail/IMAPImportMbox.java
commons/proper/net/trunk/src/main/java/examples/mail/IMAPMail.java
Modified: commons/proper/net/trunk/src/main/java/examples/Main.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/Main.java?rev=1586909&r1=1586908&r2=1586909&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/Main.java (original)
+++ commons/proper/net/trunk/src/main/java/examples/Main.java Sat Apr 12 23:24:14 2014
@@ -41,7 +41,7 @@ public class Main {
*/
public static void main(String[] args) throws Exception {
if (args.length==0) {
- System.out.println("Usage: java -jar examples.jar <exampleClass> <exampleClass parameters>");
+ System.out.println("Usage: java -jar commons-net-examples-m.n.jar <exampleClass> <exampleClass parameters>");
}
CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
Map<String, String> map = new HashMap<String, String>();
@@ -58,9 +58,11 @@ public class Main {
String name = je.getName();
if (!name.endsWith(".class")
|| name.contains("$") // subclasses
+ // TODO use reflection to eliminate non-mail classes?
|| name.equals("examples/nntp/NNTPUtils.class") // no main class
|| name.equals("examples/util/IOUtil.class") // no main class
- || name.equals("examples/Main.class")) {
+ || name.equals("examples/mail/IMAPUtils.class") // no main class
+ || name.equals("examples/Main.class")) { // ourself
continue;
}
name = name.replace(".class", "");
Modified: commons/proper/net/trunk/src/main/java/examples/mail/IMAPImportMbox.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/mail/IMAPImportMbox.java?rev=1586909&r1=1586908&r2=1586909&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/mail/IMAPImportMbox.java (original)
+++ commons/proper/net/trunk/src/main/java/examples/mail/IMAPImportMbox.java Sat Apr 12 23:24:14 2014
@@ -29,7 +29,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.net.imap.IMAPClient;
-import org.apache.commons.net.imap.IMAPSClient;
/**
* This is an example program demonstrating how to use the IMAP[S]Client class.
@@ -69,19 +68,6 @@ public final class IMAPImportMbox
throw new IOException("Cannot read mailbox file: " + mbox);
}
- final String userInfo = uri.getUserInfo();
- if (userInfo == null) {
- throw new IllegalArgumentException("Missing userInfo details");
- }
-
- String []userpass = userInfo.split(":");
- if (userpass.length != 2) {
- throw new IllegalArgumentException("Invalid userInfo details: '" + userpass + "'");
- }
-
- String username = userpass[0];
- String password = userpass[1];
-
String path = uri.getPath();
if (path == null || path.length() < 1) {
throw new IllegalArgumentException("Invalid folderPath: '" + path + "'");
@@ -113,56 +99,12 @@ public final class IMAPImportMbox
// System.out.println(msgNums.toString());
// System.out.println(java.util.Arrays.toString(contains.toArray()));
- final IMAPClient imap;
-
- if ("imaps".equalsIgnoreCase(uri.getScheme())) {
- System.out.println("Using secure protocol");
- imap = new IMAPSClient(true); // implicit
-// } else if ("null".equals(uri.getScheme())) {
-// imap = new IMAPClient(){
-// @Override
-// public void connect(String host){ }
-// @Override
-// public boolean login(String user, String pass) {return true;}
-// @Override
-// public boolean logout() {return true;}
-// @Override
-// public void disconnect() {}
-// @Override
-// public void setSoTimeout(int t) {}
-// @Override
-// public boolean append(String mailboxName, String flags, String datetime, String message) {return true;}
-// };
- } else {
- imap = new IMAPClient();
- }
-
- String server = uri.getHost();
- int port = uri.getPort();
- if (port != -1) {
- imap.setDefaultPort(port);
- }
-
- System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());
-
- // We want to timeout if a response takes longer than 60 seconds
- imap.setDefaultTimeout(60000);
-
- try {
- imap.connect(server);
- } catch (IOException e) {
- throw new RuntimeException("Could not connect to server.", e);
- }
+ // Connect and login
+ final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);
int total = 0;
int loaded = 0;
try {
- if (!imap.login(username, password)) {
- System.err.println("Could not login to server. Check password.");
- imap.disconnect();
- System.exit(3);
- }
-
imap.setSoTimeout(6000);
final BufferedReader br = new BufferedReader(new FileReader(file)); // TODO charset?
Modified: commons/proper/net/trunk/src/main/java/examples/mail/IMAPMail.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/mail/IMAPMail.java?rev=1586909&r1=1586908&r2=1586909&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/mail/IMAPMail.java (original)
+++ commons/proper/net/trunk/src/main/java/examples/mail/IMAPMail.java Sat Apr 12 23:24:14 2014
@@ -18,76 +18,40 @@
package examples.mail;
import java.io.IOException;
+import java.net.URI;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.imap.IMAPClient;
-import org.apache.commons.net.imap.IMAPSClient;
/**
* This is an example program demonstrating how to use the IMAP[S]Client class.
* This program connects to a IMAP[S] server, lists its capabilities and shows
- * the status of the inbox.
+ * the status of the Inbox.
* <p>
- * Usage: IMAPMail <imap[s] server hostname> <username> <password> [secure protocol, e.g. TLS]
+ * Usage: IMAPMail imap[s]://username:password@server/
* <p>
*/
public final class IMAPMail
{
- public static void main(String[] args)
- {
- if (args.length < 3)
+ public static void main(String[] args) throws IOException {
+ if (args.length != 1)
{
System.err.println(
- "Usage: IMAPMail <imap server hostname> <username> <password> [TLS]");
+ "Usage: IMAPMail imap[s]://username:password@server/");
System.err.println("Connects to server; lists capabilities and shows Inbox status");
System.exit(1);
}
- String server = args[0];
- String username = args[1];
- String password = args[2];
-
- String proto = (args.length > 3) ? args[3] : null;
-
- IMAPClient imap;
-
- if (proto != null) {
- System.out.println("Using secure protocol: " + proto);
- imap = new IMAPSClient(proto, true); // implicit
- // enable the next line to only check if the server certificate has expired (does not check chain):
-// ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager());
- // OR enable the next line if the server uses a self-signed certificate (no checks)
-// ((IMAPSClient) imap).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
- } else {
- imap = new IMAPClient();
- }
- System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());
+ URI uri = URI.create(args[0]);
- // We want to timeout if a response takes longer than 60 seconds
- imap.setDefaultTimeout(60000);
+ // Connect and login
+ final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null);
// suppress login details
imap.addProtocolCommandListener(new PrintCommandListener(System.out, true));
- try
- {
- imap.connect(server);
- }
- catch (IOException e)
- {
- throw new RuntimeException("Could not connect to server.", e);
- }
-
- try
- {
- if (!imap.login(username, password))
- {
- System.err.println("Could not login to server. Check password.");
- imap.disconnect();
- System.exit(3);
- }
-
+ try {
imap.setSoTimeout(6000);
imap.capability();
@@ -98,15 +62,14 @@ public final class IMAPMail
imap.status("inbox", new String[]{"MESSAGES"});
- imap.logout();
- imap.disconnect();
- }
- catch (IOException e)
- {
+ } catch (IOException e) {
System.out.println(imap.getReplyString());
e.printStackTrace();
System.exit(10);
return;
+ } finally {
+ imap.logout();
+ imap.disconnect();
}
}
}
Added: commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java?rev=1586909&view=auto
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java (added)
+++ commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java Sat Apr 12 23:24:14 2014
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package examples.mail;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.commons.net.ProtocolCommandListener;
+import org.apache.commons.net.imap.IMAPClient;
+import org.apache.commons.net.imap.IMAPSClient;
+
+/**
+ * Utility class for shared IMAP utilities
+ */
+
+class IMAPUtils {
+
+ /**
+ * Parse the URI and use the details to connect to the IMAP(S) server and login.
+ *
+ * @param uri the URI to use, e.g. imaps://user:pass@imap.mail.yahoo.com/folder
+ * or imaps://user:pass@imap.googlemail.com/folder
+ * @param defaultTimeout initial timeout
+ * @param listener for tracing protocol IO (may be null)
+ * @return the IMAP client - connected and logged in
+ * @throws IOException if any problems occur
+ */
+ static IMAPClient imapLogin(URI uri, int defaultTimeout, ProtocolCommandListener listener) throws IOException {
+ final String userInfo = uri.getUserInfo();
+ if (userInfo == null) {
+ throw new IllegalArgumentException("Missing userInfo details");
+ }
+
+ String []userpass = userInfo.split(":");
+ if (userpass.length != 2) {
+ throw new IllegalArgumentException("Invalid userInfo details: '" + userpass + "'");
+ }
+
+ String username = userpass[0];
+ String password = userpass[1]; // TODO enable reading this secretly
+
+ final IMAPClient imap;
+
+ final String scheme = uri.getScheme();
+ if ("imaps".equalsIgnoreCase(scheme)) {
+ System.out.println("Using secure protocol");
+ imap = new IMAPSClient(true); // implicit
+ } else if ("imap".equalsIgnoreCase(scheme)) {
+ imap = new IMAPClient();
+ } else {
+ throw new IllegalArgumentException("Invalid protocol: " + scheme);
+ }
+ final int port = uri.getPort();
+ if (port != -1) {
+ imap.setDefaultPort(port);
+ }
+
+ imap.setDefaultTimeout(defaultTimeout);
+
+ if (listener != null) {
+ imap.addProtocolCommandListener(listener);
+ }
+
+ final String server = uri.getHost();
+ System.out.println("Connecting to server " + server + " on " + imap.getDefaultPort());
+
+ try {
+ imap.connect(server);
+ System.out.println("Successfully connected");
+ } catch (IOException e) {
+ throw new RuntimeException("Could not connect to server.", e);
+ }
+
+ if (!imap.login(username, password)) {
+ imap.disconnect();
+ throw new RuntimeException("Could not login to server. Check login details.");
+ }
+
+ return imap;
+ }
+}
Propchange: commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/net/trunk/src/main/java/examples/mail/IMAPUtils.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision