1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.mail;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.mail.Address;
32  import javax.mail.internet.InternetAddress;
33  
34  import org.apache.commons.validator.EmailValidator;
35  
36  /**
37   * <a href="InternetAddressUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Alexander Chow
40   *
41   */
42  public class InternetAddressUtil {
43  
44      public static boolean contains(
45          InternetAddress[] internetAddresses, String emailAddress) {
46  
47          if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
48              for (int i = 0; i < internetAddresses.length; i++) {
49                  if (emailAddress.equals(internetAddresses[i].getAddress())) {
50                      return true;
51                  }
52              }
53          }
54  
55          return false;
56      }
57  
58      public static boolean isValid(String emailAddress) {
59          return EmailValidator.getInstance().isValid(emailAddress);
60      }
61  
62      public static InternetAddress[] removeEntry(
63          Address[] addresses, String emailAddress) {
64  
65          InternetAddress[] internetAddresses = (InternetAddress[])addresses;
66  
67          List<InternetAddress> list = new ArrayList<InternetAddress>();
68  
69          if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
70              return internetAddresses;
71          }
72  
73          for (int i = 0; i < internetAddresses.length; i++) {
74              if (!emailAddress.equals(internetAddresses[i].getAddress())) {
75                  list.add(internetAddresses[i]);
76              }
77          }
78  
79          return list.toArray(new InternetAddress[list.size()]);
80      }
81  
82      public static String toString(Address address) {
83          InternetAddress internetAddress = (InternetAddress)address;
84  
85          if (internetAddress != null) {
86              StringBuilder sb = new StringBuilder();
87  
88              String personal = internetAddress.getPersonal();
89              String emailAddress = internetAddress.getAddress();
90  
91              if (Validator.isNotNull(personal)) {
92                  sb.append(personal + StringPool.SPACE);
93                  sb.append(StringPool.LESS_THAN);
94                  sb.append(emailAddress);
95                  sb.append(StringPool.GREATER_THAN);
96              }
97              else {
98                  sb.append(emailAddress);
99              }
100 
101             return sb.toString();
102         }
103 
104         return StringPool.BLANK;
105     }
106 
107     public static String toString(Address[] addresses) {
108         StringBuilder sb = new StringBuilder();
109 
110         if (addresses != null) {
111             for (int i = 0; i < addresses.length; i++) {
112                 sb.append(toString(addresses[i]));
113 
114                 if (i < addresses.length - 1) {
115                     sb.append(StringPool.COMMA);
116                     sb.append(StringPool.NBSP);
117                 }
118             }
119         }
120 
121         return sb.toString();
122     }
123 
124 }