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    @Override
054    public boolean execute(ExecutionContext context) throws MessagingException {
055        Message message = context.getMessage();
056        Address[] addresses = message.getFrom();
057        if (addresses == null || addresses.length == 0 || !(addresses[0] instanceof InternetAddress)) {
058            log.debug("No internet messages, stopping the pipe: " + message);
059            return false;
060        }
061        InternetAddress address = (InternetAddress) addresses[0];
062        String principal = getPrincipal(address.getAddress());
063        if (principal == null) {
064            log.debug("Sender not in user directory. Stop processing");
065            return false;
066        }
067        context.put("sender", principal);
068        return true;
069    }
070
071    private static String getPrincipal(String address) {
072        String principal;
073        DirectoryService directoryService = Framework.getService(DirectoryService.class);
074        try (Session session = directoryService.open("userDirectory")) {
075
076            Map<String, Serializable> map = new HashMap<>();
077            map.put("email", address);
078            DocumentModelList list = session.query(map);
079            if (list == null || list.isEmpty()) {
080                log.debug("Stopping pipe, address: " + address + " return " + list);
081                return null;
082            }
083            DocumentModel dm = list.get(0);
084            principal = dm.getId();
085        }
086        return principal;
087    }
088
089    @Override
090    public void reset(ExecutionContext context) {
091        // do nothing
092    }
093
094}