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