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