Project: javamail
Repository: mercurial
Revision: 543
Author: shannon
Date: 2013-03-20 22:50:21 UTC
Link:
Log Message:
------------
MailHandler added option 'resolve' to the verify enum.
MailHandler fixed fallback code in setDefaultRecipient.
MailHandlerTest include 'resolve' option in existing tests.
MailHandlerTest added testDefaultUrlName.
(From Jason)
Revisions:
----------
543
Modified Paths:
---------------
mail/src/main/java/com/sun/mail/util/logging/MailHandler.java
mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java
Diffs:
------
diff -r 54627929ab4b -r a6f66f6e9131 mail/src/main/java/com/sun/mail/util/logging/MailHandler.java
--- a/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java Mon Mar 18 20:17:54 2013 -0700
+++ b/mail/src/main/java/com/sun/mail/util/logging/MailHandler.java Wed Mar 20 15:50:21 2013 -0700
@@ -117,7 +117,7 @@
* <li><handler-name>.attachment.filters a comma
* separated list of <tt>Filter</tt> class names used to create each attachment.
* The literal <tt>null</tt> is reserved for attachments that do not require
- * filtering. (defaults to
+ * filtering. (defaults to the
* {_at_linkplain java.util.logging.Handler#getFilter() body} filter)
*
* <li><handler-name>.attachment.formatters a comma
@@ -227,16 +227,24 @@
* trigger an early push. (defaults to <tt>Level.OFF</tt>, only push when full)
*
* <li><handler-name>.verify <a name="verify">used</a> to
- * verify all of the <tt>Handler</tt> properties prior to a push. If set to a
- * value of <tt>limited</tt> then the <tt>Handler</tt> will verify minimal
- * local machine settings. If set to a value of <tt>local</tt> the
- * <tt>Handler</tt> will verify all of settings of the local machine. If set to
- * a value of <tt>remote</tt>, the <tt>Handler</tt> will verify all local
- * settings and try to establish a connection with the email server. If the
- * value is not set, equal to an empty string, or equal to the literal
- * <tt>null</tt> then no settings are verified prior to a push. If this
- * <tt>Handler</tt> is only implicitly closed by the <tt>LogManager</tt>, then
- * verification should be turned on. (defaults to <tt>null</tt>, no verify).
+ * verify the <tt>Handler</tt> configuration prior to a push.
+ * <ul>
+ * <li>If the value is not set, equal to an empty string, or equal to the
+ * literal <tt>null</tt> then no settings are verified prior to a push.
+ * <li>If set to a value of <tt>limited</tt> then the <tt>Handler</tt> will
+ * verify minimal local machine settings.
+ * <li>If set to a value of <tt>local</tt> the <tt>Handler</tt> will verify
+ * all of settings of the local machine.
+ * <li>If set to a value of <tt>resolve</tt>, the <tt>Handler</tt> will
+ * verify all local settings and try to resolve the remote host name with
+ * the domain name server.
+ * <li>If set to a value of <tt>remote</tt>, the <tt>Handler</tt> will
+ * verify all local settings and try to establish a connection with the
+ * email server.
+ * </ul>
+ * If this <tt>Handler</tt> is only implicitly closed by the
+ * <tt>LogManager</tt>, then verification should be turned on.
+ * (defaults to <tt>null</tt>, no verify).
*
* <p>
* <b>Normalization:</b>
@@ -2360,8 +2368,9 @@
private void verifySettings0(Session session, String verify) {
assert verify != null : (String) null;
if (!"local".equals(verify) && !"remote".equals(verify)
- && !"limited".equals(verify)) {
- reportError("Verify must be 'limited', local', or 'remote'.",
+ && !"limited".equals(verify) && !"resolve".equals(verify)) {
+ reportError("Verify must be 'limited', local', "
+ + "'resolve' or 'remote'.",
new IllegalArgumentException(verify),
ErrorManager.OPEN_FAILURE);
return;
@@ -2425,7 +2434,7 @@
}
}
- String host = null;
+ String local = null;
if ("remote".equals(verify)) {
MessagingException closed = null;
t.connect();
@@ -2433,7 +2442,7 @@
try {
//Capture localhost while connection is open.
if (t instanceof SMTPTransport) {
- host = ((SMTPTransport) t).getLocalHost();
+ local = ((SMTPTransport) t).getLocalHost();
}
//A message without content will fail at message writeTo
//when sendMessage is called. This allows the handler
@@ -2469,38 +2478,44 @@
fixUpContent(abort, verify, closed);
reportError(abort, closed, ErrorManager.CLOSE_FAILURE);
}
- } else { //Force a property copy.
+ } else {
+ //Force a property copy.
final String protocol = t.getURLName().getProtocol();
session.getProperty("mail.host");
session.getProperty("mail.user");
session.getProperty("mail." + protocol + ".host");
session.getProperty("mail." + protocol + ".port");
session.getProperty("mail." + protocol + ".user");
- host = session.getProperty("mail." + protocol + ".localhost");
- if (isEmpty(host)) {
- host = session.getProperty("mail."
+ local = session.getProperty("mail." + protocol + ".localhost");
+ if (isEmpty(local)) {
+ local = session.getProperty("mail."
+ protocol + ".localaddress");
}
+
+ if ("resolve".equals(verify)) {
+ try { //Resolve the remote host name.
+ verifyHost(t.getURLName().getHost());
+ } catch (final IOException IOE) {
+ MessagingException ME =
+ new MessagingException(msg, IOE);
+ fixUpContent(abort, verify, ME);
+ reportError(abort, ME, ErrorManager.OPEN_FAILURE);
+ } catch (final RuntimeException RE) {
+ MessagingException ME =
+ new MessagingException(msg, RE);
+ fixUpContent(abort, verify, RE);
+ reportError(abort, ME, ErrorManager.OPEN_FAILURE);
+ }
+ }
}
if (!"limited".equals(verify)) {
try { //Verify host name and hit the host name cache.
if (!"remote".equals(verify)
&& t instanceof SMTPTransport) {
- host = ((SMTPTransport) t).getLocalHost();
+ local = ((SMTPTransport) t).getLocalHost();
}
-
- if (isEmpty(host)) {
- if (InetAddress.getLocalHost()
- .getCanonicalHostName().length() == 0) {
- throw new UnknownHostException();
- }
- } else {
- if (InetAddress.getByName(host)
- .getCanonicalHostName().length() == 0) {
- throw new UnknownHostException(host);
- }
- }
+ verifyHost(local);
} catch (final IOException IOE) {
MessagingException ME = new MessagingException(msg, IOE);
fixUpContent(abort, verify, ME);
@@ -2575,6 +2590,26 @@
}
/**
+ * Perform a lookup of the host address or FQDN.
+ * @param host the host or null.
+ * @return the address.
+ * @throws IOException if the host name is not valid.
+ * @since JavaMail 1.5.0
+ */
+ private static InetAddress verifyHost(String host) throws IOException {
+ InetAddress a;
+ if (isEmpty(host)) {
+ a = InetAddress.getLocalHost();
+ } else {
+ a = InetAddress.getByName(host);
+ }
+ if (a.getCanonicalHostName().length() == 0) {
+ throw new UnknownHostException();
+ }
+ return a;
+ }
+
+ /**
* Calls validate for every address given.
* If the addresses given are null, empty or not an InternetAddress then
* the check is skipped.
@@ -2683,7 +2718,7 @@
setAcceptLang(msg);
setFrom(msg);
if (!setRecipient(msg, "mail.to", Message.RecipientType.TO)) {
- setDefaultRecipient(msg);
+ setDefaultRecipient(msg, Message.RecipientType.TO);
}
setRecipient(msg, "mail.cc", Message.RecipientType.CC);
setRecipient(msg, "mail.bcc", Message.RecipientType.BCC);
@@ -3122,19 +3157,21 @@
* Computes the default to-address if none was specified. This can
* fail if the local address can't be computed.
* @param msg the message
+ * @param type the recipient type.
* @since JavaMail 1.5.0
*/
- private void setDefaultRecipient(final Message msg) {
+ private void setDefaultRecipient(final Message msg,
+ final Message.RecipientType type) {
try {
Address a = InternetAddress.getLocalAddress(msg.getSession());
if (a != null) {
- msg.setRecipient(Message.RecipientType.TO, a);
+ msg.setRecipient(type, a);
} else {
final MimeMessage m = new MimeMessage(msg.getSession());
m.setFrom(); //Should throw an exception with a cause.
Address[] from = m.getFrom();
if (from.length > 0) {
- msg.addFrom(from);
+ msg.setRecipients(type, from);
} else {
throw new MessagingException("No local address.");
}
@@ -3142,6 +3179,9 @@
} catch (final MessagingException ME) {
reportError("Unable to compute a default recipient.",
ME, ErrorManager.FORMAT_FAILURE);
+ } catch (final RuntimeException RE) {
+ reportError("Unable to compute a default recipient.",
+ RE, ErrorManager.FORMAT_FAILURE);
}
}
diff -r 54627929ab4b -r a6f66f6e9131 mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java
--- a/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java Mon Mar 18 20:17:54 2013 -0700
+++ b/mail/src/test/java/com/sun/mail/util/logging/MailHandlerTest.java Wed Mar 20 15:50:21 2013 -0700
@@ -2823,6 +2823,15 @@
}
@Test
+ public void testDefaultUrlName() throws Exception {
+ Properties props = createInitProperties("");
+ props.put("mail.transport.protocol", "smtp");
+ Session s = Session.getInstance(props);
+ Transport t = s.getTransport();
+ assertEquals(UNKNOWN_HOST, t.getURLName().getHost());
+ }
+
+ @Test
public void testAttachmentFilters() {
MailHandler instance = new MailHandler(createInitProperties(""));
InternalErrorManager em = new InternalErrorManager();
@@ -4487,6 +4496,30 @@
instance.close();
+ props.put(p.concat(".verify"), "resolve");
+
+ read(manager, props);
+
+ instance = new MailHandler();
+ em = internalErrorManagerFrom(instance);
+
+ assertEquals(InternalErrorManager.class, em.getClass());
+
+ for (int i = 0; i < em.exceptions.size(); i++) {
+ final Throwable t = em.exceptions.get(i);
+ if (isConnectOrTimeout(t)) {
+ continue;
+ }
+
+ if (t instanceof AddressException == false) {
+ dump(t);
+ fail(t.toString());
+ }
+ }
+ assertFalse(em.exceptions.isEmpty());
+
+ instance.close();
+
props.put(p.concat(".verify"), "remote");
read(manager, props);
@@ -4553,6 +4586,28 @@
instance.close();
}
+ props.put("verify", "resolve");
+ instance = new MailHandler();
+ try {
+ em = new InternalErrorManager();
+ instance.setErrorManager(em);
+ instance.setMailProperties(props);
+
+ for (int i = 0; i < em.exceptions.size(); i++) {
+ final Throwable t = em.exceptions.get(i);
+ if (isConnectOrTimeout(t)) {
+ continue;
+ }
+
+ if (t instanceof AddressException == false) {
+ dump(t);
+ fail(t.toString());
+ }
+ }
+ } finally {
+ instance.close();
+ }
+
props.put("verify", "remote");
instance = new MailHandler();
try {
@@ -4632,6 +4687,27 @@
instance.close();
}
+ props.put("verify", "resolve");
+
+ instance = new MailHandler(props);
+ try {
+ InternalErrorManager em = internalErrorManagerFrom(instance);
+
+ for (int i = 0; i < em.exceptions.size(); i++) {
+ final Throwable t = em.exceptions.get(i);
+ if (isConnectOrTimeout(t)) {
+ continue;
+ }
+ if (t instanceof AddressException == false) {
+ dump(t);
+ fail(t.toString());
+ }
+ }
+ assertFalse(em.exceptions.isEmpty());
+ } finally {
+ instance.close();
+ }
+
props.put("verify", "remote");
instance = new MailHandler(props);
try {