conveniance modification: added registry and getters for modules to BaseHandler
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -20,11 +20,10 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CompanyLegacy extends BaseHandler {
|
||||
private final ModuleRegistry registry;
|
||||
private final Configuration config;
|
||||
|
||||
public CompanyLegacy(ModuleRegistry registry, Configuration config) {
|
||||
this.registry = registry;
|
||||
super(registry);
|
||||
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules"));
|
||||
}
|
||||
|
||||
@@ -55,7 +54,7 @@ public class CompanyLegacy extends BaseHandler {
|
||||
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
if (token.isEmpty()) token = nullable(params.get(TOKEN)).map(Object::toString).map(Token::of);
|
||||
var user = registry.userService().loadUser(token);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
|
||||
return switch (path.pop()){
|
||||
@@ -65,7 +64,7 @@ public class CompanyLegacy extends BaseHandler {
|
||||
}
|
||||
|
||||
private boolean postCompanyJson(HttpExchange ex, Map<String, Object> params, UmbrellaUser user) throws IOException {
|
||||
var companies = registry.companyService().listCompaniesOf(user);
|
||||
var companies = companyService().listCompaniesOf(user);
|
||||
return sendContent(ex, mapValues(companies));
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,10 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class NotesLegacy extends BaseHandler {
|
||||
private final ModuleRegistry registry;
|
||||
private final Configuration config;
|
||||
|
||||
public NotesLegacy(ModuleRegistry registry, Configuration config) {
|
||||
this.registry = registry;
|
||||
super(registry);
|
||||
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules"));
|
||||
}
|
||||
|
||||
@@ -59,7 +58,7 @@ public class NotesLegacy extends BaseHandler {
|
||||
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
if (token.isEmpty()) token = nullable(params.get(TOKEN)).map(Object::toString).map(Token::of);
|
||||
var user = registry.userService().loadUser(token);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
|
||||
return switch (path.pop()){
|
||||
@@ -74,9 +73,9 @@ public class NotesLegacy extends BaseHandler {
|
||||
if (parts.length<2) throw invalidFieldException(URI,"URI of the form \"module:entry-id\"");
|
||||
var module = parts[0];
|
||||
var entryId = parts[1];
|
||||
var notes = registry.noteService().getNotes(module,entryId);
|
||||
var notes = noteService().getNotes(module,entryId);
|
||||
var authors = new HashMap<Long, UmbrellaUser> ();
|
||||
var users = registry.userService();
|
||||
var users = userService();
|
||||
for (var note : notes.values()) {
|
||||
var userId = note.authorId();
|
||||
authors.computeIfAbsent(userId,k -> users.loadUser(userId));
|
||||
|
||||
@@ -20,11 +20,10 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ProjectLegacy extends BaseHandler {
|
||||
private final ModuleRegistry registry;
|
||||
private final Configuration config;
|
||||
|
||||
public ProjectLegacy(ModuleRegistry registry, Configuration config) {
|
||||
this.registry = registry;
|
||||
super(registry);
|
||||
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules"));
|
||||
}
|
||||
|
||||
@@ -55,7 +54,7 @@ public class ProjectLegacy extends BaseHandler {
|
||||
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
if (token.isEmpty()) token = nullable(params.get(TOKEN)).map(Object::toString).map(Token::of);
|
||||
var user = registry.userService().loadUser(token);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
|
||||
return switch (path.pop()){
|
||||
@@ -66,7 +65,7 @@ public class ProjectLegacy extends BaseHandler {
|
||||
|
||||
private boolean postProjectJson(HttpExchange ex, Map<String, Object> params, UmbrellaUser user) throws IOException {
|
||||
var includeUsers = "1".equals(params.get(USERS));
|
||||
var projects = registry.projectService().listUserProjects(user.id(), false);
|
||||
var projects = projectService().listUserProjects(user.id(), false);
|
||||
return sendContent(ex, mapValues(projects));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.legacy;
|
||||
|
||||
|
||||
import static de.srsoftware.tools.Optionals.nullable;
|
||||
import static de.srsoftware.umbrella.core.Constants.TOKEN;
|
||||
import static de.srsoftware.umbrella.core.Constants.USERS;
|
||||
import static de.srsoftware.umbrella.core.Paths.JSON;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
|
||||
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.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TaskLegacy extends BaseHandler {
|
||||
private final Configuration config;
|
||||
|
||||
public TaskLegacy(ModuleRegistry registry, Configuration config) {
|
||||
super(registry);
|
||||
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doDelete(Path path, HttpExchange ex) throws IOException {
|
||||
return super.doDelete(path, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
if (path.empty()) return sendRedirect(ex, url(ex).replaceAll("/legacy/","/"));
|
||||
return super.doGet(path, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doOptions(Path path, HttpExchange ex) throws IOException {
|
||||
return super.doOptions(path, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||
return super.doPatch(path, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException{
|
||||
var params = formData(ex);
|
||||
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
if (token.isEmpty()) token = nullable(params.get(TOKEN)).map(Object::toString).map(Token::of);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
|
||||
return switch (path.pop()){
|
||||
case JSON -> postProjectJson(ex,params,user.get());
|
||||
default -> super.doPost(path, ex);
|
||||
};
|
||||
}
|
||||
|
||||
private boolean postProjectJson(HttpExchange ex, Map<String, Object> params, UmbrellaUser user) throws IOException {
|
||||
var includeUsers = "1".equals(params.get(USERS));
|
||||
var projects = projectService().listUserProjects(user.id(), false);
|
||||
return sendContent(ex, mapValues(projects));
|
||||
}
|
||||
}
|
||||
@@ -33,10 +33,9 @@ public class UserLegacy extends BaseHandler {
|
||||
|
||||
private final Configuration config;
|
||||
private final String messageUrl;
|
||||
private final ModuleRegistry registry;
|
||||
|
||||
public UserLegacy(ModuleRegistry registry, Configuration config) {
|
||||
this.registry = registry;
|
||||
super(registry);
|
||||
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules"));
|
||||
this.messageUrl = null;
|
||||
}
|
||||
@@ -128,7 +127,7 @@ public class UserLegacy extends BaseHandler {
|
||||
throw new UmbrellaException(400,"Fetching related users not implemented, yet!");
|
||||
}
|
||||
|
||||
Map<Long, UmbrellaUser> userMap = registry.userService().list(0, null, ids);
|
||||
Map<Long, UmbrellaUser> userMap = userService().list(0, null, ids);
|
||||
if (arrayPassed || userMap.size() != 1) {
|
||||
var userData = new HashMap<Long, Map<String, Object>>();
|
||||
for (var entry : userMap.entrySet()) userData.put(entry.getKey(),entry.getValue().toMap());
|
||||
@@ -180,7 +179,7 @@ public class UserLegacy extends BaseHandler {
|
||||
}
|
||||
}
|
||||
if (!recipients.isEmpty()){ // replace legacy user ids by user objects in receivers field
|
||||
Map<Long, UmbrellaUser> resp = registry.userService().list(0, null, recipients);
|
||||
Map<Long, UmbrellaUser> resp = userService().list(0, null, recipients);
|
||||
data.put("receivers",resp.values().stream().map(UmbrellaUser::toMap).toList());
|
||||
}
|
||||
|
||||
@@ -203,7 +202,7 @@ public class UserLegacy extends BaseHandler {
|
||||
var optToken = SessionToken.from(ex).map(Token::of);
|
||||
if (optToken.isPresent()) try{
|
||||
var token = optToken.get();
|
||||
registry.userService().dropSession(token);
|
||||
userService().dropSession(token);
|
||||
var expiredToken = new SessionToken(token.toString(),"/", Instant.now().minus(1, DAYS),true);
|
||||
expiredToken.addTo(ex);
|
||||
if (returnTo instanceof String location) return sendRedirect(ex,location);
|
||||
@@ -245,8 +244,8 @@ public class UserLegacy extends BaseHandler {
|
||||
};
|
||||
|
||||
protected Session requestSession(Token token) throws UmbrellaException {
|
||||
var session = registry.userService().load(token);
|
||||
session = registry.userService().extend(session);
|
||||
var session = userService().load(token);
|
||||
session = userService().extend(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
@@ -280,8 +279,8 @@ public class UserLegacy extends BaseHandler {
|
||||
|
||||
var o = map.get(TOKEN);
|
||||
if (!(o instanceof String token)) throw new UmbrellaException(500,"Request did not contain token!");
|
||||
var session = registry.userService().load(Token.of(token));
|
||||
var user = registry.userService().load(session);
|
||||
var session = userService().load(Token.of(token));
|
||||
var user = userService().load(session);
|
||||
var userMap = user.toMap();
|
||||
userMap.put(TOKEN,Map.of(TOKEN,token,EXPIRATION,session.expiration().getEpochSecond()));
|
||||
return sendContent(ex,userMap);
|
||||
|
||||
Reference in New Issue
Block a user