Add logger (for route calling)
This commit is contained in:
parent
d8e5948fa2
commit
ab33fd465e
3 changed files with 34 additions and 7 deletions
|
@ -8,26 +8,30 @@ public class Client<U extends User> extends Thread{
|
|||
private HttpReader reader;
|
||||
private HttpWriter writer;
|
||||
private Router<U> router;
|
||||
private Locker<Context> logger;
|
||||
|
||||
public Client(Socket socket, Router<U> router) throws Exception{
|
||||
this.reader = new HttpReader(socket);
|
||||
this.writer = new HttpWriter(socket);
|
||||
this.router = router;
|
||||
this.logger = router.getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
Context context = null;
|
||||
try{
|
||||
String[] headers = reader.readLine().split("\\s");
|
||||
System.out.println(Arrays.toString(headers));
|
||||
reader.readHeaders();
|
||||
|
||||
router.exec(RequestType.valueOf(headers[0]), headers[1], isLogin(reader), reader, writer);
|
||||
context = router.exec(RequestType.valueOf(headers[0]), headers[1], isLogin(reader), reader, writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(context != null) logger.setValue(context);
|
||||
}
|
||||
|
||||
private User isLogin(HttpReader reader) throws Exception{
|
||||
|
|
|
@ -2,17 +2,33 @@ package dev.peerat.framework;
|
|||
|
||||
public class Context{
|
||||
|
||||
private RequestType type;
|
||||
private String path;
|
||||
private User user;
|
||||
private int responseCode;
|
||||
private HttpWriter writer;
|
||||
private String[] headers;
|
||||
|
||||
public Context(User user, HttpWriter writer, String... headers){
|
||||
public Context(RequestType type, String path, User user, HttpWriter writer, String... headers){
|
||||
this.type = type;
|
||||
this.path = path;
|
||||
this.user = user;
|
||||
this.writer = writer;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public RequestType getType(){
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public String getPath(){
|
||||
return this.path;
|
||||
}
|
||||
|
||||
public boolean isLogged(){
|
||||
return user != null;
|
||||
}
|
||||
|
||||
public <U extends User> U getUser(){
|
||||
return (U) user;
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ public class Router<U extends User>{
|
|||
|
||||
}
|
||||
|
||||
private Locker<Context> logger;
|
||||
private Map<Response, Route>[] responses;
|
||||
private Map<Response, Pattern> patterns;
|
||||
private Response noFileFound;
|
||||
|
@ -42,6 +43,7 @@ public class Router<U extends User>{
|
|||
private ServerSocket serverSocket;
|
||||
|
||||
public Router() throws Exception{
|
||||
this.logger = new Locker<>();
|
||||
int types = RequestType.values().length;
|
||||
this.responses = new HashMap[types];
|
||||
for(RequestType type : RequestType.values()) this.responses[type.ordinal()] = new HashMap<>();
|
||||
|
@ -136,15 +138,15 @@ public class Router<U extends User>{
|
|||
return this;
|
||||
}
|
||||
|
||||
void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(type == null) return;
|
||||
Context context = new Context(user, writer, this.headers[type.ordinal()]);
|
||||
Context exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(type == null) return null;
|
||||
Context context = new Context(type, path, user, writer, this.headers[type.ordinal()]);
|
||||
for(Entry<Response, Route> routes : this.responses[type.ordinal()].entrySet()){
|
||||
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
||||
if(matcher.matches()){
|
||||
if(user == null && routes.getValue().needLogin()){
|
||||
writer.response(401, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
return context;
|
||||
}
|
||||
if(routes.getValue().websocket()){
|
||||
switchToWebSocket(reader, writer);
|
||||
|
@ -152,10 +154,11 @@ public class Router<U extends User>{
|
|||
writer = new WebSocketWriter(writer);
|
||||
}
|
||||
routes.getKey().exec(matcher, context, reader, writer);
|
||||
return;
|
||||
return context;
|
||||
}
|
||||
}
|
||||
if(noFileFound != null) noFileFound.exec(null, context, reader, writer);
|
||||
return context;
|
||||
}
|
||||
|
||||
public Router<U> configureSSL(String keyStore, String keyStorePassword){
|
||||
|
@ -185,6 +188,10 @@ public class Router<U extends User>{
|
|||
return jws.getCompactSerialization();
|
||||
}
|
||||
|
||||
public Locker<Context> getLogger(){
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
private void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{
|
||||
String key = reader.getHeader("Sec-WebSocket-Key");
|
||||
if (key == null) throw new IllegalArgumentException();
|
||||
|
|
Loading…
Add table
Reference in a new issue