2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.email_address;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.Query;
30 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
31 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
32 import org.mxchange.jusercore.model.user.UserUtils;
35 * A session bean for changing email addresses
37 * @author Roland Haeder<roland@mxchange.org>
39 @Stateless (name = "emailchange", description = "A bean handling email changes")
40 public class AddressbookEmailChangeSessionBean extends BaseAddressbookDatabaseBean implements EmailChangeSessionBeanRemote {
45 private static final long serialVersionUID = 182_698_165_971_548L;
51 private UserSessionBeanRemote userBean;
56 public AddressbookEmailChangeSessionBean () {
60 @SuppressWarnings ("unchecked")
61 public List<String> allQueuedAddresses () {
63 this.getLoggerBeanLocal().logTrace("allQueuedAddressesAsList: CALLED!"); //NOI18N
66 Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", String.class); //NOI18N
69 List<String> emailAddresses = query.getResultList();
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allQueuedAddressesAsList: emailAddresses.size()={0} - EXIT!", emailAddresses.size())); //NOI18N
75 return emailAddresses;
79 public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange, final String baseUrl) {
81 this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailChange={0},baseUrl={1} - CALLED!", emailChange, baseUrl)); //NOI18N
83 // Email address change should be valid
84 if (null == emailChange) {
86 throw new NullPointerException("emailChange is null"); //NOI18N
87 } else if (emailChange.getEmailChangeUser() == null) {
89 throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
90 } else if (emailChange.getEmailChangeUser().getUserId() == null) {
92 throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
93 } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
95 throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
96 } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) {
97 // User does not exist
98 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N
99 } else if (emailChange.getEmailAddress().trim().isEmpty()) {
100 // Email address is empty
101 throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N
102 } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) {
103 // Email address is already enqueued
104 throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N
107 // The email change is not (yet) there, add secure hash and "created" timestamp
108 emailChange.setEmailChangeCreated(new GregorianCalendar());
109 this.generateSecureHash(emailChange);
112 //this.getEntityManager().persist(emailChange);
115 Address emailAddress;
118 // Create email address and set
119 emailAddress = new InternetAddress(emailChange.getEmailAddress());
120 } catch (final AddressException ex) {
122 throw new EJBException(ex);
126 this.sendEmail("Email change", "email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl); //NOI18N
129 this.getLoggerBeanLocal().logTrace("enqueueEmailAddressForChange - EXIT!"); //NOI18N
133 public boolean isEmailAddressEnqueued (final String emailAddress) {
135 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
137 // Create query instance
138 Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N
140 // Add email address as parameter
141 query.setParameter("email", emailAddress); //NOI18N
143 // Initialize variable
144 boolean isFound = false;
148 // Try to get single result
149 ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
153 } catch (final NoResultException ex) {
155 this.getLoggerBeanLocal().logException(ex);
159 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressEnqueued: isFound={0} - EXIT!", isFound)); //NOI18N
166 public void updateEmailAddress (final ChangeableEmailAddress emailAddress) {
168 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
170 // Email address change should be valid
171 if (null == emailAddress) {
173 throw new NullPointerException("emailAddress is null"); //NOI18N
174 } else if (emailAddress.getEmailChangeId() == null) {
176 throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N
177 } else if (emailAddress.getEmailChangeId() < 1) {
179 throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
180 } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
181 // Email address is empty
182 throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
183 } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
184 // User does not exist
185 throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
186 } else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
187 // Email address is not enqueued
188 throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress())); //NOI18N
191 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
195 * Generates a secure, unique hash for given email address change. This
196 * requires to check if the hash is really not there.
198 * @param emailAddress Email address change
200 private void generateSecureHash (final ChangeableEmailAddress emailAddress) {
201 // Email address change should be valid
202 if (null == emailAddress) {
204 throw new NullPointerException("emailAddress is null"); //NOI18N
205 } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
206 // Email address is empty
207 throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
210 // Initialize loop with null
213 // Default is not used
214 boolean isUsed = true;
216 // Search for free hash
218 // Generate hash, there is already in UserUtils a nice method that can be used for this purpose.
219 hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N
221 // The hash *may* be unique, better test it
222 Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N
224 // Set hash as parameter
225 query.setParameter("hash", hash); //NOI18N
227 // Try to get single result
230 ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
231 } catch (final NoResultException ex) {
237 // hash should not be null and set
238 assert (hash != null) : "hash is null"; //NOI18N
239 assert (!hash.isEmpty()) : "hash is empty"; //NOI18N
241 // Set it in email change
242 emailAddress.setEmailChangeHash(hash);