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     * @see SMTPClient
031     ***/
032    
033    public final class RelayPath
034    {
035        Vector<String> _path;
036        String _emailAddress;
037    
038        /***
039         * Create a relay path with the specified email address as the ultimate
040         * destination.
041         * <p>
042         * @param emailAddress The destination email address.
043         ***/
044        public RelayPath(String emailAddress)
045        {
046            _path = new Vector<String>();
047            _emailAddress = emailAddress;
048        }
049    
050        /***
051         * Add a mail relay host to the relay path.  Hosts are added left to
052         * right.  For example, the following will create the path
053         * <code><b> &lt @bar.com,@foo.com:foobar@foo.com &gt </b></code>
054         * <pre>
055         * path = new RelayPath("foobar@foo.com");
056         * path.addRelay("bar.com");
057         * path.addRelay("foo.com");
058         * </pre>
059         * <p>
060         * @param hostname The host to add to the relay path.
061         ***/
062        public void addRelay(String hostname)
063        {
064            _path.addElement(hostname);
065        }
066    
067        /***
068         * Return the properly formatted string representation of the relay path.
069         * <p>
070         * @return The properly formatted string representation of the relay path.
071         ***/
072        @Override
073        public String toString()
074        {
075            StringBuilder buffer = new StringBuilder();
076            Enumeration<String> hosts;
077    
078            buffer.append('<');
079    
080            hosts = _path.elements();
081    
082            if (hosts.hasMoreElements())
083            {
084                buffer.append('@');
085                buffer.append(hosts.nextElement());
086    
087                while (hosts.hasMoreElements())
088                {
089                    buffer.append(",@");
090                    buffer.append(hosts.nextElement());
091                }
092                buffer.append(':');
093            }
094    
095            buffer.append(_emailAddress);
096            buffer.append('>');
097    
098            return buffer.toString();
099        }
100    
101    }