001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.mail.action;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import javax.mail.Address;
029import javax.mail.Message;
030import javax.mail.MessagingException;
031import javax.mail.internet.InternetAddress;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.directory.Session;
038import org.nuxeo.ecm.directory.api.DirectoryService;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * Action that check the mail address against the user directory. If the address of the sender is not in the user
043 * directory, the mail is not processed further.
044 * <p>
045 * If the sender is in the user directory, it put the principal as a string in the context under the "sender" key.
046 *
047 * @author Alexandre Russel
048 */
049public class CheckSenderAction implements MessageAction {
050
051    private static final Log log = LogFactory.getLog(CheckSenderAction.class);
052
053    public boolean execute(ExecutionContext context) throws MessagingException {
054        Message message = context.getMessage();
055        Address[] addresses = message.getFrom();
056        if (addresses == null || addresses.length == 0 || !(addresses[0] instanceof InternetAddress)) {
057            log.debug("No internet messages, stopping the pipe: " + message);
058            return false;
059        }
060        InternetAddress address = (InternetAddress) addresses[0];
061        String principal = getPrincipal(address.getAddress());
062        if (principal == null) {
063            log.debug("Sender not in user directory. Stop processing");
064            return false;
065        }
066        context.put("sender", principal);
067        return true;
068    }
069
070    private static String getPrincipal(String address) {
071        String principal;
072        DirectoryService directoryService = Framework.getService(DirectoryService.class);
073        try (Session session = directoryService.open("userDirectory")) {
074
075            Map<String, Serializable> map = new HashMap<String, Serializable>();
076            map.put("email", address);
077            DocumentModelList list = session.query(map);
078            if (list == null || list.isEmpty()) {
079                log.debug("Stopping pipe, address: " + address + " return " + list);
080                return null;
081            }
082            DocumentModel dm = list.get(0);
083            principal = dm.getId();
084        }
085        return principal;
086    }
087
088    public void reset(ExecutionContext context) {
089        // do nothing
090    }
091
092}