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