peer-at-code-backend/src/be/jeffcheasey88/peeratcode/webserver/Router.java
Yannick Bour cfe9d83d2c jwt login
2023-02-23 16:08:47 +01:00

37 lines
No EOL
880 B
Java

package be.jeffcheasey88.peeratcode.webserver;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
public class Router{
private List<Response> responses;
private Response noFileFound;
public Router(){
this.responses = new ArrayList<>();
}
public void register(Response response){
this.responses.add(response);
}
public void setDefault(Response response){
this.noFileFound = response;
}
public void exec(String type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception {
for(Response response : this.responses){
if(type.equals(response.getType())){
Matcher matcher = response.getPattern().matcher(path);
if(matcher.matches()){
response.exec(matcher, user, reader, writer);
return;
}
}
}
if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
}
}