1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.mail;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.mail.Address;
29  import javax.mail.internet.InternetAddress;
30  
31  import org.apache.commons.validator.EmailValidator;
32  
33  /**
34   * <a href="InternetAddressUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   *
38   */
39  public class InternetAddressUtil {
40  
41      public static boolean contains(
42          InternetAddress[] internetAddresses, String emailAddress) {
43  
44          if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
45              for (int i = 0; i < internetAddresses.length; i++) {
46                  if (emailAddress.equals(internetAddresses[i].getAddress())) {
47                      return true;
48                  }
49              }
50          }
51  
52          return false;
53      }
54  
55      public static boolean isValid(String emailAddress) {
56          return EmailValidator.getInstance().isValid(emailAddress);
57      }
58  
59      public static InternetAddress[] removeEntry(
60          Address[] addresses, String emailAddress) {
61  
62          InternetAddress[] internetAddresses = (InternetAddress[])addresses;
63  
64          List<InternetAddress> list = new ArrayList<InternetAddress>();
65  
66          if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
67              return internetAddresses;
68          }
69  
70          for (int i = 0; i < internetAddresses.length; i++) {
71              if (!emailAddress.equals(internetAddresses[i].getAddress())) {
72                  list.add(internetAddresses[i]);
73              }
74          }
75  
76          return list.toArray(new InternetAddress[list.size()]);
77      }
78  
79      public static String toString(Address address) {
80          InternetAddress internetAddress = (InternetAddress)address;
81  
82          if (internetAddress != null) {
83              StringBuilder sb = new StringBuilder();
84  
85              String personal = internetAddress.getPersonal();
86              String emailAddress = internetAddress.getAddress();
87  
88              if (Validator.isNotNull(personal)) {
89                  sb.append(personal + StringPool.SPACE);
90                  sb.append(StringPool.LESS_THAN);
91                  sb.append(emailAddress);
92                  sb.append(StringPool.GREATER_THAN);
93              }
94              else {
95                  sb.append(emailAddress);
96              }
97  
98              return sb.toString();
99          }
100 
101         return StringPool.BLANK;
102     }
103 
104     public static String toString(Address[] addresses) {
105         StringBuilder sb = new StringBuilder();
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 }