implemented cookies, implemented local file delivery option (--base /path/to/static/content), refactoring static files
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
19
de.srsoftware.cookies/build.gradle
Normal file
19
de.srsoftware.cookies/build.gradle
Normal file
@@ -0,0 +1,19 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'de.srsoftware'
|
||||
version = '1.0-SNAPSHOT'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cookies;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class Cookie implements Map.Entry<String, String> {
|
||||
private final String key;
|
||||
private String value = null;
|
||||
|
||||
Cookie(String key, String value) {
|
||||
this.key = key;
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
public <T extends Cookie> T addTo(Headers headers) {
|
||||
headers.add("Set-Cookie", "%s=%s".formatted(key, value));
|
||||
return (T)this;
|
||||
}
|
||||
|
||||
public <T extends Cookie> T addTo(HttpExchange ex) {
|
||||
return this.addTo(ex.getResponseHeaders());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
protected static Optional<List<String>> of(HttpExchange ex) {
|
||||
return Optional.ofNullable(ex.getRequestHeaders().get("Cookie"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setValue(String s) {
|
||||
var oldVal = value;
|
||||
value = s;
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.cookies;
|
||||
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SessionToken extends Cookie {
|
||||
private final String sessionId;
|
||||
|
||||
public SessionToken(String sessionId) {
|
||||
super("sessionToken", sessionId);
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
public static Optional<SessionToken> from(HttpExchange ex) {
|
||||
return Cookie.of(ex).orElseGet(List::of).stream().filter(cookie -> cookie.startsWith("sessionToken=")).map(cookie -> cookie.split("=", 2)[1]).map(id -> new SessionToken(id)).findAny();
|
||||
}
|
||||
|
||||
public String sessionId() {
|
||||
return sessionId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user