ALF-11837 - Alfresco 4.0 SMTP Inbound does not work with messages without From and To Headers.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@33015 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Mark Rogers
2012-01-03 11:45:05 +00:00
parent a56372d469
commit 183774a2ea
7 changed files with 124 additions and 40 deletions

View File

@@ -24,6 +24,8 @@ import java.util.Set;
import java.util.StringTokenizer;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.email.EmailMessageException;
import org.alfresco.service.cmr.email.EmailService;
import org.springframework.extensions.surf.util.AbstractLifecycleBean;
@@ -50,9 +52,10 @@ public abstract class EmailServer extends AbstractLifecycleBean
private boolean hideTLS = false;
private boolean enableTLS = true;
private boolean requireTLS = false;
private boolean authenticate = false;
private EmailService emailService;
private AuthenticationComponent authenticationComponent;
/**
* @param serverConfiguration Server configuration
@@ -328,6 +331,19 @@ public abstract class EmailServer extends AbstractLifecycleBean
System.err.println("Use: EmailServer configLocation1, configLocation2, ...");
System.err.println("\t configLocation - spring xml configs with EmailServer related beans (emailServer, emailServerConfiguration, emailService)");
}
protected boolean authenticateUserNamePassword(String userName, char[] password)
{
try
{
getAuthenticationComponent().authenticate(userName, password);
return true;
}
catch (AuthenticationException e)
{
return false;
}
}
/** Hide the TLS (Trusted Login Session) option
*
@@ -363,4 +379,24 @@ public abstract class EmailServer extends AbstractLifecycleBean
return requireTLS;
}
public void setAuthenticate(boolean enableAuthentication)
{
this.authenticate = enableAuthentication;
}
public boolean isAuthenticate()
{
return authenticate;
}
public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
{
this.authenticationComponent = authenticationComponent;
}
public AuthenticationComponent getAuthenticationComponent()
{
return authenticationComponent;
}
}

View File

@@ -187,7 +187,7 @@ public class EmailServiceImpl implements EmailService
// Get the username for the process using the system account
final RetryingTransactionCallback<String> getUsernameCallback = new RetryingTransactionCallback<String>()
{
public String execute() throws Throwable
{
String userName = null;
@@ -229,8 +229,6 @@ public class EmailServiceImpl implements EmailService
}
return userName;
}
};
RunAsWork<String> getUsernameRunAsWork = new RunAsWork<String>()
@@ -240,7 +238,20 @@ public class EmailServiceImpl implements EmailService
return retryingTransactionHelper.doInTransaction(getUsernameCallback, false);
}
};
String username = AuthenticationUtil.runAs(getUsernameRunAsWork, AuthenticationUtil.SYSTEM_USER_NAME);
String username;
if(delivery.getAuth() != null)
{
// The user has authenticated.
username = delivery.getAuth();
logger.debug("user has already authenticated as:" + username);
}
else
{
// Need to faff with old message stuff.
username = AuthenticationUtil.runAs(getUsernameRunAsWork, AuthenticationUtil.SYSTEM_USER_NAME);
}
// Process the message using the username's account
final RetryingTransactionCallback<Object> processMessageCallback = new RetryingTransactionCallback<Object>()
@@ -279,7 +290,7 @@ public class EmailServiceImpl implements EmailService
}
catch (AccessDeniedException e)
{
throw new EmailMessageException(ERR_ACCESS_DENIED, message.getFrom(), message.getTo());
throw new EmailMessageException(ERR_ACCESS_DENIED, delivery.getFrom(), delivery.getRecipient());
}
catch (IntegrityException e)
{
@@ -379,6 +390,12 @@ public class EmailServiceImpl implements EmailService
private String getUsername(String from)
{
String userName = null;
if(from == null)
{
return null;
}
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
String query = "TYPE:cm\\:person +@cm\\:email:\"" + from + "\"";

View File

@@ -107,12 +107,18 @@ public class FolderEmailMessageHandler extends AbstractEmailMessageHandler
Date now = new Date();
messageSubject = I18NUtil.getMessage(MSG_DEFAULT_SUBJECT, new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss").format(now));
}
String messageFrom = message.getFrom();
if(messageFrom == null)
{
messageFrom = "";
}
// Create main content node
NodeRef contentNodeRef;
contentNodeRef = addContentNode(getNodeService(), spaceNodeRef, messageSubject, overwriteDuplicates);
// Add titled aspect
addTitledAspect(contentNodeRef, messageSubject, message.getFrom());
addTitledAspect(contentNodeRef, messageSubject, messageFrom);
// Add emailed aspect
addEmailedAspect(contentNodeRef, message);
// Write the message content

View File

@@ -131,17 +131,19 @@ public class SubethaEmailMessage implements EmailMessage
}
if (addresses == null || addresses.length == 0)
{
throw new EmailMessageException(ERR_NO_FROM_ADDRESS);
}
if(addresses[0] instanceof InternetAddress)
{
from = ((InternetAddress)addresses[0]).getAddress();
//throw new EmailMessageException(ERR_NO_FROM_ADDRESS);
}
else
{
from = addresses[0].toString();
}
if(addresses[0] instanceof InternetAddress)
{
from = ((InternetAddress)addresses[0]).getAddress();
}
else
{
from = addresses[0].toString();
}
}
}
if (to == null)
@@ -157,18 +159,19 @@ public class SubethaEmailMessage implements EmailMessage
}
if (addresses == null || addresses.length == 0)
{
throw new EmailMessageException(ERR_NO_TO_ADDRESS);
}
if(addresses[0] instanceof InternetAddress)
{
to = ((InternetAddress)addresses[0]).getAddress();
//throw new EmailMessageException(ERR_NO_TO_ADDRESS);
}
else
{
to = addresses[0].toString();
}
to = addresses[0].toString();
if(addresses[0] instanceof InternetAddress)
{
to = ((InternetAddress)addresses[0]).getAddress();
}
else
{
to = addresses[0].toString();
}
}
}
if (cc == null)

View File

@@ -33,11 +33,17 @@ import org.alfresco.service.cmr.email.EmailMessage;
import org.alfresco.service.cmr.email.EmailMessageException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.subethamail.smtp.AuthenticationHandler;
import org.subethamail.smtp.AuthenticationHandlerFactory;
import org.subethamail.smtp.MessageContext;
import org.subethamail.smtp.MessageHandler;
import org.subethamail.smtp.MessageHandlerFactory;
import org.subethamail.smtp.RejectException;
import org.subethamail.smtp.TooMuchDataException;
import org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory;
import org.subethamail.smtp.auth.LoginFailedException;
import org.subethamail.smtp.auth.MultipleAuthenticationHandlerFactory;
import org.subethamail.smtp.auth.UsernamePasswordValidator;
import org.subethamail.smtp.server.SMTPServer;
/**
@@ -66,6 +72,12 @@ public class SubethaEmailServer extends EmailServer
serverImpl.setEnableTLS(isEnableTLS());
serverImpl.setRequireTLS(isRequireTLS());
if(isAuthenticate())
{
AuthenticationHandlerFactory authenticationHandler = new EasyAuthenticationHandlerFactory(new AlfrescoLoginUsernamePasswordValidator());
serverImpl.setAuthenticationHandlerFactory(authenticationHandler);
}
serverImpl.start();
log.info("Inbound SMTP Email Server has started successfully, on hostName:" + getDomain() + "port:" + getPort());
}
@@ -76,7 +88,20 @@ public class SubethaEmailServer extends EmailServer
serverImpl.stop();
log.info("Inbound SMTP Email Server has stopped successfully");
}
class AlfrescoLoginUsernamePasswordValidator implements UsernamePasswordValidator
{
@Override
public void login(String username, String password)
throws LoginFailedException
{
if(authenticateUserNamePassword(username, password.toCharArray()))
{
throw new LoginFailedException("unable to log on");
}
}
}
class HandlerFactory implements MessageHandlerFactory
{
public MessageHandler create(MessageContext messageContext)
@@ -139,7 +164,9 @@ public class SubethaEmailServer extends EmailServer
public void recipient(String recipient) throws RejectException
{
deliveries.add(new EmailDelivery(recipient, from, null));
AuthenticationHandler auth = messageContext.getAuthenticationHandler();
deliveries.add(new EmailDelivery(recipient, from, auth != null ? (String)auth.getIdentity(): null));
}
public void data(InputStream data) throws TooMuchDataException, IOException, RejectException
@@ -171,20 +198,6 @@ public class SubethaEmailServer extends EmailServer
}
}
// public List<String> getAuthenticationMechanisms()
// {
// return EMPTY_LIST;
// }
//
// public boolean auth(String clientInput, StringBuffer response) throws RejectException
// {
// return true;
// }
//
// public void resetState()
// {
// }
//
@Override
public void done()
{