<copyfiles files="${file.reference.jcontacts-business-core.jar}" todir="${build.classes.dir}"/>
<copyfiles files="${file.reference.juser-core.jar}" todir="${build.classes.dir}"/>
<copyfiles files="${file.reference.juser-lib.jar}" todir="${build.classes.dir}"/>
+ <copyfiles files="${file.reference.jmailer-ee.jar}" todir="${build.classes.dir}"/>
<copyfiles files="${reference.addressbook-lib.jar}" todir="${build.classes.dir}"/>
<copyfiles files="${reference.juser-core.jar}" todir="${build.classes.dir}"/>
</target>
<basename file="${file.reference.jcontacts-business-core.jar}" property="manifest.file.reference.jcontacts-business-core.jar"/>
<basename file="${file.reference.juser-core.jar}" property="manifest.file.reference.juser-core.jar"/>
<basename file="${file.reference.juser-lib.jar}" property="manifest.file.reference.juser-lib.jar"/>
+ <basename file="${file.reference.jmailer-ee.jar}" property="manifest.file.reference.jmailer-ee.jar"/>
<basename file="${reference.addressbook-lib.jar}" property="manifest.reference.addressbook-lib.jar"/>
<basename file="${reference.juser-core.jar}" property="manifest.reference.juser-core.jar"/>
<manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update">
- <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 "/>
+ <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 jar-12 "/>
<attribute name="jar-1-Extension-Name" value="${manifest.file.reference.jcore.jar}"/>
<attribute name="jar-2-Extension-Name" value="${manifest.file.reference.jcoreee.jar}"/>
<attribute name="jar-3-Extension-Name" value="${manifest.file.reference.jcore-logger-lib.jar}"/>
<attribute name="jar-7-Extension-Name" value="${manifest.file.reference.jcontacts-business-core.jar}"/>
<attribute name="jar-8-Extension-Name" value="${manifest.file.reference.juser-core.jar}"/>
<attribute name="jar-9-Extension-Name" value="${manifest.file.reference.juser-lib.jar}"/>
- <attribute name="jar-10-Extension-Name" value="${manifest.reference.addressbook-lib.jar}"/>
- <attribute name="jar-11-Extension-Name" value="${manifest.reference.juser-core.jar}"/>
+ <attribute name="jar-10-Extension-Name" value="${manifest.file.reference.jmailer-ee.jar}"/>
+ <attribute name="jar-11-Extension-Name" value="${manifest.reference.addressbook-lib.jar}"/>
+ <attribute name="jar-12-Extension-Name" value="${manifest.reference.juser-core.jar}"/>
</manifest>
</target>
<target depends="compile" name="library-inclusion-in-manifest">
<copyfiles files="${file.reference.jcontacts-business-core.jar}" todir="${dist.ear.dir}/lib"/>
<copyfiles files="${file.reference.juser-core.jar}" todir="${dist.ear.dir}/lib"/>
<copyfiles files="${file.reference.juser-lib.jar}" todir="${dist.ear.dir}/lib"/>
+ <copyfiles files="${file.reference.jmailer-ee.jar}" todir="${dist.ear.dir}/lib"/>
<copyfiles files="${reference.addressbook-lib.jar}" todir="${dist.ear.dir}/lib"/>
<copyfiles files="${reference.juser-core.jar}" todir="${dist.ear.dir}/lib"/>
<manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update"/>
package org.mxchange.jusercore.model.email_address;
import java.text.MessageFormat;
+import java.util.GregorianCalendar;
import java.util.List;
+import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
+import javax.faces.FacesException;
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.ObjectMessage;
+import javax.jms.Queue;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jmailee.model.delivery.DeliverableEmail;
+import org.mxchange.jmailee.model.delivery.Mailer;
import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
+import org.mxchange.jusercore.model.user.UserUtils;
/**
* A session bean for changing email addresses
*/
private static final long serialVersionUID = 182_698_165_971_548L;
+ /**
+ * Connection
+ */
+ private Connection connection;
+
+ /**
+ * Object message
+ */
+ private ObjectMessage message;
+
+ /**
+ * Message producer
+ */
+ private MessageProducer messageProducer;
+
+ /**
+ * Mailer message queue
+ */
+ private Queue queue;
+
+ /**
+ * Session instance
+ */
+ private Session session;
+
/**
* User bean
*/
this.getLoggerBeanLocal().logTrace("allQueuedAddressesAsList: CALLED!"); //NOI18N
// Get named query
- Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", List.class);
+ Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", List.class); //NOI18N
// Get all entries
List<String> emailAddresses = query.getResultList();
}
@Override
- public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailAddress) {
+ public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange) {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailAddress={0} - CALLED!", emailAddress));
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: emailChange={0} - CALLED!", emailChange)); //NOI18N
- // user should not be null
- if (null == emailAddress) {
+ // Email address change should be valid
+ if (null == emailChange) {
// Abort here
- throw new NullPointerException("emailAddress is null"); //NOI18N
- } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
+ throw new NullPointerException("emailChange is null"); //NOI18N
+ } else if (emailChange.getEmailChangeUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N
+ } else if (emailChange.getEmailChangeUser().getUserId() == null) {
+ // Throw NPE again
+ throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N
+ } else if (emailChange.getEmailChangeUser().getUserId() < 1) {
+ // Not valid id
+ throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N
+ } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) {
// User does not exist
- throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
- } else if (this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
+ throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N
+ } else if (emailChange.getEmailAddress().trim().isEmpty()) {
+ // Email address is empty
+ throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N
+ } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) {
// Email address is already enqueued
- throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailAddress.getEmailAddress()));
+ throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N
}
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ // The email change is not (yet) there, add secure hash and "created" timestamp
+ emailChange.setEmailChangeCreated(new GregorianCalendar());
+ this.generateSecureHash(emailChange);
+
+ // Persist it
+ //this.getEntityManager().persist(emailChange);
+
+ // Get mailer instance
+ DeliverableEmail mailer = new Mailer();
+
+ // Send out email change
+ mailer.sendEmailChangeMail(this.messageProducer, this.message, emailChange);
+ }
+
+ /**
+ * Initialization of this bean
+ */
+ @PostConstruct
+ public void init () {
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Get factory from JMS resource
+ QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/addressbook-queue-factory"); //NOI18N
+
+ // Lookup queue
+ this.queue = (Queue) context.lookup("jms/addressbook-email-queue"); //NOI18N
+
+ // Create connection
+ this.connection = connectionFactory.createConnection();
+
+ // Init session instance
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // And message producer
+ this.messageProducer = this.session.createProducer(this.queue);
+
+ // Finally the message instance itself
+ this.message = this.session.createObjectMessage();
+ } catch (final NamingException | JMSException e) {
+ // Continued to throw
+ throw new FacesException(e);
+ }
}
@Override
@Override
public void updateEmailAddress (final ChangeableEmailAddress emailAddress) {
// Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: emailAddress={0} - CALLED!", emailAddress));
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: emailAddress={0} - CALLED!", emailAddress)); //NOI18N
- // user should not be null
+ // Email address change should be valid
if (null == emailAddress) {
// Abort here
throw new NullPointerException("emailAddress is null"); //NOI18N
} else if (emailAddress.getEmailChangeId() < 1) {
// Not valid
throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N
+ } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
+ // Email address is empty
+ throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
} else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) {
// User does not exist
throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N
} else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) {
// Email address is not enqueued
- throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress()));
+ throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress())); //NOI18N
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
+ /**
+ * Generates a secure, unique hash for given email address change. This
+ * requires to check if the hash is really not there.
+ * <p>
+ * @param emailAddress Email address change
+ */
+ private void generateSecureHash (final ChangeableEmailAddress emailAddress) {
+ // Email address change should be valid
+ if (null == emailAddress) {
+ // Abort here
+ throw new NullPointerException("emailAddress is null"); //NOI18N
+ } else if (emailAddress.getEmailAddress().trim().isEmpty()) {
+ // Email address is empty
+ throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N
+ }
+
+ // Initialize loop with null
+ String hash = null;
+
+ // Default is not used
+ boolean isUsed = true;
+
+ // Search for free hash
+ while (isUsed) {
+ // Generate hash, there is already in UserUtils a nice method that can be used for this purpose.
+ hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N
+
+ // The hash *may* be unique, better test it
+ Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N
+
+ // Set hash as parameter
+ query.setParameter("hash", hash); //NOI18N
+
+ // Try to get single result
+ try {
+ // Get single result
+ ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult();
+ } catch (final NoResultException ex) {
+ // Not found
+ isUsed = false;
+ }
+ }
+
+ // hash should not be null and set
+ assert (hash != null) : "hash is null"; //NOI18N
+ assert (!hash.isEmpty()) : "hash is empty"; //NOI18N
+
+ // Set it in email change
+ emailAddress.setEmailChangeHash(hash);
+ }
+
}