]> git.mxchange.org Git - addressbook-mailer-ejb.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 17 May 2016 13:35:48 +0000 (15:35 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 21 May 2016 11:48:05 +0000 (13:48 +0200)
- added message-driven bean for mail delivery (generic)
- added method init() method to initialize queue/factory
- implemented business method resendConfirmationLink() (unfinished)
- the business method enqueueEmailAddressForChange() now uses JMS and not directly calling the mailer

Signed-off-by: Roland Häder <roland@mxchange.org>
nbproject/project.properties
src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java
src/java/org/mxchange/addressbook/mailer/model/delivery/EmailDeliveryMessageBean.java [new file with mode: 0644]
src/java/org/mxchange/jusercore/model/email_address/AddressbookEmailChangeSessionBean.java
src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java

index 8786f53d828b631bbcadbdcebbf3113493096b2c..469c55a7075b9e0f6fd463e23bd0f7d6eb387271 100644 (file)
@@ -97,10 +97,11 @@ javadoc.windowtitle=Addressbook EJBs
 meta.inf=${source.root}/conf
 meta.inf.excludes=sun-cmp-mappings.xml
 platform.active=default_platform
-project.addressbook-lib=../addressbook-lib
-project.addressbook-mailer=../../NetBeansProjects/addressbook-mailer
 project.juser-core=../juser-core
 project.license=agpl30
+project.addressbook-lib=../addressbook-lib
+project.addressbook-mailer=../../NetBeansProjects/addressbook-mailer
+project.serviceLocator.class=org.mxchange.addressbook.mailer.model.delivery.AddressbookMailer
 reference.addressbook-lib.jar=${project.addressbook-lib}/dist/addressbook-lib.jar
 reference.addressbook-mailer.jar=${project.addressbook-mailer}/dist/addressbook-mailer.jar
 resource.dir=setup
index 145100231d2fe230f897f65be8839d198ca59024..feb4cdef9cae32013c208b35288cff84d82af8bb 100644 (file)
  */
 package org.mxchange.addressbook.beans.resendlink;
 
+import de.chotime.landingpage.beans.resendlink.ResendLinkSessionBeanRemote;
+import java.text.MessageFormat;
+import java.util.Locale;
+import javax.annotation.PostConstruct;
 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 org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
  * A session-based EJB for resending confirmation links
@@ -32,4 +49,95 @@ public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBea
         */
        private static final long serialVersionUID = 71_546_726_857_195_360L;
 
+       /**
+        * 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;
+
+       /**
+        * Initialization of this bean
+        */
+       @PostConstruct
+       public void init () {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
+
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // Get factory from JMS resource
+                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
+
+                       // Lookup queue
+                       this.queue = (Queue) context.lookup("jms/jlandingpage-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
+       public String resendConfirmationLink (final User user, final Locale locale) {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("resendConfirmationLink: user={0} - CALLED!", user)); //NOI18N
+
+               // The user instance should be valid
+               if (null == user) {
+                       // Throw NPE
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Invalid id number
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
+               } else if (user.getUserConfirmKey() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("this.userConfirmKey is null"); //NOI18N
+               } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) {
+                       // User account status is not UNCONFIRMED
+                       throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N
+               } else if (null == locale) {
+                       // Locale should be set
+                       throw new NullPointerException("locale is null"); //NOI18N
+               }
+
+               // @TODO Unfinished!
+               // All fine
+               return "resend_done"; //NOI18N
+       }
+
 }
diff --git a/src/java/org/mxchange/addressbook/mailer/model/delivery/EmailDeliveryMessageBean.java b/src/java/org/mxchange/addressbook/mailer/model/delivery/EmailDeliveryMessageBean.java
new file mode 100644 (file)
index 0000000..abf7187
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.mailer.model.delivery;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import javax.ejb.ActivationConfigProperty;
+import javax.ejb.MessageDriven;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
+
+/**
+ * A message-driven bean for sending out emails
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@MessageDriven (activationConfig = {
+       @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jlandingpage-email-queue"),
+       @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
+})
+public class EmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 75_638_176_619_024L;
+
+       /**
+        * Mailer instance
+        */
+       private final DeliverableAddressbookEmail mailer;
+
+       /**
+        * Default constructor
+        */
+       public EmailDeliveryMessageBean () {
+               // Init mailer instance
+               this.mailer = new AddressbookMailer();
+       }
+
+       @Override
+       public void onMessage (final Message message) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
+
+               // The parameter should be valid
+               if (null == message) {
+                       // Throw NPE
+                       throw new NullPointerException("message is null"); //NOI18N
+               } else if (!(message instanceof ObjectMessage)) {
+                       // Not implementing right interface
+                       throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
+               }
+
+               // Securely cast it
+               ObjectMessage objectMessage = (ObjectMessage) message;
+
+               // Init variable
+               Serializable serializable;
+
+               try {
+                       // Get object from message
+                       serializable = objectMessage.getObject();
+               } catch (final JMSException ex) {
+                       // Log it and don't continue any further
+                       this.getLoggerBeanLocal().logException(ex);
+                       return;
+               }
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
+       }
+
+}
index f63a0ff636bb123c2478a29c782ee5f6e9545cb3..f65a2ee4fdc40ae158860a87905d446fa363fbac 100644 (file)
@@ -19,6 +19,8 @@ package org.mxchange.jusercore.model.email_address;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import java.util.List;
+import java.util.Locale;
+import java.util.Properties;
 import javax.annotation.PostConstruct;
 import javax.ejb.EJB;
 import javax.ejb.EJBException;
