1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class InternetAddressUtil {
42  
43      public static boolean contains(
44          InternetAddress[] internetAddresses, String emailAddress) {
45  
46          if ((internetAddresses != null) && Validator.isNotNull(emailAddress)) {
47              for (int i = 0; i < internetAddresses.length; i++) {
48                  if (emailAddress.equals(internetAddresses[i].getAddress())) {
49                      return true;
50                  }
51              }
52          }
53  
54          return false;
55      }
56  
57      public static boolean isValid(String emailAddress) {
58          return EmailValidator.getInstance().isValid(emailAddress);
59      }
60  
61      public static InternetAddress[] removeEntry(
62          Address[] addresses, String emailAddress) {
63  
64          InternetAddress[] internetAddresses = (InternetAddress[])addresses;
65  
66          List<InternetAddress> list = new ArrayList<InternetAddress>();
67  
68          if ((internetAddresses == null) || Validator.isNull(emailAddress)) {
69              return internetAddresses;
70          }
71  
72          for (int i = 0; i < internetAddresses.length; i++) {
73              if (!emailAddress.equals(internetAddresses[i].getAddress())) {
74                  list.add(internetAddresses[i]);
75              }
76          }
77  
78          return list.toArray(new InternetAddress[list.size()]);
79      }
80  
81      public static String toString(Address address) {
82          InternetAddress internetAddress = (InternetAddress)address;
83  
84          if (internetAddress != null) {
85              StringBuilder sb = new StringBuilder();
86  
87              String personal = internetAddress.getPersonal();
88              String emailAddress = internetAddress.getAddress();
89  
90              if (Validator.isNotNull(personal)) {
91                  sb.append(personal + StringPool.SPACE);
92                  sb.append(StringPool.LESS_THAN);
93                  sb.append(emailAddress);
94                  sb.append(StringPool.GREATER_THAN);
95              }
96              else {
97                  sb.append(emailAddress);
98              }
99  
100             return sb.toString();
101         }
102 
103         return StringPool.BLANK;
104     }
105 
106     public static String toString(Address[] addresses) {
107         StringBuilder sb = new StringBuilder();
108 
109         if (addresses != null) {
110             for (int i = 0; i < addresses.length; i++) {
111                 sb.append(toString(addresses[i]));
112 
113                 if (i < addresses.length - 1) {
114                     sb.append(StringPool.COMMA);
115                     sb.append(StringPool.NBSP);
116                 }
117             }
118         }
119 
120         return sb.toString();
121     }
122 
123 }