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.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.mail.Address;
025    import javax.mail.internet.InternetAddress;
026    
027    import org.apache.commons.validator.EmailValidator;
028    
029    /**
030     * @author Alexander Chow
031     */
032    public class InternetAddressUtil {
033    
034            public static boolean contains(
035                    InternetAddress[] internetAddresses, String emailAddress) {
036    
037                    if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
038                            for (int i = 0; i < internetAddresses.length; i++) {
039                                    if (emailAddress.equals(internetAddresses[i].getAddress())) {
040                                            return true;
041                                    }
042                            }
043                    }
044    
045                    return false;
046            }
047    
048            public static boolean isValid(String emailAddress) {
049                    return EmailValidator.getInstance().isValid(emailAddress);
050            }
051    
052            public static InternetAddress[] removeEntry(
053                    Address[] addresses, String emailAddress) {
054    
055                    InternetAddress[] internetAddresses = (InternetAddress[])addresses;
056    
057                    List<InternetAddress> list = new ArrayList<InternetAddress>();
058    
059                    if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
060                            return internetAddresses;
061                    }
062    
063                    for (int i = 0; i < internetAddresses.length; i++) {
064                            if (!emailAddress.equals(internetAddresses[i].getAddress())) {
065                                    list.add(internetAddresses[i]);
066                            }
067                    }
068    
069                    return list.toArray(new InternetAddress[list.size()]);
070            }
071    
072            public static String toString(Address address) {
073                    InternetAddress internetAddress = (InternetAddress)address;
074    
075                    if (internetAddress != null) {
076                            StringBundler sb = new StringBundler(5);
077    
078                            String personal = internetAddress.getPersonal();
079                            String emailAddress = internetAddress.getAddress();
080    
081                            if (Validator.isNotNull(personal)) {
082                                    sb.append(personal);
083                                    sb.append(StringPool.SPACE);
084                                    sb.append(StringPool.LESS_THAN);
085                                    sb.append(emailAddress);
086                                    sb.append(StringPool.GREATER_THAN);
087                            }
088                            else {
089                                    sb.append(emailAddress);
090                            }
091    
092                            return sb.toString();
093                    }
094    
095                    return StringPool.BLANK;
096            }
097    
098            public static String toString(Address[] addresses) {
099                    if ((addresses == null) || (addresses.length == 0)) {
100                            return StringPool.BLANK;
101                    }
102    
103                    StringBundler sb = new StringBundler(addresses.length * 2 - 1);
104    
105                    for (int i = 0; i < (addresses.length - 1); i++) {
106                            sb.append(toString(addresses[i]));
107                            sb.append(StringPool.COMMA);
108                    }
109    
110                    sb.append(toString(addresses[addresses.length - 1]));
111    
112                    return sb.toString();
113            }
114    
115    }