/*
* 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.Map;
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.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.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 boolean emailInboundEnabled;
/** Login of user that is set as unknown. */
private String unknownUser;
/** List of message handlers */
private Map emailMessageHandlerMap;
/**
*
* @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(EmailMessage message)
{
processMessage(null, message);
}
/**
* {@inheritDoc}
*/
public void importMessage(NodeRef nodeRef, EmailMessage message)
{
processMessage(nodeRef, message);
}
/**
* Process the message. Method is called after filtering by sender's address.
*
* @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 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 from = message.getFrom();
return getUsername(from);
}
};
RunAsWork getUsernameRunAsWork = new RunAsWork()
{
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