001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.mail;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Validator;
026:
027: import java.util.ArrayList;
028: import java.util.List;
029:
030: import javax.mail.Address;
031: import javax.mail.internet.InternetAddress;
032:
033: import org.apache.commons.validator.EmailValidator;
034:
035: /**
036: * <a href="InternetAddressUtil.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Alexander Chow
039: *
040: */
041: public class InternetAddressUtil {
042:
043: public static boolean contains(InternetAddress[] internetAddresses,
044: String emailAddress) {
045:
046: if ((internetAddresses != null)
047: && Validator.isNotNull(emailAddress)) {
048: for (int i = 0; i < internetAddresses.length; i++) {
049: if (emailAddress.equals(internetAddresses[i]
050: .getAddress())) {
051: return true;
052: }
053: }
054: }
055:
056: return false;
057: }
058:
059: public static boolean isValid(String emailAddress) {
060: return EmailValidator.getInstance().isValid(emailAddress);
061: }
062:
063: public static InternetAddress[] removeEntry(Address[] addresses,
064: String emailAddress) {
065:
066: InternetAddress[] internetAddresses = (InternetAddress[]) addresses;
067:
068: List list = new ArrayList();
069:
070: if ((internetAddresses == null)
071: || Validator.isNull(emailAddress)) {
072: return internetAddresses;
073: }
074:
075: for (int i = 0; i < internetAddresses.length; i++) {
076: if (!emailAddress.equals(internetAddresses[i].getAddress())) {
077: list.add(internetAddresses[i]);
078: }
079: }
080:
081: return (InternetAddress[]) list.toArray(new InternetAddress[0]);
082: }
083:
084: public static String toString(Address address) {
085: InternetAddress internetAddress = (InternetAddress) address;
086:
087: if (internetAddress != null) {
088: StringMaker sm = new StringMaker();
089:
090: String personal = internetAddress.getPersonal();
091: String emailAddress = internetAddress.getAddress();
092:
093: if (Validator.isNotNull(personal)) {
094: sm.append(personal + StringPool.SPACE);
095: sm.append(StringPool.LESS_THAN);
096: sm.append(emailAddress);
097: sm.append(StringPool.GREATER_THAN);
098: } else {
099: sm.append(emailAddress);
100: }
101:
102: return sm.toString();
103: }
104:
105: return StringPool.BLANK;
106: }
107:
108: public static String toString(Address[] addresses) {
109: StringMaker sm = new StringMaker();
110:
111: if (addresses != null) {
112: for (int i = 0; i < addresses.length; i++) {
113: sm.append(toString(addresses[i]));
114:
115: if (i < addresses.length - 1) {
116: sm.append(StringPool.COMMA);
117: sm.append(StringPool.NBSP);
118: }
119: }
120: }
121:
122: return sm.toString();
123: }
124:
125: }
|