]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/validator/emailaddress/JobsEmailAddressValidator.java
feb195d83f14b15259cdffb4f3c2afb6e716e47a
[jjobs-war.git] / src / java / org / mxchange / jjobs / validator / emailaddress / JobsEmailAddressValidator.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.jjobs.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.convert.ConverterException;
25 import javax.faces.validator.FacesValidator;
26 import javax.faces.validator.Validator;
27 import javax.faces.validator.ValidatorException;
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 Häder<roland@mxchange.org>
38  */
39 @FacesValidator ("EmailAddressValidator")
40 public class JobsEmailAddressValidator 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-scoped bean
49          */
50         private ContactSessionBeanRemote contactBean;
51
52         /**
53          * Default constructor
54          */
55         public JobsEmailAddressValidator () {
56         }
57
58         @Override
59         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
60                 // The required field
61                 String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
62
63                 // Pre-validation (example: not null, not a string, empty string ...)
64                 super.preValidate(context, component, value, requiredFields, false);
65
66                 // Get string from object ... ;-)
67                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
68                 String emailAddress = String.valueOf(value);
69
70                 // Checks if the email address matches a regex ("low-level" check)
71                 // @TODO Should also be done by <f:validatorRegex />)
72                 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
73
74                 // Is the email address valid?
75                 if (!matches) {
76                         // Generate message
77                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
78
79                         // Not matching
80                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
81                 }
82
83                 synchronized (this) {
84                         // Is the EJB instanciated?
85                         if (null == this.contactBean) {
86                                 // Try it
87                                 try {
88                                         // Get initial context
89                                         Context initialContext = new InitialContext();
90
91                                         // Try to lookup
92                                         this.contactBean = (ContactSessionBeanRemote) initialContext.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
93                                 } catch (final NamingException ex) {
94                                         // Continue to throw it
95                                         throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
96                                 }
97                         }
98                 }
99
100                 // Get client id (aka form id)
101                 String clientId = component.getClientId();
102
103                 // Is the email address already registered?
104                 if ((!clientId.endsWith("resendEmailAddress")) && (this.contactBean.isEmailAddressRegistered(emailAddress))) { //NOI18N
105                         // Generate message
106                         String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
107
108                         // No, then abort here
109                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
110                 } else if ((clientId.endsWith("resendEmailAddress")) && (!this.contactBean.isEmailAddressRegistered(emailAddress))) { //NOI18N
111                         // Generate message
112                         String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
113
114                         // No, then abort here
115                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
116                 }
117         }
118
119 }