major refactoring: working towards more interface-implementation splitting
This commit is contained in:
@@ -5,6 +5,7 @@ import static de.srsoftware.tools.Optionals.nullable;
|
||||
import static java.lang.System.Logger.Level.DEBUG;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.net.HttpURLConnection.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.tools.Path;
|
||||
@@ -73,4 +74,8 @@ public abstract class BaseHandler extends PathHandler {
|
||||
public boolean unauthorized(HttpExchange ex) throws IOException {
|
||||
return sendEmptyResponse(HTTP_FORBIDDEN,ex);
|
||||
}
|
||||
|
||||
public boolean notImplemented(HttpExchange ex,String message,Object clazz) throws IOException{
|
||||
return sendContent(ex,HTTP_NOT_IMPLEMENTED,format(message,clazz.getClass().getSimpleName()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,17 @@ import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import org.sqlite.SQLiteDataSource;
|
||||
|
||||
public class ConnectionProvider extends HashMap<File, Connection> {
|
||||
public Connection get(Object o) {
|
||||
public class ConnectionProvider {
|
||||
|
||||
private static final HashMap<File, Connection> connections = new HashMap<>();
|
||||
|
||||
private ConnectionProvider(){}
|
||||
|
||||
public static Connection connect(Object o) {
|
||||
if (o instanceof String filename) o = new File(filename);
|
||||
if (o instanceof File dbFile) try {
|
||||
var conn = super.get(dbFile);
|
||||
if (conn == null) put(dbFile, conn = open(dbFile));
|
||||
var conn = connections.get(dbFile);
|
||||
if (conn == null) connections.put(dbFile, conn = open(dbFile));
|
||||
return conn;
|
||||
} catch (SQLException sqle) {
|
||||
throw new RuntimeException(sqle);
|
||||
@@ -20,7 +25,7 @@ public class ConnectionProvider extends HashMap<File, Connection> {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Connection open(File dbFile) throws SQLException {
|
||||
private static Connection open(File dbFile) throws SQLException {
|
||||
SQLiteDataSource dataSource = new SQLiteDataSource();
|
||||
dataSource.setUrl("jdbc:sqlite:%s".formatted(dbFile));
|
||||
return dataSource.getConnection();
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
|
||||
public class UmbrellaException extends Exception{
|
||||
private final int statusCode;
|
||||
|
||||
public UmbrellaException(int statusCode, String message){
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
public UmbrellaException(String message, Object ... fills){
|
||||
this(HTTP_SERVER_ERROR,message,fills);
|
||||
}
|
||||
|
||||
public UmbrellaException(int statusCode, String message, Object ... fills){
|
||||
this(statusCode,format(message,fills));
|
||||
super(format(message,fills));
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
public UmbrellaException causedBy(Exception e) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.util.List;
|
||||
|
||||
public interface CompanyService {
|
||||
public List<UmbrellaUser> getMembers(long companyId);
|
||||
}
|
||||
@@ -2,12 +2,13 @@
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.umbrella.core.Token;
|
||||
import de.srsoftware.umbrella.core.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserHelper {
|
||||
public interface UserService {
|
||||
UmbrellaUser loadUser(long userId) throws UmbrellaException;
|
||||
Optional<UmbrellaUser> loadUser(Optional<Token> sessionToken) throws UmbrellaException;
|
||||
Optional<UmbrellaUser> loadUser(HttpExchange ex) throws UmbrellaException;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import java.time.Instant;
|
||||
|
||||
/* © SRSoftware 2025 */
|
||||
public record Session(UmbrellaUser user, Token token, Instant expiration) {
|
||||
public Session extended(Instant newExpiration) {
|
||||
return new Session(user,token,newExpiration);
|
||||
}
|
||||
|
||||
public SessionToken cookie() {
|
||||
return new SessionToken(token.toString(),"/",expiration,true);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.core;
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.TOKEN;
|
||||
|
||||
import de.srsoftware.tools.SessionToken;
|
||||
import de.srsoftware.umbrella.core.AddableMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Token implements CharSequence{
|
||||
Reference in New Issue
Block a user