Subversion Repositories bacoAlunos

Compare Revisions

Ignore whitespace Rev 1451 → Rev 1452

/branches/v3/impl/src/java/pt/estgp/estgweb/services/email/impl/EmailTransport.java
New file
0,0 → 1,135
package pt.estgp.estgweb.services.email.impl;
 
import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
 
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import java.util.Properties;
 
/**
* Created by jorgemachado on 13/06/16.
*/
public class EmailTransport
{
 
private static final Logger logger = Logger.getLogger(EmailTransport.class);
 
Session session = null;
Transport transport = null;
 
String sendFromAddress;
String password;
String identifier;
 
public EmailTransport(String identifier, String sendFromAddress,String password)
{
this.identifier = identifier;
this.sendFromAddress = sendFromAddress;
this.password = password;
}
 
public synchronized Transport getEmailTransport()
{
if(session == null || transport == null || !transport.isConnected())
{
logger.info("Transport(" + identifier + ") CONNECTION TO TRANSPORT DOES NOT EXIST OR FAIL WILL REOPEN : " + sendFromAddress + " password: " + password);
}
else
{
System.out.println("Transport(" + identifier + ") OK --> RETURNING Transport");
return transport;
}
 
 
 
 
// Set up properties for mail session
//Properties props = System.getProperties();
Properties props = new Properties();
props.put("mail.smtp.host", Globals.EMAIL_SERVER_HOST);
props.setProperty("mail.transport.protocol", "smtp");//todo new
javax.mail.Authenticator authenticator = null;
if(Globals.SYSTEM_EMAIL_USE_SECURITY && Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("TLS")==0)
{
logger.info("Transport(" + identifier + ") Using Email Security TLS");
 
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", ""+ Globals.EMAIL_SERVER_SECURITY_TLS);
authenticator = new javax.mail.Authenticator() {
 
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Email from:" + sendFromAddress + " pass:" + password);
return new PasswordAuthentication(sendFromAddress, password);
}
};
}
else if(Globals.SYSTEM_EMAIL_USE_SECURITY && Globals.EMAIL_SERVER_SECURITY != null && Globals.EMAIL_SERVER_SECURITY.compareTo("SSL")==0)
{
logger.info("Transport(" + identifier + ") Using Email Security SSL");
 
props.put("mail.smtp.socketFactory.port", Globals.EMAIL_SERVER_SECURITY_SSL);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", Globals.EMAIL_SERVER_SECURITY_SSL);
authenticator = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Email from:" + sendFromAddress + " pass:" + password);
 
return new PasswordAuthentication(sendFromAddress,password);
}
};
}
else
{
 
}
 
 
 
System.out.println("Transport(" + identifier + ") Autenticator != null :" + (authenticator != null));
// Get session
/*Session session = authenticator != null ?
Session.getDefaultInstance(props, authenticator)
: Session.getDefaultInstance(props) ;
*/
 
session = authenticator != null ?
Session.getInstance(props, authenticator)
: Session.getInstance(props) ;
 
try {
transport = session.getTransport("smtp");
System.out.println("Transport(" + identifier + ") before connect isConnected:" + transport.isConnected());
transport.connect();
System.out.println("Transport(" + identifier + ") after connect isConnected:" + transport.isConnected());
} catch (MessagingException e) {
logger.error(e,e);
return null;
}
 
session.setDebug(true);
session.setDebugOut(System.out);
return transport;
}
 
public String getIdentifier() {
return identifier;
}
 
public Session getSession() {
return session;
}
 
public Transport getTransport() {
return transport;
}
 
public String getSendFromAddress() {
return sendFromAddress;
}
}
/branches/v3/impl/src/java/pt/estgp/estgweb/services/email/impl/EmailTransportPool.java
New file
0,0 → 1,88
package pt.estgp.estgweb.services.email.impl;
 
import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
 
import javax.mail.Transport;
import java.util.ArrayList;
import java.util.List;
 