@@ -31,13 +33,14 @@ import javax.jms.ObjectMessage;
 import javax.jms.Queue;
 import javax.jms.QueueConnectionFactory;
 import javax.jms.Session;
+import javax.mail.Address;
+import javax.mail.internet.AddressException;
+import javax.mail.internet.InternetAddress;
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
-import org.mxchange.addressbook.mailer.model.delivery.AddressbookMailer;
-import org.mxchange.addressbook.mailer.model.delivery.DeliverableAddressbookEmail;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
 import org.mxchange.jusercore.model.user.UserUtils;
@@ -146,11 +149,42 @@ public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implemen
 
                // Persist it
                //this.getEntityManager().persist(emailChange);
-               // Get mailer instance
-               DeliverableAddressbookEmail mailer = new AddressbookMailer();
 
-               // Send out email change
-               mailer.sendEmailChangeMail(this.messageProducer, this.message, emailChange);
+               // Prepare mail wrapper
+               WrapableEmailDelivery emailWrapper = new EmailDeliveryWrapper();
+
+               try {
+                       // Create email address and set
+                       Address emailAddress = new InternetAddress(emailChange.getEmailAddress());
+                       emailWrapper.setRecipient(emailAddress);
+               } catch (final AddressException ex) {
+                       // Throw again
+                       throw new EJBException(ex);
+               }
+
+               // Set all values
+               Properties variables = UserUtils.getAllUserFields(emailChange.getEmailChangeUser());
+
+               // Set all
+               // @TODO Get locale from user + language from message bundle
+               emailWrapper.setLocale(Locale.GERMAN);
+               emailWrapper.setSubjectLine("Email change");
+               emailWrapper.setTemplateName("email_change"); //NOI18N
+               emailWrapper.setTemplateVariables(variables);
+
+               try {
+                       // Send out email change
+                       this.message.setObject(emailWrapper);
+
+                       // Send message
+                       this.sendMessage(this.message);
+               } catch (final JMSException ex) {
+                       // Throw again
+                       throw new EJBException(ex);
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("enqueueEmailAddressForChange - EXIT!"); //NOI18N
        }
 
        /**
@@ -158,15 +192,18 @@ public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implemen
         */
        @PostConstruct
        public void init () {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
+
                try {
                        // Get initial context
                        Context context = new InitialContext();
 
                        // Get factory from JMS resource
-                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/addressbook-queue-factory"); //NOI18N
+                       QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/jlandingpage-queue-factory"); //NOI18N
 
                        // Lookup queue
-                       this.queue = (Queue) context.lookup("jms/addressbook-email-queue"); //NOI18N
+                       this.queue = (Queue) context.lookup("jms/jlandingpage-email-queue"); //NOI18N
 
                        // Create connection
                        this.connection = connectionFactory.createConnection();
@@ -298,4 +335,22 @@ public class AddressbookEmailChangeSessionBean extends BaseDatabaseBean implemen
                emailAddress.setEmailChangeHash(hash);
        }
 
+       /**
+        * Sends given message to configured queue
+        * <p>
+        * @param message Message to send
+        * <p>
+        * @throws JMSException if something went wrong
+        */
+       private void sendMessage (final ObjectMessage message) throws JMSException {
+               // The parameter should be valid
+               if (null == message) {
+                       // Throw NPE
+                       throw new NullPointerException("message is null"); //NOI18N
+               }
+
+               // Send it
+               this.messageProducer.send(message);
+       }
+
 }
index 088366686c62eb00d08e065de21296c1c648a0a4..1427119289b7a20df0025dff728642ebb576cd28 100644 (file)
@@ -160,4 +160,5 @@ public class AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp
                // Return it
                return addedUser;
        }
+
 }