]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/validator/emailaddress/PizzaEmailAddressValidator.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / validator / emailaddress / PizzaEmailAddressValidator.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.pizzaapplication.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.ValidatorException;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
30 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
31
32 /**
33  * A validator for email address validation
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @FacesValidator ("EmailAddressValidator")
38 public class PizzaEmailAddressValidator extends BaseStringValidator {
39
40         /**
41          * Contact session-scoped bean
42          */
43         private static ContactSessionBeanRemote CONTACT_BEAN;
44
45         /**
46          * Email pattern
47          */
48         private static final String EMAIL_REGEX = "^([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})(\\]?)$"; //NOI18N
49
50         /**
51          * Pattern matcher
52          */
53         private static final Pattern PATTERN_MATCHER = Pattern.compile(PizzaEmailAddressValidator.EMAIL_REGEX);
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 187_536_745_607_192L;
59
60         @Override
61         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
62                 // Is the instance there?
63                 if (CONTACT_BEAN == null) {
64                         try {
65                                 // Not yet, attempt lookup
66                                 final Context initial = new InitialContext();
67
68                                 // Lookup EJB
69                                 CONTACT_BEAN = (ContactSessionBeanRemote) initial.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote");
70                         } catch (final NamingException ex) {
71                                 // Throw it again
72                                 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
73                         }
74                 }
75
76                 // The required field
77                 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
78
79                 // Default is to reject empty email address fields
80                 Boolean allowEmptyValue = Boolean.FALSE;
81
82                 // Is attribute "allowEmptyValue" set?
83                 if (component.getAttributes().containsKey("allowEmptyValue")) { //NOI18N
84                         // Get attribute
85                         final Object attribute = component.getAttributes().get("allowEmptyValue"); //NOI18N
86
87                         // Make sure, it is Boolean as no String is accepted anymore
88                         if (!(attribute instanceof String)) {
89                                 // Not valid attribute, please use "true" or "false" (default)
90                                 throw new IllegalArgumentException("allowEmptyValue must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
91                         }
92
93                         // Securely cast it
94                         allowEmptyValue = Boolean.parseBoolean((String) attribute);
95                 }
96
97                 // Pre-validation (example: not null, not a string, empty string ...)
98                 super.preValidate(context, component, value, requiredFields, allowEmptyValue);
99
100                 // Is the email address empty and allowed?
101                 if (null == value && allowEmptyValue) {
102                         // Then accept this here
103                         return;
104                 } else if (null == value) {
105                         // Abort here
106                         throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
107                 }
108
109                 // Get string from object ... ;-)
110                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
111                 final String emailAddress = String.valueOf(value).trim();
112
113                 // Checks if the email address matches a regex ("low-level" check)
114                 // @TODO Should also be done by <f:validatorRegex />)
115                 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
116
117                 // Is the email address valid?
118                 if (!matches) {
119                         // Generate message
120                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
121
122                         // Not matching
123                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
124                 }
125
126                 // Get client id (aka form id)
127                 final String clientId = component.getClientId();
128
129                 // Is it registered?
130                 final Boolean isRegistered = CONTACT_BEAN.isEmailAddressRegistered(emailAddress);
131
132                 // Is the email address already registered?
133                 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
134                         // Generate message
135                         final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
136
137                         // No, then abort here
138                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
139                 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
140                         // Generate message
141                         final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
142
143                         // No, then abort here
144                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
145                 }
146         }
147
148 }