com.fastchar.core.FastRoute Maven / Gradle / Ivy
package com.fastchar.core;
import com.fastchar.annotation.AFastCheck;
import com.fastchar.asm.FastParameter;
import com.fastchar.exception.FastReturnException;
import com.fastchar.interfaces.IFastInterceptor;
import com.fastchar.interfaces.IFastRootInterceptor;
import com.fastchar.out.FastOut;
import com.fastchar.out.FastOutFile;
import com.fastchar.out.FastOutForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class FastRoute {
String route;
Method method;
boolean beforeInvoked;
boolean responseInvoked;
boolean afterInvoked;
boolean actionLog = true;
int priority;
int firstMethodLineNumber;
int lastMethodLineNumber;
Class returnOut;
List methodParameter = new ArrayList<>();
Class actionClass;
List> rootInterceptor = new ArrayList<>();
private List doBeforeInterceptor = new ArrayList<>();
private List doAfterInterceptor = new ArrayList<>();
transient FastAction fastAction;
private transient int doBeforeIndex = -1;
private transient int doAfterIndex = -1;
transient Date inTime;
transient List stackTraceElements = new ArrayList<>();
transient HttpServletRequest request;
transient HttpServletResponse response;
transient FastUrl fastUrl;
transient FastAction forwarder;
FastRoute copy() {
FastRoute fastRoute = new FastRoute();
fastRoute.method = this.method;
fastRoute.methodParameter = this.methodParameter;
fastRoute.priority = this.priority;
fastRoute.actionClass = this.actionClass;
fastRoute.route = this.route;
fastRoute.returnOut = this.returnOut;
fastRoute.doBeforeInterceptor = this.doBeforeInterceptor;
fastRoute.doAfterInterceptor = this.doAfterInterceptor;
fastRoute.firstMethodLineNumber = this.firstMethodLineNumber;
fastRoute.lastMethodLineNumber = this.lastMethodLineNumber;
return fastRoute;
}
void sortInterceptors() {
Comparator comparator = new Comparator() {
@Override
public int compare(RouteInterceptor o1, RouteInterceptor o2) {
if (o1.priority > o2.priority) {
return -1;
}
if (o1.priority < o2.priority) {
return 1;
}
if (o1.index > o2.index) {
return 1;
}
if (o1.index < o2.index) {
return -1;
}
return 0;
}
};
Collections.sort(doBeforeInterceptor, comparator);
Collections.sort(doAfterInterceptor, comparator);
}
public void invoke() {
try {
if (fastAction == null) {
fastAction = actionClass.newInstance();
fastAction.request = request;
fastAction.response = response;
fastAction.servletContext = FastEngine.instance().getServletContext();
fastAction.fastUrl = fastUrl;
fastAction.fastRoute = this;
fastAction.forwarder = this.forwarder;
if (fastUrl.getParams() != null) {
fastAction.params.addAll(fastUrl.getParams());
}
if (this.forwarder != null) {
fastAction.params.addAll(forwarder.params);
}
}
if (responseInvoked) {
response();
return;
}
doBeforeIndex++;
if (doBeforeIndex < doBeforeInterceptor.size()) {
beforeInvoked = false;
RouteInterceptor routeInterceptor = doBeforeInterceptor.get(doBeforeIndex);
IFastInterceptor fastInterceptor = routeInterceptor.interceptorClass.newInstance();
fastInterceptor.onInterceptor(fastAction);
return;
}
beforeInvoked = true;
stackTraceElements.addAll(Arrays.asList(Thread.currentThread().getStackTrace()));
List