Subversion Repositories javautils

Rev

Rev 3 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 9
Line 1... Line 1...
1
package de.viathinksoft.utils.mail;
1
package de.viathinksoft.utils.mail;
2
 
2
 
3
import java.net.IDN;
3
import java.net.IDN;
4
 
4
 
5
import de.viathinksoft.utils.mail.syntaxchecker.MailSyntaxChecker;
-
 
6
 
-
 
7
/**
5
/**
8
 *
6
 *
9
 * This class parses an email address (trims whitespaces from it) and stores it
7
 * This class parses an email address (trims whitespaces from it) and stores it
10
 * in its original form as well as store a RFC-compatible punycoded domainpart.
8
 * in its original form as well as store a RFC-compatible punycoded domainpart.
11
 * So, if you enter a Unicode-Mail-Address you can easily access the trimmed and
9
 * So, if you enter a Unicode-Mail-Address you can easily access the trimmed and
Line 120... Line 118...
120
         *
118
         *
121
         * @param eMailAddress
119
         * @param eMailAddress
122
         *            bare computer email address. e.g. roedyg@mindprod.com No
120
         *            bare computer email address. e.g. roedyg@mindprod.com No
123
         *            "Roedy Green" <roedyg@mindprod.com> style addresses. No local
121
         *            "Roedy Green" <roedyg@mindprod.com> style addresses. No local
124
         *            addresses, e.g. roedy.
122
         *            addresses, e.g. roedy.
125
         * @throws InvalidMailAddressException
-
 
126
         */
123
         */
127
        public EMailAddress(String eMailAddress) throws InvalidMailAddressException {
124
        public EMailAddress(String eMailAddress) {
128
                super();
125
                super();
129
 
126
 
130
                if (eMailAddress == null) {
-
 
131
                        throw new InvalidMailAddressException();
-
 
132
                }
-
 
133
 
-
 
134
                // Zuerst trimmen (z.B. für Formulardaten) Wir splitten dann beim
127
                // Zuerst trimmen (z.B. für Formulardaten)
135
                // At-Zeichen (@) und berücksichtigen ein escaped-At
-
 
136
                // (\@)
-
 
137
                String[] res = eMailAddress.trim().split("(?<!\\\\)@");
128
                eMailAddress = eMailAddress.trim();
138
 
129
 
139
                // @-sign was not used once
130
                // Wir splitten dann beim At-Zeichen (@)
-
 
131
                String localPart = "";
-
 
132
                String domainPart = "";
-
 
133
                int atIndex = eMailAddress.lastIndexOf('@');
140
                if (res.length != 2) {
134
                if (atIndex == -1) {
-
 
135
                        localPart = eMailAddress;
-
 
136
                        domainPart = "";
-
 
137
                } else {
141
                        throw new InvalidMailAddressException();
138
                        localPart = eMailAddress.substring(0, atIndex);
-
 
139
                        domainPart = eMailAddress.substring(atIndex + 1);
142
                }
140
                }
143
 
141
 
144
                // Temporary we store the values here.
-
 
145
 
-
 
146
                String localPart = res[0];
-
 
147
                String domainPart = res[1];
-
 
148
 
-
 
149
                // We parse the local part.
142
                // We parse the local part.
150
 
143
 
151
                if (localPart == null)
144
                if (localPart == null)
152
                        localPart = "";
145
                        localPart = "";
153
                this.localPart = localPart;
146
                this.localPart = localPart;
Line 189... Line 182...
189
        }
182
        }
190
 
183
 
191
        // Methods
184
        // Methods
192
 
185
 
193
        /**
186
        /**
194
         * Returns the email address with punycoded domain name and TLD. You should use
187
         * Returns the email address with punycoded domain name and TLD. You should
195
         * this method to send emails.
188
         * use this method to send emails.
196
         *
189
         *
197
         * @return The email address with punycoded domain name and TLD.
190
         * @return The email address with punycoded domain name and TLD.
198
         */
191
         */
199
        public String getMailAddressPunycodedDomain() {
192
        public String getMailAddressPunycodedDomain() {
-
 
193
                if (this.domainPartPunycode.equals("")) {
-
 
194
                        return this.localPart;
-
 
195
                } else {
200
                return this.localPart + "@" + this.domainPartPunycode;
196
                        return this.localPart + "@" + this.domainPartPunycode;
201
        }
197
                }
-
 
198
        }
