Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10.10-HF/fix-NXP-29810-fix-operation-Document.Mail-to-consider-SMTP-service-state-setting #4573

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,6 +19,7 @@
package org.nuxeo.ecm.automation.core.operations.notification;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.nuxeo.ecm.core.management.api.AdministrativeStatus.PASSIVE;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -58,6 +59,7 @@
import org.nuxeo.ecm.core.api.model.impl.ListProperty;
import org.nuxeo.ecm.core.api.model.impl.MapProperty;
import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
import org.nuxeo.ecm.platform.ec.notification.service.NotificationServiceHelper;
import org.nuxeo.ecm.platform.rendering.api.RenderingException;
import org.nuxeo.runtime.api.Framework;
Expand Down Expand Up @@ -161,6 +163,11 @@ protected void send(DocumentModel doc)
// TODO should sent one by one to each recipient? and have the template
// rendered for each recipient? Use: "mailto" var name?
try {
AdministrativeStatusManager asm = Framework.getService(AdministrativeStatusManager.class);
if (asm != null && PASSIVE.equals(asm.getStatus("org.nuxeo.ecm.smtp").getState())) {
log.debug("SMTP is in passive mode, mail not sent");
return;
}
Map<String, Object> map = Scripting.initBindings(ctx);
// do not use document wrapper which is working only in mvel.
map.put("Document", doc);
Expand Down Expand Up @@ -201,6 +208,11 @@ protected void send(DocumentModel doc)

// Only visible for testing purposes
protected String createDocUrlWithToken(String documentUrl, String token) {
AdministrativeStatusManager asm = Framework.getService(AdministrativeStatusManager.class);
if (asm != null && PASSIVE.equals(asm.getStatus("org.nuxeo.ecm.smtp").getState())) {
log.debug("SMTP is in passive mode, mail not sent");
return null;
}
return token != null ? UriBuilder.fromUri(documentUrl).queryParam("token", token).build().toString()
: documentUrl;
}
Expand Down
Expand Up @@ -18,17 +18,25 @@
*/
package org.nuxeo.ecm.automation.core.operations.notification;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.nuxeo.ecm.core.management.api.AdministrativeStatus.PASSIVE;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.RuntimeFeature;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@RunWith(FeaturesRunner.class)
@Features({ RuntimeFeature.class })
@Features({ RuntimeFeature.class, CoreFeature.class })
@Deploy("org.nuxeo.runtime.management")
@Deploy("org.nuxeo.ecm.core.management")
public class SendMailTest {

protected static final String TOKEN = "ABC";
Expand All @@ -55,6 +63,16 @@ public void shouldPlaceTokenWhenUrlHasNoFragment() {
@Test
public void shouldPlaceTokenWhenUrlHasFragment() {
final String docUrl = "http://www.nuxeo.com/#/server";
assertEquals("http://www.nuxeo.com/?token="+TOKEN+"#/server", sendMail.createDocUrlWithToken(docUrl, TOKEN));
assertEquals("http://www.nuxeo.com/?token=" + TOKEN + "#/server",
sendMail.createDocUrlWithToken(docUrl, TOKEN));
}

@Test
public void shouldReturnNullOnPassiveSMTPSetting() {
AdministrativeStatusManager localManager = Framework.getService(AdministrativeStatusManager.class);
localManager.setStatus("org.nuxeo.ecm.smtp", PASSIVE, "turn off SMTP service", "Administrator");
assertTrue(localManager.getStatus("org.nuxeo.ecm.smtp").isPassive());
final String docUrl = "http://www.nuxeo.com/";
assertNull(sendMail.createDocUrlWithToken(docUrl, TOKEN));
}
}