Compare commits
2 commits
12f2561921
...
9cedf6898f
Author | SHA1 | Date | |
---|---|---|---|
|
9cedf6898f | ||
|
cdafc9cbaf |
8 changed files with 69 additions and 53 deletions
|
@ -1,12 +1,9 @@
|
||||||
package dev.peerat.framework;
|
package dev.peerat.framework;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
|
@ -5,6 +5,6 @@ import java.util.regex.Matcher;
|
||||||
|
|
||||||
public interface RouteInterceptor{
|
public interface RouteInterceptor{
|
||||||
|
|
||||||
boolean intercept(Matcher matcher, Context context, HttpReader reader, HttpWriter writer, Method method);
|
boolean intercept(Matcher matcher, Context context, HttpReader reader, HttpWriter writer, Method method, Object[] params);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,16 @@ public class RouteMapper<U extends User>{
|
||||||
if(this.calls != null) this.calls = new int[length];
|
if(this.calls != null) this.calls = new int[length];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void bind(RouteFilter filter, Class<?>... types){
|
||||||
|
for(RouteState state : this.states){
|
||||||
|
if(filter.accept(state.getMethod(), state.getRoute(), state.getPattern())) state.getBinder().bind(types);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bindAll(){
|
||||||
|
for(RouteState state : this.states) state.getBinder().bindIndexes(state.getMethod());
|
||||||
|
}
|
||||||
|
|
||||||
public void activeReOrdering(){
|
public void activeReOrdering(){
|
||||||
this.calls = new int[states.length];
|
this.calls = new int[states.length];
|
||||||
this.dif = 2;
|
this.dif = 2;
|
||||||
|
@ -63,10 +73,12 @@ public class RouteMapper<U extends User>{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(target != null){
|
if(target != null){
|
||||||
|
Object[] params = new Object[target.getBinder().getParameterCount()];
|
||||||
for(RouteInterceptor interceptor : interceptors){
|
for(RouteInterceptor interceptor : interceptors){
|
||||||
if(!interceptor.intercept(matcher, context, reader, writer, target.getMethod())) return true;
|
if(!interceptor.intercept(matcher, context, reader, writer, target.getMethod(), params)) return true;
|
||||||
}
|
}
|
||||||
target.getMethod().invoke(target.getInstance(), target.bindMethod(matcher, context, reader, writer));
|
Object[] inv = target.getBinder().bindMethod(target.getMethod(), matcher, context, reader, writer, params);
|
||||||
|
target.getMethod().invoke(target.getInstance(), inv);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -10,20 +10,18 @@ public class RouteState {
|
||||||
private Method method;
|
private Method method;
|
||||||
private Route route;
|
private Route route;
|
||||||
private Pattern pattern;
|
private Pattern pattern;
|
||||||
private int[] bindMethod;
|
private RouteBinder binder;
|
||||||
|
|
||||||
public RouteState(Object instance, Method method, Route route){
|
public RouteState(Object instance, Method method, Route route){
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.route = route;
|
this.route = route;
|
||||||
this.pattern = Pattern.compile(route.path());
|
this.pattern = Pattern.compile(route.path());
|
||||||
|
this.binder = new RouteBinder();
|
||||||
|
}
|
||||||
|
|
||||||
Class<?>[] parameters = method.getParameterTypes();
|
public void bindIndexes(){
|
||||||
this.bindMethod = new int[4];
|
this.binder.bindIndexes(this.method);
|
||||||
this.bindMethod[0] = findIndex(parameters, Matcher.class);
|
|
||||||
this.bindMethod[1] = findIndex(parameters, Context.class);
|
|
||||||
this.bindMethod[2] = findIndex(parameters, HttpReader.class);
|
|
||||||
this.bindMethod[3] = findIndex(parameters, HttpWriter.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Matcher match(String path){
|
public Matcher match(String path){
|
||||||
|
@ -31,12 +29,16 @@ public class RouteState {
|
||||||
return matcher.matches() ? matcher : null;
|
return matcher.matches() ? matcher : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Route getRoute(){
|
||||||
|
return this.route;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isWebSocket(){
|
public boolean isWebSocket(){
|
||||||
return route.websocket();
|
return this.route.websocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean needLogin(){
|
public boolean needLogin(){
|
||||||
return route.needLogin();
|
return this.route.needLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Method getMethod(){
|
public Method getMethod(){
|
||||||
|
@ -47,26 +49,12 @@ public class RouteState {
|
||||||
return this.instance;
|
return this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object[] bindMethod(Matcher matcher, Context context, HttpReader reader, HttpWriter writer){
|
public Pattern getPattern(){
|
||||||
Object[] result = new Object[method.getParameterCount()];
|
return this.pattern;
|
||||||
setElement(result, bindMethod[0], matcher);
|
|
||||||
setElement(result, bindMethod[1], context);
|
|
||||||
setElement(result, bindMethod[2], reader);
|
|
||||||
setElement(result, bindMethod[3], writer);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setElement(Object[] array, int position, Object element){
|
public RouteBinder getBinder(){
|
||||||
if(position < 0) return;
|
return this.binder;
|
||||||
array[position] = element;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int findIndex(Class<?>[] array, Class<?> type){
|
|
||||||
for(int i = 0; i < array.length; i++){
|
|
||||||
Class<?> clazz = array[i];
|
|
||||||
if(type.isAssignableFrom(clazz)) return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
package dev.peerat.framework;
|
package dev.peerat.framework;
|
||||||
|
|
||||||
import static dev.peerat.framework.RequestType.OPTIONS;
|
|
||||||
import static dev.peerat.framework.RequestType.POST;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
@ -14,7 +9,6 @@ import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import javax.net.ssl.SSLServerSocket;
|
import javax.net.ssl.SSLServerSocket;
|
||||||
import javax.net.ssl.SSLServerSocketFactory;
|
import javax.net.ssl.SSLServerSocketFactory;
|
||||||
|
@ -141,6 +135,15 @@ public class Router<U extends User>{
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Router<U> bind(RouteFilter filter, Class<?>... types){
|
||||||
|
for(RouteMapper<U> mapper : this.mappers) mapper.bind(filter, types);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindAll(){
|
||||||
|
for(RouteMapper<U> mapper : this.mappers) mapper.bindAll();
|
||||||
|
}
|
||||||
|
|
||||||
public Router<U> activeReOrdering(){
|
public Router<U> activeReOrdering(){
|
||||||
for(RouteMapper<?> mapper : this.mappers) mapper.activeReOrdering();
|
for(RouteMapper<?> mapper : this.mappers) mapper.activeReOrdering();
|
||||||
return this;
|
return this;
|
||||||
|
@ -186,6 +189,7 @@ public class Router<U extends User>{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void listen(int port, boolean ssl) throws Exception{
|
public void listen(int port, boolean ssl) throws Exception{
|
||||||
|
bindAll();
|
||||||
if (ssl) { // Not needed with the use of a proxy
|
if (ssl) { // Not needed with the use of a proxy
|
||||||
try {
|
try {
|
||||||
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
|
||||||
|
|
|
@ -3,16 +3,25 @@ package dev.peerat.framework.utils.json;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class JsonArray extends Json{
|
public class JsonArray extends Json{
|
||||||
|
|
||||||
private List<Object> list;
|
private Collection<Object> list;
|
||||||
|
|
||||||
public JsonArray(){
|
public JsonArray(){
|
||||||
this.list = new ArrayList<>();
|
this.list = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonArray(Collection<Object> collection){
|
||||||
|
this();
|
||||||
|
this.list.addAll(collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonArray(Iterator<Object> iterator){
|
||||||
|
this();
|
||||||
|
while(iterator.hasNext()) this.list.add(iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
public <E> Collection<E> toList(){
|
public <E> Collection<E> toList(){
|
||||||
return (Collection<E>) this.list;
|
return (Collection<E>) this.list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,11 @@ public class JsonMap extends Json{
|
||||||
this.map = new HashMap<>();
|
this.map = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonMap(Map<String, Object> map){
|
||||||
|
this();
|
||||||
|
for(Entry<String, Object> entry : map.entrySet()) this.map.put(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
public Set<Entry<String, Object>> entries(){
|
public Set<Entry<String, Object>> entries(){
|
||||||
return this.map.entrySet();
|
return this.map.entrySet();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package dev.peerat.framework.utils.json;
|
package dev.peerat.framework.utils.json;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.parser.TokenType.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -8,7 +10,6 @@ import java.util.Map.Entry;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.Parser;
|
import be.jeffcheasey88.peeratcode.parser.Parser;
|
||||||
import be.jeffcheasey88.peeratcode.parser.Token;
|
import be.jeffcheasey88.peeratcode.parser.Token;
|
||||||
import be.jeffcheasey88.peeratcode.parser.TokenType;
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
|
import be.jeffcheasey88.peeratcode.parser.state.RedirectStateTree;
|
||||||
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
|
import be.jeffcheasey88.peeratcode.parser.state.StateTree;
|
||||||
|
|
||||||
|
@ -43,16 +44,16 @@ public class JsonParser extends Parser<Json>{
|
||||||
.then(content_array_element);
|
.then(content_array_element);
|
||||||
|
|
||||||
content.then(new RedirectStateTree<>(base, (global, local) -> global.set(local.get()))).end();
|
content.then(new RedirectStateTree<>(base, (global, local) -> global.set(local.get()))).end();
|
||||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.STRING), (bag, token) -> bag.set(token))).end();
|
content.then((validator) -> validator.validate((token) -> token.getType().equals(STRING), (bag, token) -> bag.set(token))).end();
|
||||||
content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.CHAR), (bag, token) -> bag.set(token))).end();
|
content.then((validator) -> validator.validate((token) -> token.getType().equals(CHAR), (bag, token) -> bag.set(token))).end();
|
||||||
StateTree<Json> number = content.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set(token)));
|
StateTree<Json> number = content.then((validator) -> validator.validate((token) -> token.getType().equals(NAME), (bag, token) -> bag.set(token)));
|
||||||
number.end();
|
number.end();
|
||||||
number.then((validator) -> validator.validate((token) -> token.getValue().equals("."), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
number.then((validator) -> validator.validate((token) -> token.getValue().equals("."), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
||||||
.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.NAME), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
.then((validator) -> validator.validate((token) -> token.getType().equals(NAME), (bag, token) -> bag.set(bag.<Token>get().concat(token))))
|
||||||
.end();
|
.end();
|
||||||
|
|
||||||
StateTree<Json> mapper = new StateTree<>();
|
StateTree<Json> mapper = new StateTree<>();
|
||||||
StateTree<Json> mapper_key = mapper.then((validator) -> validator.validate((token) -> token.getType().equals(TokenType.STRING), (bag, token) -> {
|
StateTree<Json> mapper_key = mapper.then((validator) -> validator.validate((token) -> token.getType().equals(STRING), (bag, token) -> {
|
||||||
Map<Token, Object> map = bag.get("map");
|
Map<Token, Object> map = bag.get("map");
|
||||||
if(map == null){
|
if(map == null){
|
||||||
map = new HashMap<>();
|
map = new HashMap<>();
|
||||||
|
@ -105,20 +106,20 @@ public class JsonParser extends Parser<Json>{
|
||||||
if(value instanceof Token){
|
if(value instanceof Token){
|
||||||
Token token = (Token) value;
|
Token token = (Token) value;
|
||||||
String content = token.getValue();
|
String content = token.getValue();
|
||||||
if(token.getType().equals(TokenType.STRING)){
|
if(token.getType().equals(STRING)){
|
||||||
return content;
|
return content;
|
||||||
}else if(token.getType().equals(TokenType.CHAR)){
|
}else if(token.getType().equals(CHAR)){
|
||||||
return content.charAt(0);
|
return content.charAt(0);
|
||||||
}else{
|
}else{
|
||||||
try{
|
try{
|
||||||
return Long.parseLong(content);
|
return Long.parseLong(content);
|
||||||
}catch(Exception _){
|
}catch(Exception e){
|
||||||
try {
|
try {
|
||||||
return Double.parseDouble(content);
|
return Double.parseDouble(content);
|
||||||
}catch(Exception __){
|
}catch(Exception ex){
|
||||||
try{
|
try{
|
||||||
return Boolean.parseBoolean(content);
|
return Boolean.parseBoolean(content);
|
||||||
}catch(Exception ___){}
|
}catch(Exception exc){}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue