001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Alexandre Russel 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.mail.action; 021 022import java.io.Serializable; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.mail.Address; 027import javax.mail.Message; 028import javax.mail.MessagingException; 029import javax.mail.internet.InternetAddress; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentModelList; 035import org.nuxeo.ecm.directory.Session; 036import org.nuxeo.ecm.directory.api.DirectoryService; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * Action that check the mail address against the user directory. If the address of the sender is not in the user 041 * directory, the mail is not processed further. 042 * <p> 043 * If the sender is in the user directory, it put the principal as a string in the context under the "sender" key. 044 * 045 * @author Alexandre Russel 046 */ 047public class CheckSenderAction implements MessageAction { 048 049 private static final Log log = LogFactory.getLog(CheckSenderAction.class); 050 051 public boolean execute(ExecutionContext context) throws MessagingException { 052 Message message = context.getMessage(); 053 Address[] addresses = message.getFrom(); 054 if (addresses == null || addresses.length == 0 || !(addresses[0] instanceof InternetAddress)) { 055 log.debug("No internet messages, stopping the pipe: " + message); 056 return false; 057 } 058 InternetAddress address = (InternetAddress) addresses[0]; 059 String principal = getPrincipal(address.getAddress()); 060 if (principal == null) { 061 log.debug("Sender not in user directory. Stop processing"); 062 return false; 063 } 064 context.put("sender", principal); 065 return true; 066 } 067 068 private static String getPrincipal(String address) { 069 String principal; 070 DirectoryService directoryService = Framework.getService(DirectoryService.class); 071 try (Session session = directoryService.open("userDirectory")) { 072 073 Map<String, Serializable> map = new HashMap<String, Serializable>(); 074 map.put("email", address); 075 DocumentModelList list = session.query(map); 076 if (list == null || list.isEmpty()) { 077 log.debug("Stopping pipe, address: " + address + " return " + list); 078 return null; 079 } 080 DocumentModel dm = list.get(0); 081 principal = dm.getId(); 082 } 083 return principal; 084 } 085 086 public void reset(ExecutionContext context) { 087 // do nothing 088 } 089 090}