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