mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Email Server and EmailService in a working state.
- Fixed authentication - Fixed I18N - Fixed mimetype and encoding issues Changed 'avm' remote ports and hosts to be 'alfresco.rmi.services' as the 'avm' was starting to creep into non-avm stuff. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@7281 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -28,10 +28,12 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.email.server.handler.EmailMessageHandler;
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.node.integrity.IntegrityException;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
||||
import org.alfresco.repo.security.permissions.AccessDeniedException;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
@@ -43,28 +45,47 @@ import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
/**
|
||||
* Concrete email service implementation. This is responsible for routing the
|
||||
* emails into the server.
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public class EmailServiceImpl implements EmailService
|
||||
{
|
||||
private static final Log log = LogFactory.getLog(EmailServiceImpl.class);
|
||||
|
||||
private static final String ERR_INBOUND_EMAIL_DISABLED = "email.server.err.inbound_mail_disabled";
|
||||
private static final String ERR_INVALID_SUBJECT = "email.server.err.invalid_subject";
|
||||
private static final String ERR_ACCESS_DENIED = "email.server.err.access_denied";
|
||||
private static final String ERR_UNKNOWN_SOURCE_ADDRESS = "email.server.err.unknown_source_address";
|
||||
private static final String ERR_USER_NOT_EMAIL_CONTRIBUTOR = "email.server.err.user_not_email_contributor";
|
||||
private static final String ERR_NO_EMAIL_CONTRIBUTOR_GROUP = "email.server.err.no_email_contributor_group";
|
||||
private static final String ERR_INVALID_NODE_ADDRESS = "email.server.err.invalid_node_address";
|
||||
private static final String ERR_HANDLER_NOT_FOUND = "email.server.err.handler_not_found";
|
||||
|
||||
private NamespaceService namespaceService;
|
||||
private NodeService nodeService;
|
||||
private SearchService searchService;
|
||||
private RetryingTransactionHelper retryingTransactionHelper;
|
||||
|
||||
private boolean mailInboundEnabled;
|
||||
private boolean emailInboundEnabled;
|
||||
/** Login of user that is set as unknown. */
|
||||
private String unknownUser;
|
||||
/** List of message handlers */
|
||||
private Map<String, EmailMessageHandler> emailMessageHandlerMap;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param namespaceService the service to resolve namespace prefixes
|
||||
*/
|
||||
public void setNamespaceService(NamespaceService namespaceService)
|
||||
{
|
||||
this.namespaceService = namespaceService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nodeService Alfresco Node Service
|
||||
*/
|
||||
@@ -113,9 +134,9 @@ public class EmailServiceImpl implements EmailService
|
||||
this.unknownUser = unknownUser;
|
||||
}
|
||||
|
||||
public void setMailInboundEnabled(boolean mailInboundEnabled)
|
||||
public void setEmailInboundEnabled(boolean mailInboundEnabled)
|
||||
{
|
||||
this.mailInboundEnabled = mailInboundEnabled;
|
||||
this.emailInboundEnabled = mailInboundEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,87 +164,114 @@ public class EmailServiceImpl implements EmailService
|
||||
*/
|
||||
private void processMessage(final NodeRef nodeRef, final EmailMessage message)
|
||||
{
|
||||
if (!mailInboundEnabled)
|
||||
if (!emailInboundEnabled)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.mail-inbound-disabled"));
|
||||
throw new EmailMessageException(ERR_INBOUND_EMAIL_DISABLED);
|
||||
}
|
||||
try
|
||||
{
|
||||
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
|
||||
// Get the username for the process using the system account
|
||||
final RetryingTransactionCallback<String> getUsernameCallback = new RetryingTransactionCallback<String>()
|
||||
{
|
||||
public Object execute()
|
||||
public String execute() throws Throwable
|
||||
{
|
||||
final String userName = authenticate(message.getFrom());
|
||||
|
||||
AuthenticationUtil.runAs(new RunAsWork<Object>()
|
||||
String from = message.getFrom();
|
||||
return getUsername(from);
|
||||
}
|
||||
};
|
||||
RunAsWork<String> getUsernameRunAsWork = new RunAsWork<String>()
|
||||
{
|
||||
public String doWork() throws Exception
|
||||
{
|
||||
return retryingTransactionHelper.doInTransaction(getUsernameCallback, false);
|
||||
}
|
||||
};
|
||||
String username = AuthenticationUtil.runAs(getUsernameRunAsWork, AuthenticationUtil.SYSTEM_USER_NAME);
|
||||
|
||||
// Process the message using the username's account
|
||||
final RetryingTransactionCallback<Object> processMessageCallback = new RetryingTransactionCallback<Object>()
|
||||
{
|
||||
public Object execute() throws Throwable
|
||||
{
|
||||
String recipient = message.getTo();
|
||||
NodeRef targetNodeRef = null;
|
||||
if (nodeRef == null)
|
||||
{
|
||||
public Object doWork() throws Exception
|
||||
{
|
||||
String recepient = message.getTo();
|
||||
NodeRef targetNodeRef = null;
|
||||
if (nodeRef == null)
|
||||
{
|
||||
targetNodeRef = getTargetNode(recepient);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetNodeRef = nodeRef;
|
||||
}
|
||||
EmailMessageHandler messageHandler = getMessageHandler(targetNodeRef);
|
||||
messageHandler.processMessage(targetNodeRef, message);
|
||||
return null;
|
||||
}
|
||||
|
||||
}, userName);
|
||||
targetNodeRef = getTargetNode(recipient);
|
||||
}
|
||||
else
|
||||
{
|
||||
targetNodeRef = nodeRef;
|
||||
}
|
||||
EmailMessageHandler messageHandler = getMessageHandler(targetNodeRef);
|
||||
messageHandler.processMessage(targetNodeRef, message);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
retryingTransactionHelper.doInTransaction(callback, false);
|
||||
RunAsWork<Object> processMessageRunAsWork = new RunAsWork<Object>()
|
||||
{
|
||||
public Object doWork() throws Exception
|
||||
{
|
||||
return retryingTransactionHelper.doInTransaction(processMessageCallback, false);
|
||||
}
|
||||
};
|
||||
AuthenticationUtil.runAs(processMessageRunAsWork, username);
|
||||
}
|
||||
catch (EmailMessageException e)
|
||||
{
|
||||
// These are email-specific errors
|
||||
throw e;
|
||||
}
|
||||
catch (AccessDeniedException e)
|
||||
{
|
||||
throw new EmailMessageException(ERR_ACCESS_DENIED, message.getFrom(), message.getTo());
|
||||
}
|
||||
catch (IntegrityException e)
|
||||
{
|
||||
throw new EmailMessageException(ERR_INVALID_SUBJECT);
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
log.error("Error process email message", e);
|
||||
throw new EmailMessageException(e.getMessage());
|
||||
throw new AlfrescoRuntimeException("Email message processing failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nodeRef Target node
|
||||
* @return Handler that can process message addressed to specific node (target node).
|
||||
* @throws EmailMessageException Exception is thrown if <code>nodeRef</code> is <code>null</code> or suitable message handler isn't found.
|
||||
* @param nodeRef Target node
|
||||
* @return Handler that can process message addressed to specific node (target node).
|
||||
* @throws EmailMessageException is thrown if a suitable message handler isn't found.
|
||||
*/
|
||||
private EmailMessageHandler getMessageHandler(NodeRef nodeRef)
|
||||
{
|
||||
if (nodeRef == null)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-ref"));
|
||||
}
|
||||
String nodeTypeLocalName = nodeService.getType(nodeRef).getLocalName();
|
||||
EmailMessageHandler handler = emailMessageHandlerMap.get(nodeTypeLocalName);
|
||||
ParameterCheck.mandatory("nodeRef", nodeRef);
|
||||
|
||||
QName nodeTypeQName = nodeService.getType(nodeRef);
|
||||
String prefixedNodeTypeStr = nodeTypeQName.toPrefixString(namespaceService);
|
||||
EmailMessageHandler handler = emailMessageHandlerMap.get(prefixedNodeTypeStr);
|
||||
if (handler == null)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.handler-not-found"));
|
||||
throw new EmailMessageException(ERR_HANDLER_NOT_FOUND, prefixedNodeTypeStr);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method determines target node by recepient e-mail address.
|
||||
* Method determines target node by recipient e-mail address.
|
||||
*
|
||||
* @param recepient An e-mail address of a receipient
|
||||
* @return Referance to the target node.
|
||||
* @exception EmailMessageException Exception is thrown if the target node couldn't be determined by some reasons.
|
||||
* @param recipient An e-mail address of a recipient
|
||||
* @return Reference to the target node
|
||||
* @throws EmailMessageException is thrown if the target node couldn't be determined by some reasons.
|
||||
*/
|
||||
private NodeRef getTargetNode(String recepient)
|
||||
private NodeRef getTargetNode(String recipient)
|
||||
{
|
||||
if (recepient == null || recepient.length() == 0)
|
||||
if (recipient == null || recipient.length() == 0)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-address"));
|
||||
throw new EmailMessageException(ERR_INVALID_NODE_ADDRESS, recipient);
|
||||
}
|
||||
String[] parts = recepient.split("@");
|
||||
String[] parts = recipient.split("@");
|
||||
if (parts.length != 2)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-address"));
|
||||
throw new EmailMessageException(ERR_INVALID_NODE_ADDRESS, recipient);
|
||||
}
|
||||
|
||||
// Ok, address looks well, let's try to find related alias
|
||||
@@ -243,14 +291,14 @@ public class EmailServiceImpl implements EmailService
|
||||
}
|
||||
}
|
||||
|
||||
// Ok, alias wasn't found, let's try to interpret recepient address as 'node-bdid' value
|
||||
// Ok, alias wasn't found, let's try to interpret recipient address as 'node-bdid' value
|
||||
query = "@sys\\:node-dbid:" + parts[0];
|
||||
resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, query);
|
||||
if (resultSet.length() > 0)
|
||||
{
|
||||
return resultSet.getNodeRef(0);
|
||||
}
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-address"));
|
||||
throw new EmailMessageException(ERR_INVALID_NODE_ADDRESS, recipient);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,7 +308,7 @@ public class EmailServiceImpl implements EmailService
|
||||
* @return User name
|
||||
* @throws EmailMessageException Exception will be thrown if authentication is failed.
|
||||
*/
|
||||
private String authenticate(String from)
|
||||
private String getUsername(String from)
|
||||
{
|
||||
String userName = null;
|
||||
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||
@@ -270,10 +318,13 @@ public class EmailServiceImpl implements EmailService
|
||||
|
||||
if (resultSet.length() == 0)
|
||||
{
|
||||
userName = unknownUser;
|
||||
if (userName == null || !isEmailContributeUser(userName))
|
||||
if (unknownUser == null || unknownUser.length() == 0)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.unknown-user"));
|
||||
throw new EmailMessageException(ERR_UNKNOWN_SOURCE_ADDRESS, from);
|
||||
}
|
||||
else
|
||||
{
|
||||
userName = unknownUser;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -281,17 +332,21 @@ public class EmailServiceImpl implements EmailService
|
||||
NodeRef userNode = resultSet.getNodeRef(0);
|
||||
if (nodeService.exists(userNode))
|
||||
{
|
||||
userName = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(userNode, ContentModel.PROP_USERNAME));
|
||||
if (!isEmailContributeUser(userName))
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.not-contribute-user", userName));
|
||||
}
|
||||
userName = DefaultTypeConverter.INSTANCE.convert(
|
||||
String.class,
|
||||
nodeService.getProperty(userNode, ContentModel.PROP_USERNAME));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.contribute-group-not-exist"));
|
||||
// The Lucene index returned a dead result
|
||||
throw new EmailMessageException(ERR_UNKNOWN_SOURCE_ADDRESS, from);
|
||||
}
|
||||
}
|
||||
// Ensure that the user is part of the Email Contributors group
|
||||
if (userName == null || !isEmailContributeUser(userName))
|
||||
{
|
||||
throw new EmailMessageException(ERR_USER_NOT_EMAIL_CONTRIBUTOR, userName);
|
||||
}
|
||||
return userName;
|
||||
}
|
||||
|
||||
@@ -310,16 +365,22 @@ public class EmailServiceImpl implements EmailService
|
||||
|
||||
if (resultSet.length() == 0)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.contribute-group-not-exist"));
|
||||
throw new EmailMessageException(ERR_NO_EMAIL_CONTRIBUTOR_GROUP);
|
||||
}
|
||||
|
||||
NodeRef groupNode = resultSet.getNodeRef(0);
|
||||
|
||||
Collection<String> memberCollection = DefaultTypeConverter.INSTANCE.getCollection(String.class, nodeService.getProperty(groupNode, ContentModel.PROP_MEMBERS));
|
||||
Collection<String> memberCollection = DefaultTypeConverter.INSTANCE.getCollection(
|
||||
String.class,
|
||||
nodeService.getProperty(groupNode, ContentModel.PROP_MEMBERS));
|
||||
|
||||
if (memberCollection.contains(userName))
|
||||
{
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user