mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
EmailServer code.
- This is not fully integrated. In fact, the integration points have been disabled. - The main code for now is the remote stream. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6756 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.email.server.EmailServerModel;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationComponent;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
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.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.sun.star.auth.InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Abstract class implements common logic for processing email messages.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public abstract class AbstractEmailMessageHandler implements EmailMessageHandler
|
||||
{
|
||||
private static final Log log = LogFactory.getLog(EmailMessageHandler.class);
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
private AuthenticationComponent authenticationComponent;
|
||||
private NodeService nodeService;
|
||||
private PersonService personService;
|
||||
private SearchService searchService;
|
||||
private ContentService contentService;
|
||||
|
||||
/**
|
||||
* @return Alfresco Content Service.
|
||||
*/
|
||||
public ContentService getContentService()
|
||||
{
|
||||
return contentService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param contentService Alfresco Content Service.
|
||||
*/
|
||||
public void setContentService(ContentService contentService)
|
||||
{
|
||||
this.contentService = contentService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Alfresco Authentication Component.
|
||||
*/
|
||||
public AuthenticationComponent getAuthenticationComponent()
|
||||
{
|
||||
return authenticationComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param authenticationComponent Alfresco Authentication Component.
|
||||
*/
|
||||
public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
|
||||
{
|
||||
this.authenticationComponent = authenticationComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Alfresco Authentication Service.
|
||||
*/
|
||||
public AuthenticationService getAuthenticationService()
|
||||
{
|
||||
return authenticationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param authenticationService Alfresco Authentication Service.
|
||||
*/
|
||||
public void setAuthenticationService(AuthenticationService authenticationService)
|
||||
{
|
||||
this.authenticationService = authenticationService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Alfresco Node Service.
|
||||
*/
|
||||
public NodeService getNodeService()
|
||||
{
|
||||
return nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param nodeService Alfresco Node Service.
|
||||
*/
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Alfesco Person Service.
|
||||
*/
|
||||
public PersonService getPersonService()
|
||||
{
|
||||
return personService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param personService Alfresco Person Service.
|
||||
*/
|
||||
public void setPersonService(PersonService personService)
|
||||
{
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Alfresco Search Service.
|
||||
*/
|
||||
public SearchService getSearchService()
|
||||
{
|
||||
return searchService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param searchService Alfresco Search Service.
|
||||
*/
|
||||
|
||||
public void setSearchService(SearchService searchService)
|
||||
{
|
||||
this.searchService = searchService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param to Email address which user part specifies node-dbid
|
||||
* @return Referance to requested node.
|
||||
* @throws InvalidArgumentException The exception is thrown if input string has incorrect format or empty.
|
||||
*/
|
||||
protected NodeRef getTargetNode(String to) throws InvalidArgumentException
|
||||
{
|
||||
if (to == null || to.length() == 0)
|
||||
{
|
||||
throw new InvalidArgumentException("Input string has to contain email address.");
|
||||
}
|
||||
String[] parts = to.split("@");
|
||||
if (parts.length != 2)
|
||||
{
|
||||
throw new InvalidArgumentException("Incorrect email address format.");
|
||||
}
|
||||
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
|
||||
String query = "@sys\\:node-dbid:" + parts[0];
|
||||
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, query);
|
||||
if (resultSet.length() == 1)
|
||||
{
|
||||
return resultSet.getNodeRef(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the content to the node
|
||||
*
|
||||
* @param nodeRef Target node
|
||||
* @param content Content
|
||||
*/
|
||||
protected void writeContent(NodeRef nodeRef, String content)
|
||||
{
|
||||
writeContent(nodeRef, content, MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the string as content to the node.
|
||||
*
|
||||
* @param nodeRef Target node.
|
||||
* @param content Text for writting.
|
||||
* @param contentType MIME content type. For exaple you can set this parameter to "text/html" or "text/xml", etc.
|
||||
*/
|
||||
protected void writeContent(NodeRef nodeRef, String content, String contentType)
|
||||
{
|
||||
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
|
||||
writeContent(nodeRef, inputStream, contentType, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Write content to the node from InputStream.
|
||||
*
|
||||
* @param nodeRef Target node.
|
||||
* @param content Content stream.
|
||||
* @param contentType MIME content type.
|
||||
* @param encoding Encoding. Can be null for non text based content.
|
||||
*/
|
||||
protected void writeContent(NodeRef nodeRef, InputStream content, String contentType, String encoding)
|
||||
{
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Write content (MimeType=\"" + contentType + "\", Encoding=\"" + encoding + "\"");
|
||||
}
|
||||
ContentService contentService = getContentService();
|
||||
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
|
||||
writer.setMimetype(contentType);
|
||||
writer.setEncoding(encoding);
|
||||
writer.putContent(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add emailed aspect to the specified node.
|
||||
*
|
||||
* @param nodeService Alfresco Node Service.
|
||||
* @param nodeRef Target node.
|
||||
* @param mailParser Mail message that will be used for extracting necessary information
|
||||
*/
|
||||
protected void addEmailedAspect(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
Map<QName, Serializable> emailProps = new HashMap<QName, Serializable>();
|
||||
emailProps.put(ContentModel.PROP_SENTDATE, message.getSentDate());
|
||||
emailProps.put(ContentModel.PROP_ORIGINATOR, message.getFrom());
|
||||
emailProps.put(ContentModel.PROP_ADDRESSEE, message.getTo());
|
||||
emailProps.put(ContentModel.PROP_SUBJECT, message.getSubject());
|
||||
nodeService.addAspect(nodeRef, EmailServerModel.ASPECT_EMAILED, emailProps);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Emailed aspect has been added.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ApplicationModel;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.model.ForumModel;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Abstact class implements common logic for forum processing email mesages.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public abstract class AbstractForumEmailMessageHandler extends AbstractEmailMessageHandler
|
||||
{
|
||||
/**
|
||||
* Posts content
|
||||
*
|
||||
* @param nodeRef Reference to node
|
||||
* @param parser Mail parser
|
||||
*/
|
||||
protected void addPostNode(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
NodeService nodeService = getNodeService();
|
||||
Date now = new Date();
|
||||
String nodeName = "posted-" + new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss").format(now) + ".html";
|
||||
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
|
||||
properties.put(ContentModel.PROP_NAME, nodeName);
|
||||
|
||||
ChildAssociationRef childAssoc = nodeService.createNode(nodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, nodeName),
|
||||
ForumModel.TYPE_POST, properties);
|
||||
NodeRef postNode = childAssoc.getChildRef();
|
||||
|
||||
// Add necessary aspects
|
||||
properties.clear();
|
||||
properties.put(ContentModel.PROP_TITLE, nodeName);
|
||||
nodeService.addAspect(postNode, ContentModel.ASPECT_TITLED, properties);
|
||||
properties.clear();
|
||||
properties.put(ApplicationModel.PROP_EDITINLINE, true);
|
||||
nodeService.addAspect(postNode, ApplicationModel.ASPECT_INLINEEDITABLE, properties);
|
||||
|
||||
// Write content
|
||||
if (message.getBody() != null)
|
||||
{
|
||||
writeContent(postNode, message.getBody().getContent(), message.getBody().getContentType(), message.getBody().getEncoding());
|
||||
}
|
||||
else
|
||||
{
|
||||
writeContent(postNode, "<The message was empty>");
|
||||
}
|
||||
addEmailedAspect(postNode, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds first child with specified name
|
||||
*
|
||||
* @param nodeRef Parent node for the search
|
||||
* @param subject String for search
|
||||
* @return Reference to found node or null if node isn't found
|
||||
*/
|
||||
protected NodeRef getTopicNode(NodeRef nodeRef, String subject)
|
||||
{
|
||||
List<ChildAssociationRef> assocRefList = getNodeService().getChildAssocs(nodeRef);
|
||||
Iterator<ChildAssociationRef> assocRefIter = assocRefList.iterator();
|
||||
|
||||
while (assocRefIter.hasNext())
|
||||
{
|
||||
|
||||
ChildAssociationRef assocRef = assocRefIter.next();
|
||||
if (assocRef.getQName().getLocalName().equals(subject))
|
||||
{
|
||||
return assocRef.getChildRef();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds topic node into Alfresco repository
|
||||
*
|
||||
* @param parentNode Parent node
|
||||
* @param name Topic name
|
||||
* @return Reference to created node
|
||||
*/
|
||||
protected NodeRef addTopicNode(NodeRef parentNode, String name)
|
||||
{
|
||||
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
|
||||
properties.put(ContentModel.PROP_NAME, name);
|
||||
|
||||
ChildAssociationRef association = getNodeService().createNode(parentNode, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name),
|
||||
ForumModel.TYPE_TOPIC, properties);
|
||||
NodeRef topic = association.getChildRef();
|
||||
|
||||
// Add necessary aspects
|
||||
properties.clear();
|
||||
properties.put(ApplicationModel.PROP_ICON, "topic");
|
||||
getNodeService().addAspect(topic, ApplicationModel.ASPECT_UIFACETS, properties);
|
||||
|
||||
return topic;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.model.ApplicationModel;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.model.ForumModel;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.email.EmailMessageException;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Handler implementation address to document node.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public class DocumentEmailMessageHandler extends AbstractForumEmailMessageHandler
|
||||
{
|
||||
private static final String forumNodeName = "EmailForum";
|
||||
|
||||
public void processMessage(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
String messageSubject;
|
||||
|
||||
if (message.getSubject() != null)
|
||||
{
|
||||
messageSubject = message.getSubject();
|
||||
}
|
||||
else
|
||||
{
|
||||
messageSubject = "EMPTY_SUBJECT_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
QName contentType = getNodeService().getType(nodeRef);
|
||||
|
||||
if (contentType.equals(ContentModel.TYPE_CONTENT))
|
||||
{
|
||||
NodeRef forumNode = getForumNode(nodeRef);
|
||||
|
||||
if (forumNode == null)
|
||||
{
|
||||
forumNode = addForumNode(nodeRef);
|
||||
}
|
||||
|
||||
// Try to find existed node
|
||||
NodeRef topicNode = getTopicNode(forumNode, messageSubject);
|
||||
|
||||
if (topicNode == null)
|
||||
{
|
||||
topicNode = addTopicNode(forumNode, messageSubject);
|
||||
}
|
||||
|
||||
addPostNode(topicNode, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-type"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds forum node
|
||||
*
|
||||
* @param nodeRef Paren node
|
||||
* @return Reference to created node
|
||||
*/
|
||||
private NodeRef addForumNode(NodeRef nodeRef)
|
||||
{
|
||||
NodeService nodeService=getNodeService();
|
||||
//Add discussable aspect to content node
|
||||
if (!nodeService.hasAspect(nodeRef, ForumModel.ASPECT_DISCUSSABLE))
|
||||
{
|
||||
nodeService.addAspect(nodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
|
||||
}
|
||||
|
||||
//Create forum node and associate it with content node
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
|
||||
properties.put(ContentModel.PROP_NAME, forumNodeName);
|
||||
ChildAssociationRef childAssoc = nodeService.createNode(nodeRef, ForumModel.ASSOC_DISCUSSION, ForumModel.ASSOC_DISCUSSION, ForumModel.TYPE_FORUM, properties);
|
||||
NodeRef forumNode = childAssoc.getChildRef();
|
||||
|
||||
//Add necessary aspects to forum node
|
||||
properties.clear();
|
||||
properties.put(ApplicationModel.PROP_ICON, "forum");
|
||||
nodeService.addAspect(forumNode, ApplicationModel.ASPECT_UIFACETS, properties);
|
||||
|
||||
return forumNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the first forum node
|
||||
*
|
||||
* @param nodeService Alfresco Node Service
|
||||
* @param nodeRef Parent node
|
||||
* @return Found node or null
|
||||
*/
|
||||
private NodeRef getForumNode(NodeRef nodeRef)
|
||||
{
|
||||
|
||||
if (getNodeService().hasAspect(nodeRef, ForumModel.ASPECT_DISCUSSABLE))
|
||||
{
|
||||
List<ChildAssociationRef> assocRefList = getNodeService().getChildAssocs(nodeRef);
|
||||
Iterator<ChildAssociationRef> assocRefIter = assocRefList.iterator();
|
||||
|
||||
while (assocRefIter.hasNext())
|
||||
{
|
||||
ChildAssociationRef assocRef = assocRefIter.next();
|
||||
QName nodeTypeName = getNodeService().getType(assocRef.getChildRef());
|
||||
|
||||
if (nodeTypeName.equals(ForumModel.TYPE_FORUM))
|
||||
return assocRef.getChildRef();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.email.EmailMessageException;
|
||||
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
|
||||
/**
|
||||
* Interface for email handler for processing email message.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public interface EmailMessageHandler
|
||||
{
|
||||
/**
|
||||
* Method invokes for processing email message.
|
||||
*
|
||||
* @param nodeRef Target node
|
||||
* @param message Email message
|
||||
* @exception EmailMessageException Exception is thrown if processing was failed
|
||||
* @exception DuplicateChildNodeNameException Exception is thrown if node name is duplicate.
|
||||
*/
|
||||
void processMessage(NodeRef nodeRef, EmailMessage message);
|
||||
|
||||
}
|
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
|
||||
import org.alfresco.email.server.EmailServerModel;
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.email.EmailMessageException;
|
||||
import org.alfresco.service.cmr.email.EmailMessagePart;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Handler implementation address to folder node.
|
||||
*
|
||||
* @author Yan O
|
||||
* @since 2.2
|
||||
*/
|
||||
public class FolderEmailMessageHandler extends AbstractEmailMessageHandler
|
||||
{
|
||||
private static final Log log = LogFactory.getLog(FolderEmailMessageHandler.class);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void processMessage(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Message is psocessing by SpaceMailMessageHandler");
|
||||
}
|
||||
try
|
||||
{
|
||||
// Check type of the node. It must be a SPACE
|
||||
QName nodeTypeName = getNodeService().getType(nodeRef);
|
||||
|
||||
if (nodeTypeName.equals(ContentModel.TYPE_FOLDER))
|
||||
{
|
||||
// Add the content into the system
|
||||
addAlfrescoContent(nodeRef, message, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Addressed node type isn't a folder. Message has been passed without any actions.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.content-error"), ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add content to Alfresco repository
|
||||
*
|
||||
* @param spaceNodeRef Addressed node
|
||||
* @param mailParser Mail message
|
||||
* @param nameConflictResolver String that can be used as part of name for resolving name conflict.
|
||||
* @throws IOException Exception can be thrown while saving a content into Alfresco repository.
|
||||
* @throws MessagingException Exception can be thrown while parsing e-mail message.
|
||||
*/
|
||||
public void addAlfrescoContent(NodeRef spaceNodeRef, EmailMessage message, String nameConflictResolver) throws IOException
|
||||
{
|
||||
// Set default values for email fields
|
||||
if (nameConflictResolver == null)
|
||||
nameConflictResolver = "";
|
||||
String messageSubject = "EMPTY_SUBJECT_" + nameConflictResolver;
|
||||
if (message.getSubject().length() != 0)
|
||||
{
|
||||
messageSubject = message.getSubject() + nameConflictResolver;
|
||||
}
|
||||
|
||||
// Create node
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Adding main content node ...");
|
||||
}
|
||||
NodeRef contentNodeRef;
|
||||
contentNodeRef = addContentNode(getNodeService(), spaceNodeRef, messageSubject);
|
||||
|
||||
// Add titled aspect
|
||||
addTitledAspect(contentNodeRef, messageSubject);
|
||||
|
||||
// Add emailed aspect
|
||||
addEmailedAspect(contentNodeRef, message);
|
||||
|
||||
// Write the message content
|
||||
|
||||
if (message.getBody() != null)
|
||||
writeContent(contentNodeRef, message.getBody().getContent(), message.getBody().getContentType(), message.getBody().getEncoding());
|
||||
else
|
||||
writeContent(contentNodeRef, "<The message was empty>");
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Main content node has been added.");
|
||||
}
|
||||
|
||||
// Add attachments
|
||||
EmailMessagePart[] attachments = message.getAttachments();
|
||||
for (EmailMessagePart attachment : attachments)
|
||||
{
|
||||
NodeRef attachmentNode;
|
||||
String fileName = attachment.getFileName();
|
||||
|
||||
// Add name conflict resolver if necessary
|
||||
if (nameConflictResolver.length() != 0)
|
||||
{
|
||||
if (fileName.lastIndexOf('.') != -1)
|
||||
fileName = fileName.substring(0, fileName.lastIndexOf('.')) + " (" + nameConflictResolver + ")" + fileName.substring(fileName.lastIndexOf('.'));
|
||||
else
|
||||
fileName += " (" + nameConflictResolver + ")";
|
||||
}
|
||||
|
||||
attachmentNode = addAttachment(getNodeService(), spaceNodeRef, contentNodeRef, fileName);
|
||||
writeContent(attachmentNode, attachment.getContent(), attachment.getContentType(), attachment.getEncoding());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new node into Alfresco repository with specified parameters. Node content isn't added. New node will be created with ContentModel.ASSOC_CONTAINS association with parent.
|
||||
*
|
||||
* @param nodeService Alfresco Node Service
|
||||
* @param parent Parent node
|
||||
* @param name Name of the new node
|
||||
* @return Reference to created node
|
||||
*/
|
||||
private NodeRef addContentNode(NodeService nodeService, NodeRef parent, String name)
|
||||
{
|
||||
return addContentNode(nodeService, parent, name, ContentModel.ASSOC_CONTAINS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new node into Alfresco repository with specified parameters. Node content isn't added.
|
||||
*
|
||||
* @param nodeService Alfresco Node Service
|
||||
* @param parent Parent node
|
||||
* @param name Name of the new node
|
||||
* @param assocType Association type that should be set between parent node and the new one.
|
||||
* @return Reference to created node
|
||||
*/
|
||||
private NodeRef addContentNode(NodeService nodeService, NodeRef parent, String name, QName assocType)
|
||||
{
|
||||
Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
|
||||
contentProps.put(ContentModel.PROP_NAME, name);
|
||||
ChildAssociationRef associationRef = nodeService.createNode(parent, assocType, QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name), ContentModel.TYPE_CONTENT,
|
||||
contentProps);
|
||||
return associationRef.getChildRef();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new node into Alfresco repository and mark its as an attachment.
|
||||
*
|
||||
* @param nodeService Alfresco Node Service.
|
||||
* @param folder Space/Folder to add.
|
||||
* @param mainContentNode Main content node. Any mail is added into Alfresco as one main content node and several its attachments. Each attachment related with its main node.
|
||||
* @param fileName File name for the attachment.
|
||||
* @return Reference to created node.
|
||||
*/
|
||||
private NodeRef addAttachment(NodeService nodeService, NodeRef folder, NodeRef mainContentNode, String fileName)
|
||||
{
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Adding attachment node (name=" + fileName + ").");
|
||||
}
|
||||
|
||||
NodeRef attachmentNode = addContentNode(nodeService, folder, fileName);
|
||||
|
||||
// Add attached aspect
|
||||
Map<QName, Serializable> attachedProps = new HashMap<QName, Serializable>();
|
||||
nodeService.addAspect(attachmentNode, EmailServerModel.ASPECT_ATTACHED, attachedProps);
|
||||
nodeService.createAssociation(attachmentNode, mainContentNode, EmailServerModel.ASSOC_ATTACHMENT);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Attachment has been added.");
|
||||
}
|
||||
return attachmentNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds titled aspect to the specified node.
|
||||
*
|
||||
* @param nodeRef Target node.
|
||||
* @param title Title
|
||||
*/
|
||||
private void addTitledAspect(NodeRef nodeRef, String title)
|
||||
{
|
||||
Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
|
||||
titledProps.put(ContentModel.PROP_TITLE, title);
|
||||
titledProps.put(ContentModel.PROP_DESCRIPTION, "Received by SMTP");
|
||||
getNodeService().addAspect(nodeRef, ContentModel.ASPECT_TITLED, titledProps);
|
||||
|
||||
if (log.isDebugEnabled())
|
||||
{
|
||||
log.debug("Titled aspect has been added.");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.model.ForumModel;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.email.EmailMessageException;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Handler implementation address to forum node.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public class ForumEmailMessageHandler extends AbstractForumEmailMessageHandler
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void processMessage(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
String messageSubject;
|
||||
|
||||
if (message.getSubject() != null)
|
||||
{
|
||||
messageSubject = message.getSubject();
|
||||
}
|
||||
else
|
||||
{
|
||||
messageSubject = "EMPTY_SUBJECT_" + System.currentTimeMillis();
|
||||
}
|
||||
|
||||
QName nodeType = getNodeService().getType(nodeRef);
|
||||
|
||||
if (nodeType.equals(ForumModel.TYPE_FORUM))
|
||||
{
|
||||
NodeRef topicNode = getTopicNode(nodeRef, messageSubject);
|
||||
|
||||
if (topicNode == null)
|
||||
{
|
||||
topicNode = addTopicNode(nodeRef, messageSubject);
|
||||
}
|
||||
addPostNode(topicNode, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-type"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2007 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program 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 General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.email.server.handler;
|
||||
|
||||
import org.alfresco.i18n.I18NUtil;
|
||||
import org.alfresco.model.ForumModel;
|
||||
import org.alfresco.service.cmr.email.EmailMessage;
|
||||
import org.alfresco.service.cmr.email.EmailMessageException;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Handler implementation address to topic node.
|
||||
*
|
||||
* @author maxim
|
||||
* @since 2.2
|
||||
*/
|
||||
public class TopicEmailMessageHandler extends AbstractForumEmailMessageHandler
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void processMessage(NodeRef nodeRef, EmailMessage message)
|
||||
{
|
||||
QName nodeType = getNodeService().getType(nodeRef);
|
||||
NodeRef topicNode = null;
|
||||
|
||||
if (nodeType.equals(ForumModel.TYPE_TOPIC))
|
||||
{
|
||||
topicNode = nodeRef;
|
||||
}
|
||||
else if (nodeType.equals(ForumModel.TYPE_POST))
|
||||
{
|
||||
topicNode = getNodeService().getPrimaryParent(nodeRef).getChildRef();
|
||||
}
|
||||
if (topicNode == null)
|
||||
{
|
||||
throw new EmailMessageException(I18NUtil.getMessage("email.server.incorrect-node-ref"));
|
||||
}
|
||||
addPostNode(topicNode, message);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user