RelayPath.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.smtp;

  18. import java.util.Enumeration;
  19. import java.util.Vector;

  20. /**
  21.  * A class used to represent forward and reverse relay paths. The SMTP MAIL command requires a reverse relay path while the SMTP RCPT command requires a forward
  22.  * relay path. See RFC 821 for more details. In general, you will not have to deal with relay paths.
  23.  *
  24.  * @see SMTPClient
  25.  */
  26. public final class RelayPath {
  27.     private final Vector<String> path;
  28.     private final String emailAddress;

  29.     /**
  30.      * Create a relay path with the specified email address as the ultimate destination.
  31.      *
  32.      * @param emailAddress The destination email address.
  33.      */
  34.     public RelayPath(final String emailAddress) {
  35.         this.path = new Vector<>();
  36.         this.emailAddress = emailAddress;
  37.     }

  38.     /**
  39.      * Add a mail relay host to the relay path. Hosts are added left to right. For example, the following will create the path
  40.      * <code><b> &lt; @bar.com,@foo.com:foobar@foo.com &gt; </b></code>
  41.      *
  42.      * <pre>
  43.      * path = new RelayPath("foobar@foo.com");
  44.      * path.addRelay("bar.com");
  45.      * path.addRelay("foo.com");
  46.      * </pre>
  47.      *
  48.      * @param hostname The host to add to the relay path.
  49.      */
  50.     public void addRelay(final String hostname) {
  51.         path.addElement(hostname);
  52.     }

  53.     /**
  54.      * Return the properly formatted string representation of the relay path.
  55.      *
  56.      * @return The properly formatted string representation of the relay path.
  57.      */
  58.     @Override
  59.     public String toString() {
  60.         final StringBuilder buffer = new StringBuilder();

  61.         buffer.append('<');

  62.         final Enumeration<String> hosts = path.elements();

  63.         if (hosts.hasMoreElements()) {
  64.             buffer.append('@');
  65.             buffer.append(hosts.nextElement());

  66.             while (hosts.hasMoreElements()) {
  67.                 buffer.append(",@");
  68.                 buffer.append(hosts.nextElement());
  69.             }
  70.             buffer.append(':');
  71.         }

  72.         buffer.append(emailAddress);
  73.         buffer.append('>');

  74.         return buffer.toString();
  75.     }

  76. }