]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/validator/emailaddress/FinancialsEmailAddressValidator.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / validator / emailaddress / FinancialsEmailAddressValidator.java
1 /*
2  * Copyright (C) 2016 - 2022 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.jfinancials.validator.emailaddress;
18
19 import java.text.MessageFormat;
20 import java.util.regex.Pattern;
21 import javax.enterprise.inject.spi.CDI;
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.jcoreee.validator.string.BaseStringValidator;
28 import org.mxchange.jfinancials.beans.contact.FinancialsContactWebRequestBean;
29 import org.mxchange.jfinancials.beans.contact.FinancialsContactWebRequestController;
30
31 /**
32  * A validator for email address validation
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @FacesValidator (value = "EmailAddressValidator")
37 public class FinancialsEmailAddressValidator extends BaseStringValidator {
38
39         /**
40          * Contact backing bean
41          */
42         private static FinancialsContactWebRequestController CONTACT_CONTROLLER;
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(FinancialsEmailAddressValidator.EMAIL_REGEX);
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 187_536_745_607_192L;
58
59         /**
60          * Whether empty data is allowed
61          */
62         private Boolean allowEmptyRequiredData;
63
64         /**
65          * Default constructor
66          */
67         public FinancialsEmailAddressValidator () {
68                 // Set allowEmpty to FALSE by default
69                 this.allowEmptyRequiredData = Boolean.FALSE;
70         }
71
72         /**
73          * Setter for allowEmptyRequiredData flag
74          * <p>
75          * @param allowEmptyRequiredData Whether empty values are allowed
76          */
77         public void setAllowEmptyRequiredData (final Boolean allowEmptyRequiredData) {
78                 this.allowEmptyRequiredData = allowEmptyRequiredData;
79         }
80
81         @Override
82         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
83                 // Validate parameter
84                 if (null == context) {
85                         // Throw NPE
86                         throw new NullPointerException("Parameter context is null"); //NOI18N
87                 } else if (null == component) {
88                         // Throw NPE again
89                         throw new NullPointerException("Parameter component is null"); //NOI18N
90                 } else if (null == this.allowEmptyRequiredData) {
91                         // Should not be NULL
92                         throw new IllegalStateException("this.allowEmptyRequiredData was set to null, this should not happen."); //NOI18N
93                 }
94
95                 // The required field
96                 final String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
97
98                 // Pre-validation (example: not null, not a string, empty string ...)
99                 super.preValidate(context, component, value, requiredFields, this.allowEmptyRequiredData);
100
101                 // Is the email address empty and allowed?
102                 if (null == value && this.allowEmptyRequiredData) {
103                         // Then accept this here
104                         return;
105                 } else if (null == value) {
106                         // Abort here
107                         throw new ValidatorException(new FacesMessage("No empty email address allowed.")); //NOI18N
108                 }
109
110                 // Get string from object ... ;-)
111                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
112                 final String emailAddress = String.valueOf(value).trim();
113
114                 // Checks if the email address matches a regex ("low-level" check)
115                 // @TODO Should also be done by <f:validatorRegex />)
116                 final boolean matches = PATTERN_MATCHER.matcher(emailAddress).matches(); //NOI18N
117
118                 // Is the email address valid?
119                 if (!matches) {
120                         // Generate message
121                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
122
123                         // Not matching
124                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
125                 }
126
127                 // Get client id (aka form id)
128                 final String clientId = component.getClientId();
129
130                 // Is the instance there?
131                 if (null == CONTACT_CONTROLLER) {
132                         // Get bean from CDI directly
133                         CONTACT_CONTROLLER = CDI.current().select(FinancialsContactWebRequestBean.class).get();
134                 }
135
136                 // Is it registered?
137                 final Boolean isRegistered = CONTACT_CONTROLLER.isEmailAddressRegistered(emailAddress);
138
139                 // Is the email address already registered?
140                 if ((!clientId.endsWith("resendEmailAddress")) && (isRegistered)) { //NOI18N
141                         // Generate message
142                         final String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
143
144                         // No, then abort here
145                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
146                 } else if ((clientId.endsWith("resendEmailAddress")) && (!isRegistered)) { //NOI18N
147                         // Generate message
148                         final String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
149
150                         // No, then abort here
151                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
152                 }
153         }
154
155 }