KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > mail > EmailAddress


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.core.mail;
22
23 import java.io.Serializable JavaDoc;
24
25 /**
26  * Helper class to store an email address
27  * @author Jean-Francois POUX
28  *
29  */

30 public class EmailAddress implements Serializable JavaDoc {
31
32     private String JavaDoc user = "";
33     private String JavaDoc host = "";
34     private String JavaDoc authContext = "";
35
36     /**
37      * Creates an instance by parsing a string user@domain, or <>
38      * @param input the string
39      * @return instance
40      * @throws InvalidAddress when parsing error
41      */

42     public static EmailAddress parseAddress(String JavaDoc input) throws InvalidAddress {
43         if ((input==null) || input.equals(""))
44             throw new InvalidAddress();
45          
46         if (input.equals("<>")) {
47             EmailAddress e = new EmailAddress();
48             e.setUser("<>");
49             return e;
50         }
51         EmailAddress e = new EmailAddress();
52         String JavaDoc mailPart;
53         String JavaDoc tmp = input.toLowerCase().trim();
54         if (tmp.indexOf(" auth=")!=-1) {
55             String JavaDoc[] res = input.trim().split(" ");
56             e.setAuthContext(res[1].trim());
57             mailPart=res[0].trim();
58         } else
59             mailPart=input.trim();
60         
61         if (mailPart.indexOf('@') == -1)
62             throw new InvalidAddress();
63         String JavaDoc[] res = mailPart.split("@");
64
65         if ((res[0].length() > 512) || (res[1].length() > 512))
66             throw new InvalidAddress();
67
68         
69         e.setUser(res[0]);
70         e.setHost(res[1].toLowerCase());
71         return e;
72     }
73
74     /**
75      * builds a user@domain string
76      */

77     public String JavaDoc toString() {
78         if (user.equals("<>"))
79             return user;
80
81         return (user + "@" + host);
82     }
83
84     public String JavaDoc getHost() {
85         return host;
86     }
87
88     public void setHost(String JavaDoc host) {
89         this.host = host;
90     }
91
92     public String JavaDoc getUser() {
93         return user;
94     }
95
96     public void setUser(String JavaDoc user) {
97         this.user = user;
98     }
99
100     /**
101      * test if to instances are equals. matches user and domain, or * as user @ domain
102      * @param in
103      * @return true if equals
104      */

105     public boolean isEqual(EmailAddress in) {
106         if (in == this)
107             return true;
108
109         if (in.getHost().equals(this.host) && in.getUser().equals(this.user))
110             return true;
111
112         if (in.getHost().equals(this.host) && in.getUser().equals("*"))
113             return true;
114
115         return false;
116
117     }
118     
119     
120     
121     public int hashCode() {
122         String JavaDoc tmp=host;
123         return tmp.hashCode();
124     }
125     public boolean equals(Object JavaDoc obj) {
126         if (! (obj instanceof EmailAddress))
127             return false;
128         return isEqual((EmailAddress)obj);
129     }
130     
131     public EmailAddress () {
132         
133     }
134     
135     public EmailAddress (EmailAddress original) {
136         setHost(new String JavaDoc (original.getHost()));
137         setUser(new String JavaDoc (original.getUser()));
138     }
139
140     
141     public String JavaDoc getAuthContext() {
142         return authContext;
143     }
144
145     
146     public void setAuthContext(String JavaDoc authContext) {
147         this.authContext = authContext;
148     }
149 }
Popular Tags