Continue structural port from Spring Boot to Undertow
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
|
||||
package io.kamax.mxisd.threepid.connector;
|
||||
|
||||
public interface IThreePidConnector {
|
||||
public interface ThreePidConnector {
|
||||
|
||||
String getId();
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.email;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.mxisd.Mxisd;
|
||||
import io.kamax.mxisd.config.threepid.connector.EmailSmtpConfig;
|
||||
import io.kamax.mxisd.config.threepid.medium.EmailConfig;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BuiltInEmailConnectorSupplier implements EmailConnectorSupplier {
|
||||
|
||||
@Override
|
||||
public Optional<EmailConnector> apply(EmailConfig cfg, Mxisd mxisd) {
|
||||
if (StringUtils.equals(EmailSmtpConnector.ID, cfg.getConnector())) {
|
||||
EmailSmtpConfig smtpCfg = GsonUtil.get().fromJson(cfg.getConnectors().getOrDefault(EmailSmtpConnector.ID, new JsonObject()), EmailSmtpConfig.class);
|
||||
return Optional.of(new EmailSmtpConnector(smtpCfg));
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,9 +21,9 @@
|
||||
package io.kamax.mxisd.threepid.connector.email;
|
||||
|
||||
import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.threepid.connector.IThreePidConnector;
|
||||
import io.kamax.mxisd.threepid.connector.ThreePidConnector;
|
||||
|
||||
public interface IEmailConnector extends IThreePidConnector {
|
||||
public interface EmailConnector extends ThreePidConnector {
|
||||
|
||||
@Override
|
||||
default String getMedium() {
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.email;
|
||||
|
||||
import io.kamax.mxisd.Mxisd;
|
||||
import io.kamax.mxisd.config.threepid.medium.EmailConfig;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface EmailConnectorSupplier extends BiFunction<EmailConfig, Mxisd, Optional<EmailConnector>> {
|
||||
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.email;
|
||||
|
||||
import com.sendgrid.SendGrid;
|
||||
import com.sendgrid.SendGridException;
|
||||
import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.as.IMatrixIdInvite;
|
||||
import io.kamax.mxisd.config.MatrixConfig;
|
||||
import io.kamax.mxisd.config.ServerConfig;
|
||||
import io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig;
|
||||
import io.kamax.mxisd.exception.FeatureNotAvailable;
|
||||
import io.kamax.mxisd.invitation.IThreePidInviteReply;
|
||||
import io.kamax.mxisd.notification.INotificationHandler;
|
||||
import io.kamax.mxisd.threepid.notification.PlaceholderNotificationGenerator;
|
||||
import io.kamax.mxisd.threepid.session.IThreePidSession;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static com.sendgrid.SendGrid.Email;
|
||||
import static com.sendgrid.SendGrid.Response;
|
||||
import static io.kamax.mxisd.config.threepid.connector.EmailSendGridConfig.EmailTemplate;
|
||||
|
||||
public class EmailSendGridNotificationHandler extends PlaceholderNotificationGenerator implements INotificationHandler {
|
||||
|
||||
private transient final Logger log = LoggerFactory.getLogger(EmailSendGridNotificationHandler.class);
|
||||
|
||||
private EmailSendGridConfig cfg;
|
||||
private SendGrid sendgrid;
|
||||
|
||||
public EmailSendGridNotificationHandler(MatrixConfig mxCfg, ServerConfig srvCfg, EmailSendGridConfig cfg) {
|
||||
super(mxCfg, srvCfg);
|
||||
this.cfg = cfg;
|
||||
this.sendgrid = new SendGrid(cfg.getApi().getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "sendgrid";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMedium() {
|
||||
return ThreePidMedium.Email.getId();
|
||||
}
|
||||
|
||||
protected Email getEmail() {
|
||||
Email email = new Email();
|
||||
email.setFrom(cfg.getIdentity().getFrom());
|
||||
email.setFromName(cfg.getIdentity().getName());
|
||||
return email;
|
||||
}
|
||||
|
||||
private String getFromFile(String path) {
|
||||
try {
|
||||
return IOUtils.toString(new FileInputStream(path), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Couldn't create notification content using file " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForInvite(IMatrixIdInvite invite) {
|
||||
EmailTemplate template = cfg.getTemplates().getGeneric().get("matrixId");
|
||||
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForInvite(invite, template.getSubject()));
|
||||
email.setText(populateForInvite(invite, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForInvite(invite, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(invite.getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForReply(IThreePidInviteReply invite) {
|
||||
EmailTemplate template = cfg.getTemplates().getInvite();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForReply(invite, template.getSubject()));
|
||||
email.setText(populateForReply(invite, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForReply(invite, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(invite.getInvite().getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForValidation(IThreePidSession session) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getLocal();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForValidation(session, template.getSubject()));
|
||||
email.setText(populateForValidation(session, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForValidation(session, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(session.getThreePid().getAddress(), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendForRemoteValidation(IThreePidSession session) {
|
||||
EmailTemplate template = cfg.getTemplates().getSession().getLocal();
|
||||
Email email = getEmail();
|
||||
email.setSubject(populateForRemoteValidation(session, template.getSubject()));
|
||||
email.setText(populateForRemoteValidation(session, getFromFile(template.getBody().getText())));
|
||||
email.setHtml(populateForRemoteValidation(session, getFromFile(template.getBody().getHtml())));
|
||||
|
||||
send(session.getThreePid().getAddress(), email);
|
||||
}
|
||||
|
||||
private void send(String recipient, Email email) {
|
||||
if (StringUtils.isBlank(cfg.getIdentity().getFrom())) {
|
||||
throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " +
|
||||
"You must set a value for notifications to work");
|
||||
}
|
||||
|
||||
try {
|
||||
email.addTo(recipient);
|
||||
email.setFrom(cfg.getIdentity().getFrom());
|
||||
email.setFromName(cfg.getIdentity().getName());
|
||||
Response response = sendgrid.send(email);
|
||||
if (response.getStatus()) {
|
||||
log.info("Successfully sent email to {} using SendGrid", recipient);
|
||||
} else {
|
||||
throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
|
||||
}
|
||||
} catch (SendGridException e) {
|
||||
throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,7 +40,9 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
public class EmailSmtpConnector implements IEmailConnector {
|
||||
public class EmailSmtpConnector implements EmailConnector {
|
||||
|
||||
public static final String ID = "smtp";
|
||||
|
||||
private transient final Logger log = LoggerFactory.getLogger(EmailSmtpConnector.class);
|
||||
|
||||
@@ -48,7 +50,7 @@ public class EmailSmtpConnector implements IEmailConnector {
|
||||
private Session session;
|
||||
|
||||
public EmailSmtpConnector(EmailSmtpConfig cfg) {
|
||||
this.cfg = cfg;
|
||||
this.cfg = cfg.build();
|
||||
|
||||
Properties sCfg = new Properties();
|
||||
sCfg.setProperty("mail.smtp.host", cfg.getHost());
|
||||
@@ -68,7 +70,7 @@ public class EmailSmtpConnector implements IEmailConnector {
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "smtp";
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,7 +20,9 @@
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.phone;
|
||||
|
||||
public class BlackholePhoneConnector implements IPhoneConnector {
|
||||
public class BlackholePhoneConnector implements PhoneConnector {
|
||||
|
||||
public static final String ID = "none";
|
||||
|
||||
@Override
|
||||
public void send(String recipient, String content) {
|
||||
@@ -29,7 +31,7 @@ public class BlackholePhoneConnector implements IPhoneConnector {
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "BLACKHOLE";
|
||||
return ID;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.phone;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.mxisd.Mxisd;
|
||||
import io.kamax.mxisd.config.threepid.connector.PhoneTwilioConfig;
|
||||
import io.kamax.mxisd.config.threepid.medium.PhoneConfig;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BuiltInPhoneConnectorSupplier implements PhoneConnectorSupplier {
|
||||
|
||||
@Override
|
||||
public Optional<PhoneConnector> apply(PhoneConfig cfg, Mxisd mxisd) {
|
||||
if (StringUtils.equals(PhoneSmsTwilioConnector.ID, cfg.getConnector())) {
|
||||
PhoneTwilioConfig cCfg = GsonUtil.get().fromJson(cfg.getConnectors().getOrDefault(PhoneSmsTwilioConnector.ID, new JsonObject()), PhoneTwilioConfig.class);
|
||||
return Optional.of(new PhoneSmsTwilioConnector(cCfg));
|
||||
}
|
||||
|
||||
if (StringUtils.equals(BlackholePhoneConnector.ID, cfg.getConnector())) {
|
||||
return Optional.of(new BlackholePhoneConnector());
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,9 +21,9 @@
|
||||
package io.kamax.mxisd.threepid.connector.phone;
|
||||
|
||||
import io.kamax.matrix.ThreePidMedium;
|
||||
import io.kamax.mxisd.threepid.connector.IThreePidConnector;
|
||||
import io.kamax.mxisd.threepid.connector.ThreePidConnector;
|
||||
|
||||
public interface IPhoneConnector extends IThreePidConnector {
|
||||
public interface PhoneConnector extends ThreePidConnector {
|
||||
|
||||
@Override
|
||||
default String getMedium() {
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2018 Kamax Sarl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.threepid.connector.phone;
|
||||
|
||||
import io.kamax.mxisd.Mxisd;
|
||||
import io.kamax.mxisd.config.threepid.medium.PhoneConfig;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface PhoneConnectorSupplier extends BiFunction<PhoneConfig, Mxisd, Optional<PhoneConnector>> {
|
||||
|
||||
}
|
||||
@@ -29,14 +29,16 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PhoneSmsTwilioConnector implements IPhoneConnector {
|
||||
public class PhoneSmsTwilioConnector implements PhoneConnector {
|
||||
|
||||
public static final String ID = "twilio";
|
||||
|
||||
private transient final Logger log = LoggerFactory.getLogger(PhoneSmsTwilioConnector.class);
|
||||
|
||||
private PhoneTwilioConfig cfg;
|
||||
|
||||
public PhoneSmsTwilioConnector(PhoneTwilioConfig cfg) {
|
||||
this.cfg = cfg;
|
||||
this.cfg = cfg.build();
|
||||
|
||||
Twilio.init(cfg.getAccountSid(), cfg.getAuthToken());
|
||||
log.info("Twilio API has been initiated");
|
||||
@@ -44,7 +46,7 @@ public class PhoneSmsTwilioConnector implements IPhoneConnector {
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "twilio";
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user