001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.mail;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import javax.mail.Address;
026    import javax.mail.internet.AddressException;
027    import javax.mail.internet.InternetAddress;
028    
029    import org.apache.commons.validator.EmailValidator;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class InternetAddressUtil {
035    
036            public static boolean contains(
037                    InternetAddress[] internetAddresses, String emailAddress) {
038    
039                    if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
040                            for (int i = 0; i < internetAddresses.length; i++) {
041                                    if (emailAddress.equals(internetAddresses[i].getAddress())) {
042                                            return true;
043                                    }
044                            }
045                    }
046    
047                    return false;
048            }
049    
050            public static boolean isValid(String emailAddress) {
051                    return EmailValidator.getInstance().isValid(emailAddress);
052            }
053    
054            public static InternetAddress[] removeEntry(
055                    Address[] addresses, String emailAddress) {
056    
057                    InternetAddress[] internetAddresses = (InternetAddress[])addresses;
058    
059                    List<InternetAddress> list = new ArrayList<InternetAddress>();
060    
061                    if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
062                            return internetAddresses;
063                    }
064    
065                    for (int i = 0; i < internetAddresses.length; i++) {
066                            if (!emailAddress.equals(internetAddresses[i].getAddress())) {
067                                    list.add(internetAddresses[i]);
068                            }
069                    }
070    
071                    return list.toArray(new InternetAddress[list.size()]);
072            }
073    
074            public static String toString(Address address) {
075                    InternetAddress internetAddress = (InternetAddress)address;
076    
077                    if (internetAddress != null) {
078                            StringBundler sb = new StringBundler(5);
079    
080                            String personal = internetAddress.getPersonal();
081                            String emailAddress = internetAddress.getAddress();
082    
083                            if (Validator.isNotNull(personal)) {
084                                    sb.append(personal);
085                                    sb.append(StringPool.SPACE);
086                                    sb.append(StringPool.LESS_THAN);
087                                    sb.append(emailAddress);
088                                    sb.append(StringPool.GREATER_THAN);
089                            }
090                            else {
091                                    sb.append(emailAddress);
092                            }
093    
094                            return sb.toString();
095                    }
096    
097                    return StringPool.BLANK;
098            }
099    
100            public static String toString(Address[] addresses) {
101                    if ((addresses == null) || (addresses.length == 0)) {
102                            return StringPool.BLANK;
103                    }
104    
105                    StringBundler sb = new StringBundler(addresses.length * 2 - 1);
106    
107                    for (int i = 0; i < (addresses.length - 1); i++) {
108                            sb.append(toString(addresses[i]));
109                            sb.append(StringPool.COMMA);
110                    }
111    
112                    sb.append(toString(addresses[addresses.length - 1]));
113    
114                    return sb.toString();
115            }
116    
117            public static void validateAddress(Address address)
118                    throws AddressException {
119    
120                    if (address == null) {
121                            throw new AddressException("Email address is null");
122                    }
123    
124                    String addressString = address.toString();
125    
126                    for (char c : addressString.toCharArray()) {
127                            if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
128                                    StringBundler sb = new StringBundler(3);
129    
130                                    sb.append("Email address ");
131                                    sb.append(addressString);
132                                    sb.append(" is invalid because it contains line breaks");
133    
134                                    throw new AddressException(sb.toString());
135                            }
136                    }
137            }
138    
139            public static void validateAddresses(Address[] addresses)
140                    throws AddressException {
141    
142                    if (addresses == null) {
143                            throw new AddressException();
144                    }
145    
146                    for (Address internetAddress : addresses) {
147                            validateAddress(internetAddress);
148                    }
149            }
150    
151    }