1   /**
2    * Copyright (c) 2000-2008 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.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import javax.mail.Address;
33  import javax.mail.internet.InternetAddress;
34  
35  import org.apache.commons.validator.EmailValidator;
36  
37  /**
38   * <a href="InternetAddressUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Alexander Chow
41   *
42   */
43  public class InternetAddressUtil {
44  
45      public static boolean contains(
46          InternetAddress[] internetAddresses, String emailAddress) {
47  
48          if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
49              for (int i = 0; i < internetAddresses.length; i++) {
50                  if (emailAddress.equals(internetAddresses[i].getAddress())) {
51                      return true;
52                  }
53              }
54          }
55  
56          return false;
57      }
58  
59      public static boolean isValid(String emailAddress) {
60          return EmailValidator.getInstance().isValid(emailAddress);
61      }
62  
63      public static InternetAddress[] removeEntry(
64          Address[] addresses, String emailAddress) {
65  
66          InternetAddress[] internetAddresses = (InternetAddress[])addresses;
67  
68          List<InternetAddress> list = new ArrayList<InternetAddress>();
69  
70          if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
71              return internetAddresses;
72          }
73  
74          for (int i = 0; i < internetAddresses.length; i++) {
75              if (!emailAddress.equals(internetAddresses[i].getAddress())) {
76                  list.add(internetAddresses[i]);
77              }
78          }
79  
80          return list.toArray(new InternetAddress[list.size()]);
81      }
82  
83      public static String toString(Address address) {
84          InternetAddress internetAddress = (InternetAddress)address;
85  
86          if (internetAddress != null) {
87              StringMaker sm = new StringMaker();
88  
89              String personal = internetAddress.getPersonal();
90              String emailAddress = internetAddress.getAddress();
91  
92              if (Validator.isNotNull(personal)) {
93                  sm.append(personal + StringPool.SPACE);
94                  sm.append(StringPool.LESS_THAN);
95                  sm.append(emailAddress);
96                  sm.append(StringPool.GREATER_THAN);
97              }
98              else {
99                  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 }