mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-18 07:23:08 +00:00
Copy service code from abandoned DS-1182 attempt.
This commit is contained in:
@@ -47,6 +47,10 @@
|
|||||||
<artifactId>servlet-api</artifactId>
|
<artifactId>servlet-api</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.mail</groupId>
|
||||||
|
<artifactId>mail</artifactId>
|
||||||
|
</dependency>
|
||||||
<!-- SPECIAL CASE - need JUNIT at build time and testing time -->
|
<!-- SPECIAL CASE - need JUNIT at build time and testing time -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
@@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dspace.services;
|
||||||
|
|
||||||
|
import javax.mail.Session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author mwood
|
||||||
|
*/
|
||||||
|
public interface EmailService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Provide a reference to the JavaMail session.
|
||||||
|
*/
|
||||||
|
Session getSession();
|
||||||
|
}
|
@@ -0,0 +1,137 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
package org.dspace.services.email;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import javax.mail.Authenticator;
|
||||||
|
import javax.mail.PasswordAuthentication;
|
||||||
|
import javax.mail.Session;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.dspace.services.EmailService;
|
||||||
|
import org.dspace.utils.DSpace;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides mail sending services through JavaMail. If a {@link javax.mail.Session}
|
||||||
|
* instance is provided through JNDI, it will be used. If not, then a session
|
||||||
|
* will be created from DSpace configuration data ({@code mail.server} etc.)
|
||||||
|
*
|
||||||
|
* @author mwood
|
||||||
|
*/
|
||||||
|
public class EmailServiceImpl
|
||||||
|
extends Authenticator
|
||||||
|
implements EmailService
|
||||||
|
{
|
||||||
|
private Session session = null;
|
||||||
|
|
||||||
|
/** Lazy initialized since the service manager won't be running yet. */
|
||||||
|
private static ConfigurationService cfg = null;
|
||||||
|
|
||||||
|
private static final Logger logger = (Logger) LoggerFactory.getLogger(EmailServiceImpl.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide a reference to the JavaMail session.
|
||||||
|
*
|
||||||
|
* @return the managed Session, or null if none could be created.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session getSession()
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init()
|
||||||
|
{
|
||||||
|
if (null != session)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null == cfg)
|
||||||
|
{
|
||||||
|
cfg = new DSpace().getConfigurationService();
|
||||||
|
}
|
||||||
|
|
||||||
|
// See if there is already a Session in our environment
|
||||||
|
String sessionName = cfg.getProperty("mail.session.name");
|
||||||
|
if (null == sessionName)
|
||||||
|
{
|
||||||
|
sessionName = "Session";
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
InitialContext ctx = new InitialContext(null);
|
||||||
|
session = (Session) ctx.lookup("java:comp/env/mail/" + sessionName);
|
||||||
|
} catch (NamingException ex)
|
||||||
|
{
|
||||||
|
logger.warn("Couldn't get an email session from environment: {}",
|
||||||
|
ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null != session)
|
||||||
|
{
|
||||||
|
logger.info("Email session retrieved from environment.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // No Session provided, so create one
|
||||||
|
logger.info("Initializing an email session from configuration.");
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("mail.transport.protocol", "smtp");
|
||||||
|
String host = cfg.getProperty("mail.server");
|
||||||
|
if (null != host)
|
||||||
|
{
|
||||||
|
props.put("mail.host", cfg.getProperty("mail.server"));
|
||||||
|
}
|
||||||
|
String port = cfg.getProperty("mail.server.port");
|
||||||
|
if (null != port)
|
||||||
|
{
|
||||||
|
props.put("mail.smtp.port", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null == cfg.getProperty("mail.server.username"))
|
||||||
|
{
|
||||||
|
session = Session.getInstance(props);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
session = Session.getInstance(null, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set extra configuration properties
|
||||||
|
String extras = cfg.getProperty("mail.extraproperties");
|
||||||
|
if ((extras != null) && (!"".equals(extras.trim())))
|
||||||
|
{
|
||||||
|
String arguments[] = extras.split(",");
|
||||||
|
String key, value;
|
||||||
|
for (String argument : arguments)
|
||||||
|
{
|
||||||
|
key = argument.substring(0, argument.indexOf('=')).trim();
|
||||||
|
value = argument.substring(argument.indexOf('=') + 1).trim();
|
||||||
|
props.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication()
|
||||||
|
{
|
||||||
|
if (null == cfg)
|
||||||
|
{
|
||||||
|
cfg = new DSpace().getConfigurationService();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PasswordAuthentication(
|
||||||
|
cfg.getProperty("mail.server.username"),
|
||||||
|
cfg.getProperty("mail.server.password"));
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages a JavaMail session for code which wants to send email.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dspace.services.email;
|
@@ -0,0 +1,102 @@
|
|||||||
|
/**
|
||||||
|
* The contents of this file are subject to the license and copyright
|
||||||
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||||
|
* tree and available online at
|
||||||
|
*
|
||||||
|
* http://www.dspace.org/license/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dspace.services.email;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.PasswordAuthentication;
|
||||||
|
import javax.mail.Session;
|
||||||
|
import org.dspace.services.ConfigurationService;
|
||||||
|
import org.dspace.services.EmailService;
|
||||||
|
import org.dspace.test.DSpaceAbstractKernelTest;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author mwood
|
||||||
|
*/
|
||||||
|
public class EmailServiceImplTest
|
||||||
|
extends DSpaceAbstractKernelTest
|
||||||
|
{
|
||||||
|
private static final String USERNAME = "auser";
|
||||||
|
private static final String PASSWORD = "apassword";
|
||||||
|
|
||||||
|
/*
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUpClass()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void tearDownClass()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test of getSession method, of class EmailService.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetSession()
|
||||||
|
throws MessagingException
|
||||||
|
{
|
||||||
|
System.out.println("getSession");
|
||||||
|
Session session;
|
||||||
|
EmailService instance = getService(EmailService.class);
|
||||||
|
|
||||||
|
// Try to get a Session
|
||||||
|
session = instance.getSession();
|
||||||
|
assertNotNull(" getSession returned null", session);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String CFG_USERNAME = "mail.server.username";
|
||||||
|
private static final String CFG_PASSWORD = "mail.server.password";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test of getPasswordAuthentication method, of class EmailServiceImpl.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testGetPasswordAuthentication()
|
||||||
|
{
|
||||||
|
System.out.println("getPasswordAuthentication");
|
||||||
|
ConfigurationService cfg = getKernel().getConfigurationService();
|
||||||
|
|
||||||
|
// Save existing values.
|
||||||
|
String oldUsername = cfg.getProperty(CFG_USERNAME);
|
||||||
|
String oldPassword = cfg.getProperty(CFG_PASSWORD);
|
||||||
|
|
||||||
|
// Set known values.
|
||||||
|
cfg.setProperty(CFG_USERNAME, USERNAME);
|
||||||
|
cfg.setProperty(CFG_PASSWORD, PASSWORD);
|
||||||
|
|
||||||
|
EmailServiceImpl instance = (EmailServiceImpl) getService(EmailService.class);
|
||||||
|
|
||||||
|
PasswordAuthentication result = instance.getPasswordAuthentication();
|
||||||
|
assertNotNull(" null returned", result);
|
||||||
|
assertEquals(" username does not match configuration", result.getUserName(), USERNAME);
|
||||||
|
assertEquals(" password does not match configuration", result.getPassword(), PASSWORD);
|
||||||
|
|
||||||
|
// Restore old values, if any.
|
||||||
|
cfg.setProperty(CFG_USERNAME, oldUsername);
|
||||||
|
cfg.setProperty(CFG_PASSWORD, oldPassword);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user