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