preparing to re-implement message settings
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.message;
|
||||
|
||||
import static de.srsoftware.tools.PathHandler.CONTENT_TYPE;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.constants.Constants.TIME_FORMATTER;
|
||||
import static de.srsoftware.umbrella.core.constants.Constants.UTF8;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingConfig;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static de.srsoftware.umbrella.message.Constants.*;
|
||||
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
import de.srsoftware.tools.Path;
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.BaseHandler;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.api.PostBox;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Envelope;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import de.srsoftware.umbrella.core.model.User;
|
||||
import de.srsoftware.umbrella.message.model.CombinedMessage;
|
||||
@@ -29,10 +36,13 @@ import jakarta.mail.internet.MimeBodyPart;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
import jakarta.mail.util.ByteArrayDataSource;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class MessageSystem implements PostBox, EventListener {
|
||||
public class MessageSystem extends BaseHandler implements PostBox, EventListener {
|
||||
public static final System.Logger LOG = System.getLogger(MessageSystem.class.getSimpleName());
|
||||
private final Timer timer = new Timer();
|
||||
|
||||
@@ -77,8 +87,8 @@ public class MessageSystem implements PostBox, EventListener {
|
||||
debugAddress = config.get(DEBUG_ADDREESS).map(Object::toString).orElse(null);
|
||||
port = config.get(CONFIG_SMTP_PORT,587);
|
||||
host = config.get(CONFIG_SMTP_HOST).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.host not configured!"));
|
||||
user = config.get(CONFIG_SMTP_USER).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.user not configured!"));
|
||||
pass = config.get(CONFIG_SMTP_PASS).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.pass not configured!"));
|
||||
user = config.get(CONFIG_SMTP_USER).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.user not configured!"));
|
||||
pass = config.get(CONFIG_SMTP_PASS).map(Object::toString).orElseThrow(() -> new RuntimeException("umbrella.modules.message.smtp.pass not configured!"));
|
||||
from = user;
|
||||
ModuleRegistry.add(this);
|
||||
new SubmissionTask(8).schedule();
|
||||
@@ -91,6 +101,37 @@ public class MessageSystem implements PostBox, EventListener {
|
||||
messageBus().register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = ModuleRegistry.userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case null -> listMessages(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_BAD_REQUEST,"Invalid project id");
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean listMessages(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
var messages = queue.stream().filter(e -> e.isFor(user)).map(e -> summary(e, user.language())).toList();
|
||||
return sendContent(ex,messages);
|
||||
}
|
||||
|
||||
private static JSONObject summary(Envelope envelope, String lang) {
|
||||
var sender = envelope.message().sender().name();
|
||||
var subject = envelope.message().subject().translate(lang);
|
||||
var time = envelope.time().format(TIME_FORMATTER);
|
||||
return new JSONObject(Map.of(SENDER,sender,SUBJECT,subject,TIMESTAMP,time));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(Event<?> event) {
|
||||
for (var user : event.audience()){
|
||||
@@ -102,12 +143,6 @@ public class MessageSystem implements PostBox, EventListener {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void send(Envelope envelope) {
|
||||
queue.add(envelope);
|
||||
new Thread(() -> processMessages(null)).start();
|
||||
}
|
||||
|
||||
private synchronized void processMessages(Integer scheduledHour) {
|
||||
LOG.log(INFO,"Running {0}…",scheduledHour == null ? "instantly" : "scheduled at "+scheduledHour);
|
||||
var queue = new ArrayList<>(this.queue);
|
||||
@@ -149,24 +184,6 @@ public class MessageSystem implements PostBox, EventListener {
|
||||
if (scheduledHour != null) new SubmissionTask(scheduledHour).schedule();
|
||||
}
|
||||
|
||||
private Session session() {
|
||||
if (session == null){
|
||||
Properties props = new Properties();
|
||||
props.put(HOST, host);
|
||||
props.put(PORT, port);
|
||||
props.put(AUTH, true);
|
||||
props.put(SSL, true);
|
||||
props.put(ENVELOPE_FROM,from);
|
||||
session = Session.getInstance(props);
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
public void setDebugAddress(String newVal) {
|
||||
this.debugAddress = newVal;
|
||||
}
|
||||
|
||||
|
||||
private void send(CombinedMessage message, Date date) throws MessagingException {
|
||||
var receiver = message.receiver();
|
||||
LOG.log(TRACE,"Sending combined message to {0}…",receiver);
|
||||
@@ -202,4 +219,28 @@ public class MessageSystem implements PostBox, EventListener {
|
||||
Transport.send(msg,user,pass);
|
||||
LOG.log(DEBUG, "Sent message to {0}.", receiver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Envelope envelope) {
|
||||
queue.add(envelope);
|
||||
new Thread(() -> processMessages(null)).start();
|
||||
}
|
||||
|
||||
private Session session() {
|
||||
if (session == null){
|
||||
Properties props = new Properties();
|
||||
props.put(HOST, host);
|
||||
props.put(PORT, port);
|
||||
props.put(AUTH, true);
|
||||
props.put(SSL, true);
|
||||
props.put(ENVELOPE_FROM,from);
|
||||
session = Session.getInstance(props);
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
public void setDebugAddress(String newVal) {
|
||||
this.debugAddress = newVal;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user