001    /**
002     * Copyright (c) 2000-present 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.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import javax.mail.Address;
027    import javax.mail.internet.AddressException;
028    import javax.mail.internet.InternetAddress;
029    
030    import org.apache.commons.validator.routines.EmailValidator;
031    
032    /**
033     * @author Alexander Chow
034     * @see com.liferay.petra.mail.InternetAddressUtil
035     */
036    public class InternetAddressUtil {
037    
038            public static boolean contains(
039                    InternetAddress[] internetAddresses, String emailAddress) {
040    
041                    if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
042                            for (int i = 0; i < internetAddresses.length; i++) {
043                                    if (emailAddress.equals(internetAddresses[i].getAddress())) {
044                                            return true;
045                                    }
046                            }
047                    }
048    
049                    return false;
050            }
051    
052            public static boolean isValid(String emailAddress) {
053                    return EmailValidator.getInstance().isValid(emailAddress);
054            }
055    
056            public static InternetAddress[] removeEntry(
057                    Address[] addresses, String emailAddress) {
058    
059                    InternetAddress[] internetAddresses = (InternetAddress[])addresses;
060    
061                    List<InternetAddress> list = new ArrayList<>();
062    
063                    if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
064                            return internetAddresses;
065                    }
066    
067                    for (int i = 0; i < internetAddresses.length; i++) {
068                            if (!emailAddress.equals(internetAddresses[i].getAddress())) {
069                                    list.add(internetAddresses[i]);
070                            }
071                    }
072    
073                    return list.toArray(new InternetAddress[list.size()]);
074            }
075    
076            public static String toString(Address address) {
077                    InternetAddress internetAddress = (InternetAddress)address;
078    
079                    if (internetAddress != null) {
080                            StringBundler sb = new StringBundler(5);
081    
082                            String personal = internetAddress.getPersonal();
083                            String emailAddress = internetAddress.getAddress();
084    
085                            if (Validator.isNotNull(personal)) {
086                                    sb.append(personal);
087                                    sb.append(StringPool.SPACE);
088                                    sb.append(StringPool.LESS_THAN);
089                                    sb.append(emailAddress);
090                                    sb.append(StringPool.GREATER_THAN);
091                            }
092                            else {
093                                    sb.append(emailAddress);
094                            }
095    
096                            return sb.toString();
097                    }
098    
099                    return StringPool.BLANK;
100            }
101    
102            public static String toString(Address[] addresses) {
103                    if (ArrayUtil.isEmpty(addresses)) {
104                            return StringPool.BLANK;
105                    }
106    
107                    StringBundler sb = new StringBundler(addresses.length * 2 - 1);
108    
109                    for (int i = 0; i < (addresses.length - 1); i++) {
110                            sb.append(toString(addresses[i]));
111                            sb.append(StringPool.COMMA);
112                    }
113    
114                    sb.append(toString(addresses[addresses.length - 1]));
115    
116                    return sb.toString();
117            }
118    
119            public static void validateAddress(Address address)
120                    throws AddressException {
121    
122                    if (address == null) {
123                            throw new AddressException("Email address is null");
124                    }
125    
126                    String addressString = address.toString();
127    
128                    for (char c : addressString.toCharArray()) {
129                            if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
130                                    StringBundler sb = new StringBundler(3);
131    
132                                    sb.append("Email address ");
133                                    sb.append(addressString);
134                                    sb.append(" is invalid because it contains line breaks");
135    
136                                    throw new AddressException(sb.toString());
137                            }
138                    }
139            }
140    
141            public static void validateAddresses(Address[] addresses)
142                    throws AddressException {
143    
144                    if (addresses == null) {
145                            throw new AddressException();
146                    }
147    
148                    for (Address internetAddress : addresses) {
149                            validateAddress(internetAddress);
150                    }
151            }
152    
153    }