1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.smtp;
  17. import java.util.Enumeration;
  18. import java.util.Vector;
  19. /***
  20. * A class used to represent forward and reverse relay paths. The
  21. * SMTP MAIL command requires a reverse relay path while the SMTP RCPT
  22. * command requires a forward relay path. See RFC 821 for more details.
  23. * In general, you will not have to deal with relay paths.
  24. * <p>
  25. * <p>
  26. * @author Daniel F. Savarese
  27. * @see SMTPClient
  28. ***/
  29. public final class RelayPath
  30. {
  31. Vector _path;
  32. String _emailAddress;
  33. /***
  34. * Create a relay path with the specified email address as the ultimate
  35. * destination.
  36. * <p>
  37. * @param emailAddress The destination email address.
  38. ***/
  39. public RelayPath(String emailAddress)
  40. {
  41. _path = new Vector();
  42. _emailAddress = emailAddress;
  43. }
  44. /***
  45. * Add a mail relay host to the relay path. Hosts are added left to
  46. * right. For example, the following will create the path
  47. * <code><b> < @bar.com,@foo.com:foobar@foo.com > </b></code>
  48. * <pre>
  49. * path = new RelayPath("foobar@foo.com");
  50. * path.addRelay("bar.com");
  51. * path.addRelay("foo.com");
  52. * </pre>
  53. * <p>
  54. * @param hostname The host to add to the relay path.
  55. ***/
  56. public void addRelay(String hostname)
  57. {
  58. _path.addElement(hostname);
  59. }
  60. /***
  61. * Return the properly formatted string representation of the relay path.
  62. * <p>
  63. * @return The properly formatted string representation of the relay path.
  64. ***/
  65. public String toString()
  66. {
  67. StringBuffer buffer = new StringBuffer();
  68. Enumeration hosts;
  69. buffer.append('<');
  70. hosts = _path.elements();
  71. if (hosts.hasMoreElements())
  72. {
  73. buffer.append('@');
  74. buffer.append((String)hosts.nextElement());
  75. while (hosts.hasMoreElements())
  76. {
  77. buffer.append(",@");
  78. buffer.append((String)hosts.nextElement());
  79. }
  80. buffer.append(':');
  81. }
  82. buffer.append(_emailAddress);
  83. buffer.append('>');
  84. return buffer.toString();
  85. }
  86. }