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