conveniance modification: added registry and getters for modules to BaseHandler

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-08-17 23:31:06 +02:00
parent ecb1ddac0f
commit a50a451b95
21 changed files with 260 additions and 198 deletions

View File

@@ -9,6 +9,7 @@ import static java.net.HttpURLConnection.*;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.tools.Path;
import de.srsoftware.tools.PathHandler;
import de.srsoftware.umbrella.core.api.*;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -16,8 +17,14 @@ import java.util.List;
public abstract class BaseHandler extends PathHandler {
private final ModuleRegistry registry;
public record Page(String mime, byte[] bytes){}
public BaseHandler(ModuleRegistry registry){
this.registry = registry.add(this);
}
public HttpExchange addCors(HttpExchange ex){
var headers = ex.getRequestHeaders();
var origin = nullable(headers.get("Origin")).orElse(List.of()).stream().filter(url -> url.contains("://localhost")||url.contains("://127.0.0.1")).findAny();
@@ -34,11 +41,23 @@ public abstract class BaseHandler extends PathHandler {
return ex;
}
public CompanyService companyService(){
return registry.companyService();
}
public DocumentService documentService(){
return registry.documentService();
}
@Override
public boolean doOptions(Path path, HttpExchange ex) throws IOException {
return ok(addCors(ex));
}
public ItemService itemService(){
return registry.itemService();
}
public boolean load(Path path, HttpExchange ex) throws IOException {
try {
var doc = load(path.toString());
@@ -65,17 +84,43 @@ public abstract class BaseHandler extends PathHandler {
}
}
public NoteService noteService(){
return registry.noteService();
}
public boolean ok(HttpExchange ex) throws IOException {
return sendEmptyResponse(HTTP_OK,ex);
}
public PostBox postBox() {
return registry.postBox();
}
public ProjectService projectService(){
return registry.projectService();
}
public boolean send(HttpExchange ex, UmbrellaException e) throws IOException {
return sendContent(ex,e.statusCode(),e.getMessage());
}
public TagService tagService(){
return registry.tagService();
}
public TaskService taskService(){
return registry.taskService();
}
public Translator translator(){
return registry.translator();
}
public boolean unauthorized(HttpExchange ex) throws IOException {
return sendEmptyResponse(HTTP_UNAUTHORIZED,ex);
}
public UserService userService(){
return registry.userService();
}
}

View File

@@ -1,86 +1,47 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.api.*;
public class ModuleRegistry {
private Translator translator;
private PostBox postBox;
private UserService userService;
private TagService tagService;
private BookmarkService bookmarkService;
private CompanyService companyService;
private DocumentService documentService;
private ItemService itemService;
private MarkdownService markdownService;
private NoteService noteService;
private PostBox postBox;
private ProjectService projectService;
private TagService tagService;
private TaskService taskService;
private TimeService timeService;
private Translator translator;
private UserService userService;
public ModuleRegistry add(BookmarkService bookmarkService) {
this.bookmarkService = bookmarkService;
public ModuleRegistry add(Object service) {
switch (service) {
case BookmarkService bs: bookmarkService = bs; break;
case CompanyService cs: companyService = cs; break;
case DocumentService ds: documentService = ds; break;
case ItemService is: itemService = is; break;
case MarkdownService ms: markdownService = ms; break;
case NoteService ns: noteService = ns; break;
case PostBox pb: postBox = pb; break;
case ProjectService ps: projectService = ps; break;
case TagService ts: tagService = ts; break;
case TaskService ts: taskService = ts; break;
case TimeService ts: timeService = ts; break;
case Translator tr: translator = tr; break;
case UserService us: userService = us; break;
default: throw new RuntimeException(format("Trying to add unknown service ({0}) to {1}",service.getClass().getSimpleName(),getClass().getSimpleName()));
}
return this;
}
public ModuleRegistry add(CompanyService companyService) {
this.companyService = companyService;
return this;
}
public ModuleRegistry add(DocumentService documentService) {
this.documentService = documentService;
return this;
}
public ModuleRegistry add(ItemService itemService) {
this.itemService = itemService;
return this;
}
public ModuleRegistry add(MarkdownService markdownService) {
this.markdownService = markdownService;
return this;
}
public ModuleRegistry add(NoteService noteService) {
this.noteService = noteService;
return this;
}
public ModuleRegistry add(PostBox postBox) {
this.postBox = postBox;
return this;
}
public ModuleRegistry add(ProjectService projectService) {
this.projectService = projectService;
return this;
}
public ModuleRegistry add(TagService tagService) {
this.tagService = tagService;
return this;
}
public ModuleRegistry add(TaskService taskService) {
this.taskService = taskService;
return this;
}
public ModuleRegistry add(TimeService timeService) {
this.timeService = timeService;
return this;
}
public ModuleRegistry add(Translator translator) {
this.translator = translator;
return this;
}
public ModuleRegistry add(UserService userService) {
this.userService = userService;
return this;
public BookmarkService bookmarkService(){
return bookmarkService;
}
public CompanyService companyService(){
@@ -95,6 +56,10 @@ public class ModuleRegistry {
return itemService;
}
public MarkdownService markdownService(){
return markdownService;
}
public NoteService noteService(){
return noteService;
}
@@ -115,6 +80,10 @@ public class ModuleRegistry {
return taskService;
}
public TimeService timeService(){
return timeService;
}
public Translator translator(){
return translator;
}