001/*
002 * (C) Copyright 2013 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 *     Martin Pernollet
018 */
019package org.nuxeo.ecm.platform.groups.audit.service.acl.job.publish;
020
021import java.io.Serializable;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.automation.AutomationService;
027import org.nuxeo.ecm.automation.InvalidChainException;
028import org.nuxeo.ecm.automation.OperationChain;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.OperationException;
031import org.nuxeo.ecm.automation.OperationParameters;
032import org.nuxeo.ecm.automation.core.mail.Mailer;
033import org.nuxeo.ecm.automation.core.operations.notification.SendMail;
034import org.nuxeo.ecm.automation.core.util.StringList;
035import org.nuxeo.ecm.core.api.Blob;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.NuxeoException;
039import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
040import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
041import org.nuxeo.ecm.platform.groups.audit.service.acl.utils.MessageAccessor;
042import org.nuxeo.runtime.api.Framework;
043
044public class PublishByMail implements IResultPublisher {
045
046    private static final long serialVersionUID = 1L;
047
048    private static final Log log = LogFactory.getLog(PublishByMail.class);
049
050    public static final String PROPERTY_ACLAUDIT_SENDMAIL_CHAIN = "ACL.Audit.SendMail";
051
052    public static final String PROPERTY_MAILFROM = "mail.from";
053
054    public static final String PROPERTY_MAIL_SUBJECT = "message.acl.audit.mail.title";
055
056    public static final String PROPERTY_MAIL_BODY = "message.acl.audit.mail.body";
057
058    public static String OUTPUT_FILE_NAME = "permissions.xls";
059
060    public static String FROM = "noreply@nuxeo.com";
061
062    protected String repositoryName;
063
064    protected String to;
065
066    protected String defaultFrom;
067
068    public PublishByMail(String to, String defaultFrom, String repositoryName) {
069        this.repositoryName = repositoryName;
070        this.to = to;
071        this.defaultFrom = defaultFrom;
072    }
073
074    @Override
075    public void publish(final Blob file) {
076        file.setFilename(OUTPUT_FILE_NAME);
077        new UnrestrictedSessionRunner(repositoryName) {
078            @Override
079            public void run() {
080                DocumentModel docToSend = createDocument(session, file, "", "");
081                doCallOperationSendMail(session, docToSend, to, defaultFrom);
082                log.debug("audit sent");
083            }
084        }.runUnrestricted();
085    }
086
087    protected void doCallOperationSendMail(CoreSession session, DocumentModel docToSend, String to, String defaultFrom)
088            {
089        String title = MessageAccessor.get(session, PROPERTY_MAIL_SUBJECT);
090        String body = MessageAccessor.get(session, PROPERTY_MAIL_BODY);
091        String from = Framework.getProperty(PROPERTY_MAILFROM, defaultFrom);
092        AutomationService automation = Framework.getLocalService(AutomationService.class);
093
094        OperationContext ctx = new OperationContext(session);
095        ctx.setInput(docToSend);
096
097        try {
098            OperationChain chain = new OperationChain(PROPERTY_ACLAUDIT_SENDMAIL_CHAIN);
099            OperationParameters params = chain.add(SendMail.ID);
100            if (params == null) {
101                log.error("failed to retrieve operation " + SendMail.ID + " in chain " + chain);
102                return;
103            }
104
105            // configure email
106            params.set("from", from);
107            params.set("to", to);
108            params.set("subject", title);
109            params.set("message", body);
110            String[] str = { "file:content" };
111            params.set("files", new StringList(str));
112            // TODO: see SendMail test case where we can directly pass a blob
113
114            // do send mail
115            log.debug("Automation run " + PROPERTY_ACLAUDIT_SENDMAIL_CHAIN + " for " + to);
116            automation.run(ctx, chain);
117            log.debug("Automation done " + PROPERTY_ACLAUDIT_SENDMAIL_CHAIN + " for " + to);
118        } catch (OperationException e) {
119            throw new NuxeoException(e);
120        }
121    }
122
123    protected OperationParameters findParameters(OperationChain chain, String id) {
124        List<OperationParameters> params = chain.getOperations();
125        for (OperationParameters p : params)
126            if (p.id().equals(id))
127                return p;
128        return null;
129    }
130
131    protected DocumentModel createDocument(CoreSession session, Blob blob, String title, String filename)
132            {
133        DocumentModel document = session.createDocumentModel("File");
134        document.setPropertyValue("file:content", (Serializable) blob);
135        document.setPropertyValue("file:filename", filename);
136        document.setPropertyValue("dublincore:title", title);
137        return document;
138    }
139
140    protected void logMailerConfiguration() {
141        Mailer m = SendMail.COMPOSER.getMailer();
142        log.info("mail.smtp.auth:" + m.getConfiguration().get("mail.smtp.auth"));
143        log.info("mail.smtp.starttls.enable:" + m.getConfiguration().get("mail.smtp.starttls.enable"));
144        log.info("mail.smtp.host:" + m.getConfiguration().get("mail.smtp.host"));
145        log.info("mail.smtp.user:" + m.getConfiguration().get("mail.smtp.user"));
146        log.info("mail.smtp.password:" + m.getConfiguration().get("mail.smtp.password"));
147    }
148}