001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net.smtp;
017    
018    import java.util.Enumeration;
019    import java.util.Vector;
020    
021    /***
022     * A class used to represent forward and reverse relay paths.  The
023     * SMTP MAIL command requires a reverse relay path while the SMTP RCPT
024     * command requires a forward relay path.  See RFC 821 for more details.
025     * In general, you will not have to deal with relay paths.
026     * <p>
027     * <p>
028     * @author Daniel F. Savarese
029     * @see SMTPClient
030     ***/
031    
032    public final class RelayPath
033    {
034        Vector _path;
035        String _emailAddress;
036    
037        /***
038         * Create a relay path with the specified email address as the ultimate
039         * destination.
040         * <p>
041         * @param emailAddress The destination email address.
042         ***/
043        public RelayPath(String emailAddress)
044        {
045            _path = new Vector();
046            _emailAddress = emailAddress;
047        }
048    
049        /***
050         * Add a mail relay host to the relay path.  Hosts are added left to
051         * right.  For example, the following will create the path
052         * <code><b> &lt @bar.com,@foo.com:foobar@foo.com &gt </b></code>
053         * <pre>
054         * path = new RelayPath("foobar@foo.com");
055         * path.addRelay("bar.com");
056         * path.addRelay("foo.com");
057         * </pre>
058         * <p>
059         * @param hostname The host to add to the relay path.
060         ***/
061        public void addRelay(String hostname)
062        {
063            _path.addElement(hostname);
064        }
065    
066        /***
067         * Return the properly formatted string representation of the relay path.
068         * <p>
069         * @return The properly formatted string representation of the relay path.
070         ***/
071        public String toString()
072        {
073            StringBuffer buffer = new StringBuffer();
074            Enumeration hosts;
075    
076            buffer.append('<');
077    
078            hosts = _path.elements();
079    
080            if (hosts.hasMoreElements())
081            {
082                buffer.append('@');
083                buffer.append((String)hosts.nextElement());
084    
085                while (hosts.hasMoreElements())
086                {
087                    buffer.append(",@");
088                    buffer.append((String)hosts.nextElement());
089                }
090                buffer.append(':');
091            }
092    
093            buffer.append(_emailAddress);
094            buffer.append('>');
095    
096            return buffer.toString();
097        }
098    
099    }