/**
* Created by jorgemachado on 13/06/16.
*/
public class EmailTransportPool
{
private static final Logger logger = Logger.getLogger(EmailTransportPool.class);
 
private List<EmailTransport> pool = new ArrayList<EmailTransport>();
 
//SINGLETON INSTANCE
static EmailTransportPool instance = new EmailTransportPool();
 
public static EmailTransportPool getInstance(){
return instance;
}
 
String[] serverEmails;
 
private EmailTransportPool()
{
serverEmails = new String[Globals.EMAIL_SERVER_USER_COUNT];
for(int i = 1;i<= Globals.EMAIL_SERVER_USER_COUNT;i++)
{
serverEmails[i-1] = Globals.EMAIL_SERVER_USER_PREFIX + i + Globals.EMAIL_SERVER_USER_SUFIX;
}
 
if(Globals.SYSTEM_EMAIL_USE_ROUND_ROBIN)
{
for(String emailFrom: serverEmails)
{
String pass = Globals.EMAIL_SERVER_PASSWORD;
logger.info("POOL: Adding to POOL " + emailFrom + " with pass: " + pass);
createTransport(emailFrom,emailFrom,pass);
}
}
else
{
logger.info("POOL: Will use only system mail box");
createTransport(Globals.SYSTEM_EMAIL_BOX,Globals.SYSTEM_EMAIL_BOX,Globals.EMAIL_SERVER_PASSWORD);
}
}
 
private void createTransport(String identifier, String emailFrom, String emailServerPassword)
{
EmailTransport emailTransport = new EmailTransport(identifier,emailFrom,emailServerPassword);
pool.add(emailTransport);
}
 
int indexPool = -1;
 
public EmailTransport getAvailableTransportFromPool()
{
int tries = pool.size();
while(tries > 0)
{
tries--;
 
indexPool++;
 
if(indexPool >= pool.size())
indexPool = 0;
 
EmailTransport emailTransport = pool.get(indexPool);
indexPool++;
Transport transport = emailTransport.getEmailTransport();
if(transport.isConnected())
{
return emailTransport;
}
else
{
logger.warn("EMAIL POOL: Transport (" + emailTransport.getIdentifier() + ") is not connected, will try another");
}
}
logger.warn("EMAIL POOL: Transport (There are no available transports)");
return null;
}
 
}
/branches/v3/impl/src/java/pt/estgp/estgweb/services/email/SendEmailService.java
5,6 → 5,8
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.enums.LangEnum;
import pt.estgp.estgweb.services.email.impl.EmailTransport;
import pt.estgp.estgweb.services.email.impl.EmailTransportPool;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.utils.ConfigProperties;
import pt.estgp.estgweb.utils.Email;
12,12 → 14,15
import pt.estgp.estgweb.web.utils.DatesUtils;
import pt.utl.ist.berserk.logic.serviceManager.IService;
 
import javax.mail.*;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
* @author Jorge Machado
31,14 → 36,14
 
private static final String gradeUnitTemplate = Globals.TEMPLATE_EMAIL_PATH + "/" + Globals.EMAIL_GRADE_UNIT_TEMPLATE;
 
static String sendFromAddress;
/*static String sendFromAddress;*/
 
public SendEmailService() {
 
 
}
 
static String[] serverEmails;
/*static String[] serverEmails;
static int serverEmailPos = -1;
static Session session = null;
static Transport transport = null;
62,13 → 67,17
sendFromAddress = Globals.SYSTEM_EMAIL_USE_ROUND_ROBIN ?
getRoundRobinEmailUser() : Globals.SYSTEM_EMAIL_BOX;
 
logger.info("CONNECTION SESSION EMAIL: " + sendFromAddress + " using round robin: " + Globals.SYSTEM_EMAIL_USE_ROUND_ROBIN);
logger.info("CONNECTION TO TRANSPORT FAIL WILL REOPEN : " + sendFromAddress + " using round robin: " + Globals.SYSTEM_EMAIL_USE_ROUND_ROBIN);
}
else
{
System.out.println("TRANSPORT OK RETURNING");
return transport;
}
 
 
 
 
// Set up properties for mail session
//Properties props = System.getProperties();
Properties props = new Properties();
118,11 → 127,11
 
System.out.println("Autenticator != null :" + (authenticator != null));
// Get session
/*Session session = authenticator != null ?
Session.getDefaultInstance(props, authenticator)
: Session.getDefaultInstance(props) ;
*/
//Session session = authenticator != null ?
// Session.getDefaultInstance(props, authenticator)
// : Session.getDefaultInstance(props) ;
 
 
session = authenticator != null ?
Session.getInstance(props, authenticator)
: Session.getInstance(props) ;
153,8 → 162,8
}
return serverEmails[serverEmailPos];
}
*/
 
 
public void sendEmail(Email email) throws ServiceException
{
sendEmail(email,null,null);
169,10 → 178,15
}
try{
 
Transport transport = getEmailTransport();
//Transport transport = getEmailTransport();
//MimeMessage message = new MimeMessage(session);
 
// Create message
MimeMessage message = new MimeMessage(session);
EmailTransport emailTransport = EmailTransportPool.getInstance().getAvailableTransportFromPool();
MimeMessage message = new MimeMessage(emailTransport.getSession());
 
 
 
 
// Set the recipients of the message
boolean valid = false;
for (String recipient : email.getRecipients())
211,10 → 225,10
 
 
 
logger.info("FROM:" + email.getFrom() + " but using: " + sendFromAddress);
logger.info("FROM:" + email.getFrom() + " but using: " + emailTransport.getSendFromAddress());
//if(email.getFromName() != null)
try {
message.setFrom(new InternetAddress(sendFromAddress,sendFromAddress));
message.setFrom(new InternetAddress(emailTransport.getSendFromAddress(),emailTransport.getSendFromAddress()));
} catch (UnsupportedEncodingException e) {
logger.error(e,e);
//message.setFrom(new InternetAddress(sendFromAddress));
252,7 → 266,7
 
 
 
transport.send(message);
emailTransport.getTransport().send(message);
//session.getTransport("smtp").sendMessage(message,message.getAllRecipients());
//session.getTransport().close();
}