preparing for sending documents

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-07-17 20:15:28 +02:00
parent 93449e4bad
commit 6a58087ace
19 changed files with 146 additions and 110 deletions

View File

@@ -33,9 +33,12 @@ public class Constants {
public static final String ESTIMATED_TIME = "estimated_time";
public static final String EXPIRATION = "expiration";
public static final String FALLBACK_LANG = "de";
public static final String GET = "GET";
public static final String ID = "id";
public static final String JSONARRAY = "json array";
public static final String JSONOBJECT = "json object";
public static final String KEY = "key";
public static final String LANGUAGE = "language";
public static final String LOGIN = "login";

View File

@@ -0,0 +1,8 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.api;
import de.srsoftware.umbrella.core.model.Envelope;
public interface PostBox {
public void send(Envelope envelope);
}

View File

@@ -1,7 +1,6 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.api;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

View File

@@ -0,0 +1,39 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;
import java.util.Set;
import org.json.JSONObject;
public record Attachment(String name, String mime, byte[] content) {
private static final Base64.Decoder BASE64 = Base64.getDecoder();
@Override
public boolean equals(Object o) {
if (!(o instanceof Attachment that)) return false;
return Objects.equals(name, that.name) && Objects.equals(mime, that.mime) && Objects.deepEquals(content, that.content);
}
@Override
public int hashCode() {
return Objects.hash(name, mime, Arrays.hashCode(content));
}
public static <T> Attachment of(JSONObject json) throws UmbrellaException {
for (var key : Set.of(NAME, MIME, DATA)) {
if (!json.has(key)) throw missingFieldException(key);
}
if (!(json.get(NAME) instanceof String name)) throw invalidFieldException(NAME,STRING);
if (!(json.get(MIME) instanceof String mime)) throw invalidFieldException(MIME,STRING);
if (!(json.get(DATA) instanceof String data)) throw invalidFieldException(DATA,STRING);
return new Attachment(name,mime, BASE64.decode(data));
}
}

View File

@@ -0,0 +1,66 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.json.JSONArray;
import org.json.JSONObject;
public class Envelope {
private Message message;
private Set<User> receivers;
public Envelope(Message message, User receiver){
this(message,new HashSet<>(Set.of(receiver)));
}
public Envelope(Message message, HashSet<User> receivers) {
this.message = message;
this.receivers = receivers;
}
/**
* TODO: this is legacy, move to legacy module!
* @param json
* @return
* @throws UmbrellaException
*/
public static Envelope from(JSONObject json) throws UmbrellaException {
if (!json.has(RECEIVERS)) throw missingFieldException(RECEIVERS);
var message = Message.from(json);
var obj = json.get(RECEIVERS);
if (obj instanceof JSONObject) obj = new JSONArray(List.of(obj));
if (!(obj instanceof JSONArray receiverList)) throw invalidFieldException(RECEIVERS, JSONARRAY);
var receivers = new HashSet<User>();
for (var o : receiverList){
if (!(o instanceof JSONObject receiverData)) throw invalidFieldException("entries of "+ RECEIVERS, JSONOBJECT);
receivers.add(User.of(receiverData));
}
return new Envelope(message,receivers);
}
public boolean isFor(User receiver) {
return receivers.contains(receiver);
}
public Message message(){
return message;
}
public Set<User> receivers(){
return receivers;
}
@Override
public String toString() {
return format("{0} (to: {1}), subject: {2}",getClass().getSimpleName(),receivers.stream().map(User::email).map(EmailAddress::toString).collect(Collectors.joining(", ")),message.subject());
}
}

View File

@@ -0,0 +1,56 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.tools.Optionals.isSet;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.*;
import org.json.JSONArray;
import org.json.JSONObject;
public record Message(UmbrellaUser sender, String subject, String body, Map<String,String> fills, List<Attachment> attachments) {
@Override
public boolean equals(Object o) {
if (!(o instanceof Message message)) return false;
return Objects.equals(sender, message.sender) && Objects.equals(subject, message.subject) && Objects.equals(body, message.body) && Objects.equals(attachments, message.attachments);
}
public static Message from(JSONObject json) throws UmbrellaException {
for (var key : Set.of(SENDER, SUBJECT, BODY)) {
if (!json.has(key)) throw missingFieldException(key);
}
if (!(json.get(SENDER) instanceof JSONObject senderObject)) throw invalidFieldException(SENDER, JSONOBJECT);
if (!(json.get(SUBJECT) instanceof String subject && isSet(subject))) throw invalidFieldException(SUBJECT,STRING);
if (!(json.get(BODY) instanceof String body && isSet(body))) throw invalidFieldException(BODY,STRING);
var user = UmbrellaUser.of(senderObject);
if (!(user instanceof UmbrellaUser sender)) throw new UmbrellaException(400,"Sender is not an umbrella user!");
var attachments = new ArrayList<Attachment>();
if (json.has(ATTACHMENTS)){
var jsonAttachments = json.get(ATTACHMENTS);
if (jsonAttachments instanceof JSONObject obj) jsonAttachments = new JSONArray(List.of(obj));
if (jsonAttachments instanceof JSONArray arr){
for (var att : arr){
if (!(att instanceof JSONObject o)) throw new UmbrellaException(400,"Attachments contains entry that is not an object: {}",att);
var attachment = Attachment.of(o);
attachments.add(attachment);
}
}
}
return new Message(sender,subject,body,null, attachments);
}
@Override
public int hashCode() {
return Objects.hash(subject, body, attachments);
}
@Override
public String toString() {
return format("{0}(from: {1}), subject: {2}",getClass().getSimpleName(),sender,subject);
}
}

View File

@@ -3,72 +3,43 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.json.JSONObject;
/* © SRSoftware 2025 */
public class UmbrellaUser implements Mappable {
public class UmbrellaUser extends User implements Mappable {
private final long id;
private final String theme, name, lang;
private final EmailAddress email;
private final String theme;
public UmbrellaUser(long id, String name, EmailAddress email, String theme, String languageCode) {
this.email = email;
this.name = name;
super(name, email, languageCode);
this.id = id;
this.theme = theme;
this.lang = languageCode;
}
public EmailAddress email(){
return email;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof UmbrellaUser user)) return false;
return Objects.equals(email, user.email)
&& Objects.equals(name, user.name)
return Objects.equals(email(), user.email())
&& Objects.equals(name(), user.name())
&& Objects.equals(id, user.id)
&& Objects.equals(lang, user.lang);
&& Objects.equals(language(), user.language());
}
@Override
public int hashCode() {
return Objects.hash(email, id, lang, name);
return Objects.hash(email(), id, language(), name());
}
public long id(){
return id;
}
public String language(){
return lang;
}
public String name(){
return name;
}
public static UmbrellaUser of(JSONObject json) throws UmbrellaException {
if (json.has(USER)) json = json.getJSONObject(USER);
if (!json.has(ID)) throw missingFieldException(ID);
var id = json.getLong(ID);
var name = json.has(NAME) ? json.getString(NAME) : null;
var email = json.has(EMAIL) ? new EmailAddress(json.getString(EMAIL)) : null;
var theme = json.has(THEME) ? json.getString(THEME) : null;
var lang = json.has(LANGUAGE) ? json.getString(LANGUAGE) : null;
return new UmbrellaUser(id, name, email, theme, lang);
}
public String theme(){
return theme;
}
@@ -81,7 +52,7 @@ public class UmbrellaUser implements Mappable {
map.put(NAME, name());
map.put(EMAIL,email() instanceof EmailAddress ea ? ea.toString() : null);
map.put(THEME,theme);
map.put(LANGUAGE,lang);
map.put(LANGUAGE,language());
return map;
}
}

