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