Router as design pattern builder (without build method)

This commit is contained in:
jeffcheasey88 2023-07-26 16:03:15 +02:00
parent fb442f075b
commit dc4ccce385

View file

@ -25,7 +25,7 @@ import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder; import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException; import org.jose4j.lang.JoseException;
public class Router<U extends User>{ public class Router<U extends User>{l
private Map<RequestType, Map<Response, Route>> responses; private Map<RequestType, Map<Response, Route>> responses;
private Map<Response, Pattern> patterns; private Map<Response, Pattern> patterns;
@ -89,7 +89,7 @@ public class Router<U extends User>{
} }
} }
public void register(Response response){ public Router<U> register(Response response){
try{ try{
Method method = response.getClass().getDeclaredMethod("exec", Method method = response.getClass().getDeclaredMethod("exec",
Response.class.getDeclaredMethods()[0].getParameterTypes()); Response.class.getDeclaredMethods()[0].getParameterTypes());
@ -100,13 +100,15 @@ public class Router<U extends User>{
}catch(Exception e){ }catch(Exception e){
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
return this;
} }
public void setDefault(Response response){ public Router<U> setDefault(Response response){
this.noFileFound = response; this.noFileFound = response;
return this;
} }
public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{ void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
if(type == null) return; if(type == null) return;
for(Entry<Response, Route> routes : this.responses.get(type).entrySet()){ for(Entry<Response, Route> routes : this.responses.get(type).entrySet()){
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path); Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
@ -127,9 +129,10 @@ public class Router<U extends User>{
if(noFileFound != null) noFileFound.exec(null, user, reader, writer); if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
} }
public void configureSSL(String keyStore, String keyStorePassword){ public Router<U> configureSSL(String keyStore, String keyStorePassword){
System.setProperty("javax.net.ssl.keyStore", keyStore); System.setProperty("javax.net.ssl.keyStore", keyStore);
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword); System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
return this;
} }
public U getUser(String token) throws Exception{ public U getUser(String token) throws Exception{