    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    private final Vector<String> path;
    035    private final 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(final String emailAddress)
    044    {
    045        this.path = new Vector<>();
    046        this.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(final 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        final StringBuilder buffer = new StringBuilder();
    075        final 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}