1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.mail;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.Validator;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.mail.Address;
25  import javax.mail.internet.InternetAddress;
26  
27  import org.apache.commons.validator.EmailValidator;
28  
29  /**
30   * <a href="InternetAddressUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Alexander Chow
33   */
34  public class InternetAddressUtil {
35  
36      public static boolean contains(
37          InternetAddress[] internetAddresses, String emailAddress) {
38  
39          if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
40              for (int i = 0; i < internetAddresses.length; i++) {
41                  if (emailAddress.equals(internetAddresses[i].getAddress())) {
42                      return true;
43                  }
44              }
45          }
46  
47          return false;
48      }
49  
50      public static boolean isValid(String emailAddress) {
51          return EmailValidator.getInstance().isValid(emailAddress);
52      }
53  
54      public static InternetAddress[] removeEntry(
55          Address[] addresses, String emailAddress) {
56  
57          InternetAddress[] internetAddresses = (InternetAddress[])addresses;
58  
59          List<InternetAddress> list = new ArrayList<InternetAddress>();
60  
61          if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
62              return internetAddresses;
63          }
64  
65          for (int i = 0; i < internetAddresses.length; i++) {
66              if (!emailAddress.equals(internetAddresses[i].getAddress())) {
67                  list.add(internetAddresses[i]);
68              }
69          }
70  
71          return list.toArray(new InternetAddress[list.size()]);
72      }
73  
74      public static String toString(Address address) {
75          InternetAddress internetAddress = (InternetAddress)address;
76  
77          if (internetAddress != null) {
78              StringBundler sb = new StringBundler(5);
79  
80              String personal = internetAddress.getPersonal();
81              String emailAddress = internetAddress.getAddress();
82  
83              if (Validator.isNotNull(personal)) {
84                  sb.append(personal);
85                  sb.append(StringPool.SPACE);
86                  sb.append(StringPool.LESS_THAN);
87                  sb.append(emailAddress);
88                  sb.append(StringPool.GREATER_THAN);
89              }
90              else {
91                  sb.append(emailAddress);
92              }
93  
94              return sb.toString();
95          }
96  
97          return StringPool.BLANK;
98      }
99  
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 * 3 - 2);
106 
107         if (addresses != null) {
108             for (int i = 0; i < addresses.length; i++) {
109                 sb.append(toString(addresses[i]));
110 
111                 if (i < addresses.length - 1) {
112                     sb.append(StringPool.COMMA);
113                     sb.append(StringPool.NBSP);
114                 }
115             }
116         }
117 
118         return sb.toString();
119     }
120 
121 }