202
 
199
 
203
        /**
200
        /**
204
         * Returns the email address with internationalized domain names and TLD.
201
         * Returns the email address with internationalized domain names and TLD.
205
         *
202
         *
206
         * @return The email address with internationalized domain name and TLD.
203
         * @return The email address with internationalized domain name and TLD.
207
         */
204
         */
208
        public String getMailAddressUnicode() {
205
        public String getMailAddressUnicode() {
-
 
206
                if (this.domainPartUnicode.equals("")) {
-
 
207
                        return this.localPart;
-
 
208
                } else {
209
                return this.localPart + "@" + this.domainPartUnicode;
209
                        return this.localPart + "@" + this.domainPartUnicode;
210
        }
210
                }
-
 
211
        }
211
 
212
 
212
        /**
213
        /**
213
         * Returns a string which represents the mail address. If the constant
214
         * Returns a string which represents the mail address. If the constant
214
         * USE_UNICODE_AS_STANDARD is true, the internationalized domain names will
215
         * USE_UNICODE_AS_STANDARD is true, the internationalized domain names will
215
         * not translated into the corresponding Punycode. If false, then not.
216
         * not translated into the corresponding Punycode. If false, then not.
Line 277... Line 278...
277
         * @return A new instance of the email address object with the same
278
         * @return A new instance of the email address object with the same
278
         *         properties.
279
         *         properties.
279
         */
280
         */
280
        @Override
281
        @Override
281
        protected EMailAddress clone() throws CloneNotSupportedException {
282
        protected EMailAddress clone() throws CloneNotSupportedException {
282
                try {
-
 
283
                        return new EMailAddress(this.toString());
283
                return new EMailAddress(this.toString());
284
                } catch (InvalidMailAddressException e) {
-
 
285
                        return null;
-
 
286
                }
-
 
287
        }
-
 
288
 
-
 
289
        /**
-
 
290
         * Asks the mail syntax checker if the current mail address is valid or not.
-
 
291
         * Warning! This check is NOT performed automatically. There is no guarantee
-
 
292
         * that the syntax check is 100% correct. There might be mail address which
-
 
293
         * are valid but marked as invalid (because server disobeyed RFC rules etc)
-
 
294
         * and mail addresses which are invalid but marked valid (e.g. simply if
-
 
295
         * they were not assigned).
-
 
296
         *
-
 
297
         * @return Boolean which represents if the mail address is valid or not.
-
 
298
         */
-
 
299
        public boolean isSyntaxValid() {
-
 
300
                return MailSyntaxChecker.isMailValid(this);
-
 
301
        }
284
        }
302
 
285
 
303
        // ---------- STATIC FUNCTIONS ----------
286
        // ---------- STATIC FUNCTIONS ----------
304
 
287
 
305
        /**
288
        /**
Line 308... Line 291...
308
         * @param str
291
         * @param str
309
         *            The string which should be checked
292
         *            The string which should be checked
310
         * @return Boolean which shows if the string is not yet punicoded.
293
         * @return Boolean which shows if the string is not yet punicoded.
311
         */
294
         */
312
        protected static boolean isUnicode(String str) {
295
        protected static boolean isUnicode(String str) {
313
                if (str == null)
296
                if (str == null) {
314
                        return false;
297
                        return false;
-
 
298
                }
315
                return (!IDN.toASCII(str).equals(str));
299
                return (!IDN.toASCII(str).equals(str));
316
        }
300
        }
317
 
301
 
318
        /**
302
        /**
319
         * Determinates if a given string is in Punycode format.
303
         * Determinates if a given string is in Punycode format.
Line 321... Line 305...
321
         * @param str
305
         * @param str
322
         *            The string which should be checked
306
         *            The string which should be checked
323
         * @return Boolean which shows if the string is punycoded or not.
307
         * @return Boolean which shows if the string is punycoded or not.
324
         */
308
         */
325
        protected static boolean isPunycode(String str) {
309
        protected static boolean isPunycode(String str) {
326
                if (str == null)
310
                if (str == null) {
327
                        return false;
311
                        return false;
-
 
312
                }
328
                return (!IDN.toUnicode(str).equals(str));
313
                return (!IDN.toUnicode(str).equals(str));
329
        }
314
        }
330
}
315
}