View File

@@ -0,0 +1,52 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.EMAIL;
import static de.srsoftware.umbrella.core.Constants.LANGUAGE;
import static de.srsoftware.umbrella.core.Constants.NAME;
import static de.srsoftware.umbrella.core.Constants.THEME;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import org.json.JSONObject;
public class User {
private String lang, name;
private EmailAddress email;
public User(String name, EmailAddress email, String languageCode){
lang = languageCode;
this.name = name;
this.email = email;
}
public EmailAddress email(){
return email;
}
public String language(){
return lang;
}
public String name(){
return name;
}
public static User of(JSONObject json) throws UmbrellaException {
if (json.has(USER)) json = json.getJSONObject(USER);
if (!(json.has(NAME) && json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!(json.has(EMAIL) && json.get(EMAIL) instanceof String email)) throw missingFieldException(EMAIL);
if (!(json.has(LANGUAGE) && json.get(LANGUAGE) instanceof String lang)) throw missingFieldException(LANGUAGE);
var addr = new EmailAddress(email);
if (json.has(ID) && json.get(ID) instanceof Number id) {
var theme = json.has(THEME) ? json.getString(THEME) : null;
return new UmbrellaUser(id.longValue(), name, addr, theme, lang);
}
return new User(name,addr,lang);
}
}

View File

@@ -1,12 +1,12 @@
import de.srsoftware.umbrella.core.api.Translator;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import java.util.Map;
/* © SRSoftware 2025 */
import static java.text.MessageFormat.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import de.srsoftware.umbrella.core.api.Translator;
import java.util.Map;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
public class TranslatorTest {
private static final Translator translator = new Translator() {