View Javadoc

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  
18  package org.apache.commons.net.smtp;
19  
20  import java.util.Enumeration;
21  import java.util.Vector;
22  
23  /***
24   * A class used to represent forward and reverse relay paths.  The
25   * SMTP MAIL command requires a reverse relay path while the SMTP RCPT
26   * command requires a forward relay path.  See RFC 821 for more details.
27   * In general, you will not have to deal with relay paths.
28   * <p>
29   * <p>
30   * @author Daniel F. Savarese
31   * @see SMTPClient
32   ***/
33  
34  public final class RelayPath
35  {
36      Vector<String> _path;
37      String _emailAddress;
38  
39      /***
40       * Create a relay path with the specified email address as the ultimate
41       * destination.
42       * <p>
43       * @param emailAddress The destination email address.
44       ***/
45      public RelayPath(String emailAddress)
46      {
47          _path = new Vector<String>();
48          _emailAddress = emailAddress;
49      }
50  
51      /***
52       * Add a mail relay host to the relay path.  Hosts are added left to
53       * right.  For example, the following will create the path
54       * <code><b> &lt @bar.com,@foo.com:foobar@foo.com &gt </b></code>
55       * <pre>
56       * path = new RelayPath("foobar@foo.com");
57       * path.addRelay("bar.com");
58       * path.addRelay("foo.com");
59       * </pre>
60       * <p>
61       * @param hostname The host to add to the relay path.
62       ***/
63      public void addRelay(String hostname)
64      {
65          _path.addElement(hostname);
66      }
67  
68      /***
69       * Return the properly formatted string representation of the relay path.
70       * <p>
71       * @return The properly formatted string representation of the relay path.
72       ***/
73      @Override
74      public String toString()
75      {
76          StringBuffer buffer = new StringBuffer();
77          Enumeration<String> hosts;
78  
79          buffer.append('<');
80  
81          hosts = _path.elements();
82  
83          if (hosts.hasMoreElements())
84          {
85              buffer.append('@');
86              buffer.append(hosts.nextElement());
87  
88              while (hosts.hasMoreElements())
89              {
90                  buffer.append(",@");
91                  buffer.append(hosts.nextElement());
92              }
93              buffer.append(':');
94          }
95  
96          buffer.append(_emailAddress);
97          buffer.append('>');
98  
99          return buffer.toString();
100     }
101 
102 }