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 SMTP MAIL command requires a reverse relay path while the SMTP RCPT command requires a forward
25 * relay path. See RFC 821 for more details. In general, you will not have to deal with relay paths.
26 *
27 * @see SMTPClient
28 */
29 public final class RelayPath {
30 private final Vector<String> path;
31 private final String emailAddress;
32
33 /**
34 * Create a relay path with the specified email address as the ultimate destination.
35 *
36 * @param emailAddress The destination email address.
37 */
38 public RelayPath(final String emailAddress) {
39 this.path = new Vector<>();
40 this.emailAddress = emailAddress;
41 }
42
43 /**
44 * Add a mail relay host to the relay path. Hosts are added left to right. For example, the following will create the path
45 * <code><b> < @bar.com,@foo.com:foobar@foo.com > </b></code>
46 *
47 * <pre>
48 * path = new RelayPath("foobar@foo.com");
49 * path.addRelay("bar.com");
50 * path.addRelay("foo.com");
51 * </pre>
52 *
53 * @param hostname The host to add to the relay path.
54 */
55 public void addRelay(final String hostname) {
56 path.addElement(hostname);
57 }
58
59 /**
60 * Return the properly formatted string representation of the relay path.
61 *
62 * @return The properly formatted string representation of the relay path.
63 */
64 @Override
65 public String toString() {
66 final StringBuilder buffer = new StringBuilder();
67 final Enumeration<String> hosts;
68
69 buffer.append('<');
70
71 hosts = path.elements();
72
73 if (hosts.hasMoreElements()) {
74 buffer.append('@');
75 buffer.append(hosts.nextElement());
76
77 while (hosts.hasMoreElements()) {
78 buffer.append(",@");
79 buffer.append(hosts.nextElement());
80 }
81 buffer.append(':');
82 }
83
84 buffer.append(emailAddress);
85 buffer.append('>');
86
87 return buffer.toString();
88 }
89
90 }