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   * @see SMTPClient
31   ***/
32  
33  public final class RelayPath
34  {
35      Vector<String> _path;
36      String _emailAddress;
37  
38      /***
39       * Create a relay path with the specified email address as the ultimate
40       * destination.
41       * <p>
42       * @param emailAddress The destination email address.
43       ***/
44      public RelayPath(String emailAddress)
45      {
46          _path = new Vector<String>();
47          _emailAddress = emailAddress;
48      }
49  
50      /***
51       * Add a mail relay host to the relay path.  Hosts are added left to
52       * right.  For example, the following will create the path
53       * <code><b> &lt @bar.com,@foo.com:foobar@foo.com &gt </b></code>
54       * <pre>
55       * path = new RelayPath("foobar@foo.com");
56       * path.addRelay("bar.com");
57       * path.addRelay("foo.com");
58       * </pre>
59       * <p>
60       * @param hostname The host to add to the relay path.
61       ***/
62      public void addRelay(String hostname)
63      {
64          _path.addElement(hostname);
65      }
66  
67      /***
68       * Return the properly formatted string representation of the relay path.
69       * <p>
70       * @return The properly formatted string representation of the relay path.
71       ***/
72      @Override
73      public String toString()
74      {
75          StringBuilder buffer = new StringBuilder();
76          Enumeration<String> hosts;
77  
78          buffer.append('<');
79  
80          hosts = _path.elements();
81  
82          if (hosts.hasMoreElements())
83          {
84              buffer.append('@');
85              buffer.append(hosts.nextElement());
86  
87              while (hosts.hasMoreElements())
88              {
89                  buffer.append(",@");
90                  buffer.append(hosts.nextElement());
91              }
92              buffer.append(':');
93          }
94  
95          buffer.append(_emailAddress);
96          buffer.append('>');
97  
98          return buffer.toString();
99      }
100 
101 }