001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.mail.listener.action; 023 024import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.ATTACHMENTS_KEY; 025import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.CONTENT_KEY; 026import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PARENT_PATH_KEY; 027import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.SUBJECT_KEY; 028 029import java.util.Collections; 030import java.util.List; 031import java.util.regex.Pattern; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.common.utils.ExceptionUtils; 036import org.nuxeo.ecm.automation.AutomationService; 037import org.nuxeo.ecm.automation.OperationContext; 038import org.nuxeo.ecm.automation.OperationException; 039import org.nuxeo.ecm.core.api.CoreSession; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.core.api.NuxeoException; 042import org.nuxeo.ecm.core.api.PathRef; 043import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 044import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 045import org.nuxeo.ecm.platform.mail.action.ExecutionContext; 046import org.nuxeo.runtime.api.Framework; 047 048/** 049 * Creates a MailMessage document for every new email found in the INBOX. The creation is handled by an AutomationChain 050 * 051 * @since 6.0 052 * @author tiry 053 */ 054public class CreateDocumentsFromAutomationChainAction extends AbstractMailAction { 055 056 private static final Log log = LogFactory.getLog(CreateDocumentsFromAutomationChainAction.class); 057 058 protected String chainName; 059 060 public CreateDocumentsFromAutomationChainAction(String chainName) { 061 super(); 062 this.chainName = chainName; 063 } 064 065 public Pattern stupidRegexp = Pattern.compile("^[- .,;?!:/\\\\'\"]*$"); 066 067 protected String getChainName() { 068 if (chainName == null) { 069 return Framework.getProperty("org.nuxeo.mail.automation.chain", "CreateMailDocumentFromAutomation"); 070 } 071 return chainName; 072 } 073 074 protected String generateMailName(String subject) { 075 PathSegmentService pss = Framework.getService(PathSegmentService.class); 076 return pss.generatePathSegment(subject + System.currentTimeMillis() % 10000); 077 } 078 079 @Override 080 public boolean execute(ExecutionContext context) { 081 CoreSession session = getCoreSession(context); 082 if (session == null) { 083 log.error("Could not open CoreSession"); 084 return false; 085 } 086 087 AutomationService as = Framework.getService(AutomationService.class); 088 089 try (OperationContext automationCtx = new OperationContext(session)) { 090 automationCtx.putAll(context); 091 092 ExecutionContext initialContext = context.getInitialContext(); 093 String parentPath = (String) initialContext.get(PARENT_PATH_KEY); 094 DocumentModel mailFolder = session.getDocument(new PathRef(parentPath)); 095 automationCtx.put("mailFolder", mailFolder); 096 automationCtx.put("executionContext", initialContext); 097 String subject = (String) context.get(SUBJECT_KEY); 098 automationCtx.put("mailDocumentName", generateMailName(subject)); 099 100 @SuppressWarnings("unchecked") 101 List<FileBlob> attachments = (List<FileBlob>) context.get(ATTACHMENTS_KEY); 102 if (attachments == null) { 103 automationCtx.put(ATTACHMENTS_KEY, Collections.EMPTY_LIST); 104 automationCtx.put(CONTENT_KEY, Collections.EMPTY_MAP); 105 } 106 107 try { 108 as.run(automationCtx, getChainName()); 109 } catch (OperationException e) { 110 throw new NuxeoException(e); 111 } 112 } catch (Exception e) { 113 ExceptionUtils.checkInterrupt(e); 114 } 115 116 return true; 117 } 118 119}