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
018package org.apache.commons.net.smtp;
019
020import java.util.Enumeration;
021import 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 *
029 * @see SMTPClient
030 ***/
031
032public final class RelayPath
033{
034    Vector<String> _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<String>();
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    @Override
072    public String toString()
073    {
074        StringBuilder buffer = new StringBuilder();
075        Enumeration<String> hosts;
076
077        buffer.append('<');
078
079        hosts = _path.elements();
080
081        if (hosts.hasMoreElements())
082        {
083            buffer.append('@');
084            buffer.append(hosts.nextElement());
085
086            while (hosts.hasMoreElements())
087            {
088                buffer.append(",@");
089                buffer.append(hosts.nextElement());
090            }
091            buffer.append(':');
092        }
093
094        buffer.append(_emailAddress);
095        buffer.append('>');
096
097        return buffer.toString();
098    }
099
100}