implemented legacy module for company

This commit is contained in:
2025-08-13 15:42:53 +02:00
parent 89758f6c21
commit 6b2a437539
2 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
/* © SRSoftware 2025 */
package de.srsoftware.umbrella.legacy;
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;
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;
public class CompanyLegacy extends BaseHandler {
private final ModuleRegistry registry;
private final Configuration config;
public CompanyLegacy(ModuleRegistry registry, Configuration config) {
this.registry = 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 = registry.userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
return switch (path.pop()){
case JSON -> postCompanyJson(ex,params,user.get());
default -> super.doPost(path, ex);
};
}
private boolean postCompanyJson(HttpExchange ex, Map<String, Object> params, UmbrellaUser user) throws IOException {
var companies = registry.companyService().listCompaniesOf(user);
return sendContent(ex, mapValues(companies));
}
}