]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/validator/emailaddress/AddressbookEmailAddressValidator.java
Continued with resending confirmation links:
[addressbook-war.git] / src / java / org / mxchange / addressbook / validator / emailaddress / AddressbookEmailAddressValidator.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.validator.emailaddress;
18
19 import java.text.MessageFormat;
20 import java.util.regex.Pattern;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.FacesValidator;
25 import javax.faces.validator.Validator;
26 import javax.faces.validator.ValidatorException;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
32 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
33
34 /**
35  * A validator for email address validation
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @FacesValidator ("EmailAddressValidator")
40 public class AddressbookEmailAddressValidator extends BaseStringValidator implements Validator {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 187_536_745_607_192L;
46
47         /**
48          * Contact session bean
49          */
50         private final ContactSessionBeanRemote contactBean;
51
52         /**
53          * Default constructor
54          */
55         public AddressbookEmailAddressValidator () {
56                 // Try it
57                 try {
58                         // Get initial context
59                         Context context = new InitialContext();
60
61                         // Try to lookup
62                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
63                 } catch (final NamingException e) {
64                         // Throw again
65                         throw new FaceletException(e);
66                 }
67         }
68
69         @Override
70         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
71                 // Trace message
72                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
73
74                 // The required field
75                 String[] requiredFields = {"emailAddress", "emailAddressRepeat"}; //NOI18N
76
77                 // Pre-validation (example: not null, not a string, empty string ...)
78                 super.preValidate(context, component, value, requiredFields, false);
79
80                 // Get string from object ... ;-)
81                 String emailAddress = String.valueOf(value);
82
83                 // Checks if the email address matches a regex ("low-level" check, should also be done by <f:validatorRegex />)
84                 boolean matches = Pattern.matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", emailAddress); //NOI18N
85
86                 // Is the email address valid?
87                 if (!matches) {
88                         // Generate message
89                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
90
91                         // Not matching
92                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
93                 }
94
95                 // Is the email address already registered?
96                 if (this.contactBean.isEmailAddressRegistered(emailAddress)) {
97                         // Generate message
98                         String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
99
100                         // No, then abort here
101                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
102                 }
103
104                 // Trace message
105                 //* NOISY-DEBUG: */ System.out.println("validate: EXIT!"); //NOI18N
106         }
107
108 }