<commit_before>/*
 * =============================================================================
 * Copyright 1998-2015, IAESTE Internet Development Team. All rights reserved.
 * ----------------------------------------------------------------------------
 * Project: IntraWeb Services (iws-ejb) - net.iaeste.iws.ejb.emails.EmailSender
 * -----------------------------------------------------------------------------
 * This software is provided by the members of the IAESTE Internet Development
 * Team (IDT) to IAESTE A.s.b.l. It is for internal use only and may not be
 * redistributed. IAESTE A.s.b.l. is not permitted to sell this software.
 *
 * This software is provided "as is"; the IDT or individuals within the IDT
 * cannot be held legally responsible for any problems the software may cause.
 * =============================================================================
 */
package net.iaeste.iws.ejb.emails;

import net.iaeste.iws.api.constants.IWSErrors;
import net.iaeste.iws.api.exceptions.IWSException;
import net.iaeste.iws.common.configuration.Settings;
import net.iaeste.iws.ejb.cdi.IWSBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.Serializable;
import java.util.Properties;

/**
 * The sending information (port, addresses) will be injected using JNDI.
 *
 * @author  <NAME> / last $Author:$
 * @version $Revision:$ / $Date:$
 * @since   IWS 1.0
 */
@MessageDriven(
        mappedName = "jms/queue/iwsEmailQueue", /*required by glassfish*/
        activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                // Not sure if the following should be "destination" or "destinationLookup".
                // I took the "destination", as that is what I've used in a JBoss 6.x project
                // ... destinationLookup seems to work on glassfish
                @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/queue/iwsEmailQueue"),
                @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
        }
//        activationConfig = {
//                @ActivationConfigProperty(
//                        propertyName = "destinationType",
//                        propertyValue = "javax.jms.Queue"),
//                @ActivationConfigProperty(/* required by jboss */
//                        propertyName = "destinationLookup",
//                        propertyValue = "jms/queue/iwsEmailQueue")
//        }
)
public class EmailSender implements MessageListener {

    private static final Logger log = LoggerFactory.getLogger(EmailSender.class);

    @Inject @IWSBean private Settings settings;

    /**
     * Default constructor
     *
     * Log message could be delated once we are sure it's working properly
     */
    public EmailSender() {
        log.info("Starting EmailSender");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onMessage(final Message message) {
        if (message instanceof ObjectMessage) {
            final ObjectMessage msg = (ObjectMessage) message;
            try {
                final Serializable object = msg.getObject();
                if (object instanceof EmailMessage) {<commit_msg>Added todo, mailsender is very noisy when starting up.<commit_after>/*
 * =============================================================================
 * Copyright 1998-2015, IAESTE Internet Development Team. All rights reserved.
 * ----------------------------------------------------------------------------
 * Project: IntraWeb Services (iws-ejb) - net.iaeste.iws.ejb.emails.EmailSender
 * -----------------------------------------------------------------------------
 * This software is provided by the members of the IAESTE Internet Development
 * Team (IDT) to IAESTE A.s.b.l. It is for internal use only and may not be
 * redistributed. IAESTE A.s.b.l. is not permitted to sell this software.
 *
 * This software is provided "as is"; the IDT or individuals within the IDT
 * cannot be held legally responsible for any problems the software may cause.
 * =============================================================================
 */
package net.iaeste.iws.ejb.emails;

import net.iaeste.iws.api.constants.IWSErrors;
import net.iaeste.iws.api.exceptions.IWSException;
import net.iaeste.iws.common.configuration.Settings;
import net.iaeste.iws.ejb.cdi.IWSBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Inject;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.Serializable;
import java.util.Properties;

/**
 * The sending information (port, addresses) will be injected using JNDI.
 *
 * @author  <NAME> / last $Author:$
 * @version $Revision:$ / $Date:$
 * @since   IWS 1.0
 */
@MessageDriven(
        mappedName = "jms/queue/iwsEmailQueue", /*required by glassfish*/
        activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                // Not sure if the following should be "destination" or "destinationLookup".
                // I took the "destination", as that is what I've used in a JBoss 6.x project
                // ... destinationLookup seems to work on glassfish
                @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/queue/iwsEmailQueue"),
                @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
        }
//        activationConfig = {
//                @ActivationConfigProperty(
//                        propertyName = "destinationType",
//                        propertyValue = "javax.jms.Queue"),
//                @ActivationConfigProperty(/* required by jboss */
//                        propertyName = "destinationLookup",
//                        propertyValue = "jms/queue/iwsEmailQueue")
//        }
)
public class EmailSender implements MessageListener {

    private static final Logger log = LoggerFactory.getLogger(EmailSender.class);

    @Inject @IWSBean private Settings settings;

    /**
     * Default constructor
     *
     * Log message could be delated once we are sure it's working properly
     */
    public EmailSender() {
        // TODO figure out why 30 bean instances is started initially - we're not sending that many mails!
        log.info("Starting EmailSender");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onMessage(final Message message) {
        if (message instanceof ObjectMessage) {
            final ObjectMessage msg = (ObjectMessage) message;
            try {
                final Serializable object = msg.getObject();
                if (object instanceof EmailMessage) {
{'fraction_non_alphanumeric': 0.17063907805133577, 'fraction_numerical': 0.003666841278156103, 'mean_word_length': 2.5670247547874823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4440972', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1920, 'n_words': 554}