]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/validator/emailaddress/JobsEmailAddressValidator.java
Please cherry-pick:
[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.List;
21 import java.util.regex.Pattern;
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.convert.ConverterException;
26 import javax.faces.validator.FacesValidator;
27 import javax.faces.validator.Validator;
28 import javax.faces.validator.ValidatorException;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
33 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
34
35 /**
36  * A validator for email address validation
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @FacesValidator ("EmailAddressValidator")
41 public class JobsEmailAddressValidator extends BaseStringValidator implements Validator {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 187_536_745_607_192L;
47
48         /**
49          * Contact session-scoped bean
50          */
51         private ContactSessionBeanRemote contactBean;
52
53         /**
54          * Cached list of all email addresses
55          */
56         private List<String> emailAddresses;
57
58         /**
59          * Default constructor
60          */
61         public JobsEmailAddressValidator () {
62         }
63
64         @Override
65         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
66                 // The required field
67                 String[] requiredFields = {"emailAddress", "emailAddressRepeat", "resendEmailAddress"}; //NOI18N
68
69                 // Pre-validation (example: not null, not a string, empty string ...)
70                 super.preValidate(context, component, value, requiredFields, false);
71
72                 // Get string from object ... ;-)
73                 // @TODO Add IDN support (GNU lib?) Search for emailAddressRepeat
74                 String emailAddress = String.valueOf(value);
75
76                 // Checks if the email address matches a regex ("low-level" check)
77                 // @TODO Should also be done by <f:validatorRegex />)
78                 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
79
80                 // Is the email address valid?
81                 if (!matches) {
82                         // Generate message
83                         String message = MessageFormat.format("Email address {0} does not match regular expression.", emailAddress); //NOI18N
84
85                         // Not matching
86                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
87                 }
88
89                 synchronized (this) {
90                         // Is the EJB instanciated?
91                         if (null == this.contactBean) {
92                                 // Try it
93                                 try {
94                                         // Get initial context
95                                         Context initialContext = new InitialContext();
96
97                                         // Try to lookup
98                                         this.contactBean = (ContactSessionBeanRemote) initialContext.lookup("java:global/jjobs-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
99
100                                         // Get whole list of email addresses
101                                         this.emailAddresses = this.contactBean.getEmailAddressList();
102                                 } catch (final NamingException ex) {
103                                         // Continue to throw it
104                                         throw new ConverterException(MessageFormat.format("initialContext.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
105                                 }
106                         }
107                 }
108
109                 // Get client id (aka form id)
110                 String clientId = component.getClientId();
111
112                 // Is the email address already registered?
113                 if ((!clientId.endsWith("resendEmailAddress")) && (this.emailAddresses.contains(emailAddress))) { //NOI18N
114                         // Generate message
115                         String message = MessageFormat.format("Email address {0} is already registered.", emailAddress); //NOI18N
116
117                         // No, then abort here
118                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
119                 } else if ((clientId.endsWith("resendEmailAddress")) && (!this.emailAddresses.contains(emailAddress))) { //NOI18N
120                         // Generate message
121                         String message = MessageFormat.format("Email address {0} is not registered.", emailAddress); //NOI18N
122
123                         // No, then abort here
124                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
125                 }
126         }
127
128 }