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