/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.email.server;
import java.util.Collection;
import java.util.Map;
import javax.mail.internet.InternetAddress;
import org.alfresco.email.server.handler.EmailMessageHandler;
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.attributes.AttributeService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.email.EmailDelivery;
import org.alfresco.service.cmr.email.EmailMessage;
import org.alfresco.service.cmr.email.EmailMessageException;
import org.alfresco.service.cmr.email.EmailService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
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.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.surf.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 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_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 AuthorityService authorityService;
private DictionaryService dictionaryService;
private AttributeService attributeService;
/**
* The authority that needs to contain the users and groups
* who are allowed to contribute email.
*/
private String emailContributorsAuthority="EMAIL_CONTRIBUTORS";
private static Log logger = LogFactory.getLog(EmailServiceImpl.class);
private boolean emailInboundEnabled;
/** Login of user that is set as unknown. */
private String unknownUser;
/** List of message handlers */
private Map emailMessageHandlerMap;
public void init()
{
PropertyCheck.mandatory(this, "namespaceService", namespaceService);
PropertyCheck.mandatory(this, "dictionaryService", getDictionaryService());
PropertyCheck.mandatory(this, "searchService", searchService);
PropertyCheck.mandatory(this, "authorityService", authorityService);
PropertyCheck.mandatory(this, "emailMessageHandlerMap", emailMessageHandlerMap);
PropertyCheck.mandatory(this, "attributeService", getAttributeService());
}
/**
*
* @param namespaceService the service to resolve namespace prefixes
*/
public void setNamespaceService(NamespaceService namespaceService)
{
this.namespaceService = namespaceService;
}
/**
* @param nodeService Alfresco Node Service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* @param searchService Alfresco Search Service
*/
public void setSearchService(SearchService searchService)
{
this.searchService = searchService;
}
/**
* @param retryingTransactionHelper Alfresco RetryingTransactionHelper
*/
public void setRetryingTransactionHelper(RetryingTransactionHelper retryingTransactionHelper)
{
this.retryingTransactionHelper = retryingTransactionHelper;
}
/**
* @param authorityService Alfresco authority service
*/
public void setAuthorityService(AuthorityService authorityService)
{
this.authorityService = authorityService;
}
/**
* @return Map of message handlers
*/
public Map getEmailMessageHandlerMap()
{
return emailMessageHandlerMap;
}
/**
* @param emailMessageHandlerMap Map of message handlers
*/
public void setEmailMessageHandlerMap(Map emailMessageHandlerMap)
{
this.emailMessageHandlerMap = emailMessageHandlerMap;
}
/**
* @param unknownUser Login of user that should be set as unknown.
*/
public void setUnknownUser(String unknownUser)
{
this.unknownUser = unknownUser;
}
public void setEmailInboundEnabled(boolean mailInboundEnabled)
{
this.emailInboundEnabled = mailInboundEnabled;
}
/**
* {@inheritDoc}
*/
public void importMessage(EmailDelivery delivery, EmailMessage message)
{
processMessage(delivery, null, message);
}
/**
* {@inheritDoc}
*/
public void importMessage(EmailDelivery delivery, NodeRef nodeRef, EmailMessage message)
{
processMessage(delivery, nodeRef, message);
}
/**
* Process the message. Method is called after filtering by sender's address.
* @param delivery - who gets the message and who is it from (may be different from the contents of the message)
* @param nodeRef Addressed node (target node).
* @param message Email message
* @throws EmailMessageException Any exception occured inside the method will be converted and thrown as EmailMessageException
*/
private void processMessage(final EmailDelivery delivery, final NodeRef nodeRef, final EmailMessage message)
{
if (!emailInboundEnabled)
{
throw new EmailMessageException(ERR_INBOUND_EMAIL_DISABLED);
}
try
{
// Get the username for the process using the system account
final RetryingTransactionCallback getUsernameCallback = new RetryingTransactionCallback()
{
public String execute() throws Throwable
{
String userName = null;
userName = getUsername(delivery.getFrom());
if(userName == null)
{
if(logger.isDebugEnabled())
{
logger.debug("unable to find user for from: " + delivery.getFrom() + ",trying message.from next");
}
userName = getUsername(message.getFrom());
}
if(logger.isDebugEnabled())
{
logger.debug("userName = : " + userName);
}
if (userName == null)
{
if(unknownUser.isEmpty())
{
if(logger.isDebugEnabled())
{
logger.debug("unable to find user for from: " + message.getFrom());
}
throw new EmailMessageException(ERR_UNKNOWN_SOURCE_ADDRESS, message.getFrom());
}
else
{
if(logger.isDebugEnabled())
{
logger.debug("unable to find user for from - return anonymous: ");
}
userName = unknownUser;
}
}
// 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;
}
};
RunAsWork getUsernameRunAsWork = new RunAsWork()
{
public String doWork() throws Exception
{
return retryingTransactionHelper.doInTransaction(getUsernameCallback, false);
}
};
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