Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加入注解指定http配置 #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package top.crossoverjie.cicada.server.annotation;


import top.crossoverjie.cicada.server.enums.HttpMethod;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
Expand All @@ -9,4 +11,6 @@
public @interface CicadaRoute {

String value() default "" ;

HttpMethod[] method() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package top.crossoverjie.cicada.server.enums;

import java.util.HashMap;
import java.util.Map;

public enum HttpMethod {

POST,

GET,

PUT,

PATCH,

DELETED;

private static final Map<String, HttpMethod> mappings = new HashMap<>(16);

static {
for (HttpMethod httpMethod : values()) {
mappings.put(httpMethod.name(), httpMethod);
}
}

public static HttpMethod resolve(String method) {
return (method != null ? mappings.get(method) : null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public enum StatusEnum {
/** empty of package */
NULL_PACKAGE("8000", "Your main class is empty of package"),

REPEAT_ROUTE("8000","Request correspond multiple method"),

/** 404 */
NOT_FOUND("404", "Need to declare a method by using @CicadaRoute!"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest httpReque
}

// execute Method
Method method = routerScanner.routeMethod(queryStringDecoder);
Method method = routerScanner.routeMethod(cicadaRequest.getMethod(),queryStringDecoder);
routeProcess.invoke(method,queryStringDecoder) ;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import top.crossoverjie.cicada.server.annotation.CicadaRoute;
import top.crossoverjie.cicada.server.config.AppConfig;
import top.crossoverjie.cicada.server.context.CicadaContext;
import top.crossoverjie.cicada.server.enums.HttpMethod;
import top.crossoverjie.cicada.server.enums.StatusEnum;
import top.crossoverjie.cicada.server.exception.CicadaException;
import top.crossoverjie.cicada.server.reflect.ClassScanner;
import top.crossoverjie.cicada.server.route.register.RouterMethodInfo;
import top.crossoverjie.cicada.server.route.register.RequestRouteInfo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Function:
Expand All @@ -23,7 +24,10 @@
*/
public class RouterScanner {

private static Map<String, Method> routes = null;
//private static Map<String, Method> routes = null;

private static Map<RequestRouteInfo, Method> routes = new HashMap<>(16);


private volatile static RouterScanner routerScanner;

Expand All @@ -45,21 +49,24 @@ public static RouterScanner getInstance() {
return routerScanner;
}

private RouterScanner() {
private RouterScanner(){
try {
registerRouter(appConfig.getRootPackageName());
}catch (Exception e){
e.printStackTrace();
}

}

/**
* get route method
* get route method
*
* @param method http method
* @param queryStringDecoder
* @return
* @throws Exception
*/
public Method routeMethod(QueryStringDecoder queryStringDecoder) throws Exception {
if (routes == null) {
routes = new HashMap<>(16);
loadRouteMethods(appConfig.getRootPackageName());
}
public Method routeMethod(String method, QueryStringDecoder queryStringDecoder) throws Exception {

//default response
boolean defaultResponse = defaultResponse(queryStringDecoder.path());
Expand All @@ -68,15 +75,12 @@ public Method routeMethod(QueryStringDecoder queryStringDecoder) throws Exceptio
return null;
}

Method method = routes.get(queryStringDecoder.path());

if (method == null) {
RequestRouteInfo info = new RequestRouteInfo(queryStringDecoder.path(), HttpMethod.resolve(method));
boolean isContains = routes.containsKey(info);
if (!isContains){
throw new CicadaException(StatusEnum.NOT_FOUND);
}

return method;


return routes.get(info);
}

private boolean defaultResponse(String path) {
Expand All @@ -88,21 +92,57 @@ private boolean defaultResponse(String path) {
return false;
}


private void loadRouteMethods(String packageName) throws Exception {
private void registerRouter(String packageName) throws Exception {
Set<Class<?>> classes = ClassScanner.getClasses(packageName);
if (routes == null){
routes = new HashMap<>(16);
}

Set<RouterMethodInfo> defaultRouterMethod = new HashSet<>(16);

for (Class<?> aClass : classes) {
Method[] declaredMethods = aClass.getMethods();
CicadaAction cicadaAction = aClass.getAnnotation(CicadaAction.class);

for (Method method : declaredMethods) {
CicadaRoute annotation = method.getAnnotation(CicadaRoute.class);
if (annotation == null) {
continue;
}
HttpMethod[] methods = annotation.method();
String url = appConfig.getRootPath() + "/" + cicadaAction.value() + "/" + annotation.value();

if (methods.length <= 0){
RouterMethodInfo info = new RouterMethodInfo(url,method);
//判断是否重复标记
if (defaultRouterMethod.contains(info)){
throw new CicadaException(StatusEnum.REPEAT_ROUTE);
}
defaultRouterMethod.add(info);
}else {
for (HttpMethod httpMethod : methods) {

RequestRouteInfo info = new RequestRouteInfo(url,httpMethod);
if (routes.containsKey(info)){
throw new CicadaException(StatusEnum.REPEAT_ROUTE);
}
routes.put(info,method);
}
}
}
}

for (RouterMethodInfo routerMethodInfo : defaultRouterMethod) {
String url = routerMethodInfo.getPath();
Method method = routerMethodInfo.getMethod();
HttpMethod[] methods = HttpMethod.values();

CicadaAction cicadaAction = aClass.getAnnotation(CicadaAction.class);
routes.put(appConfig.getRootPath() + "/" + cicadaAction.value() + "/" + annotation.value(), method);
for (HttpMethod httpMethod : methods) {
RequestRouteInfo info = new RequestRouteInfo(url,httpMethod);
if (routes.containsKey(info)){
continue;
}
routes.put(info,method);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package top.crossoverjie.cicada.server.route.register;

import com.google.common.base.Objects;
import top.crossoverjie.cicada.server.enums.HttpMethod;

public class RequestRouteInfo {

private HttpMethod httpMethods;

private String path;

public RequestRouteInfo(String path, HttpMethod httpMethods) {
this.httpMethods = httpMethods;
this.path = path;
}

public RequestRouteInfo(){}

public HttpMethod getHttpMethods() {
return httpMethods;
}

public void setHttpMethods(HttpMethod httpMethods) {
this.httpMethods = httpMethods;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RequestRouteInfo that = (RequestRouteInfo) o;
return httpMethods == that.httpMethods &&
Objects.equal(path, that.path);
}

@Override
public int hashCode() {
return Objects.hashCode(httpMethods, path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package top.crossoverjie.cicada.server.route.register;

import com.google.common.base.Objects;

import java.lang.reflect.Method;

/**
* @author TANG
*/
public class RouterMethodInfo {

private String path;

private Method method;

public RouterMethodInfo(String path, Method method) {
this.path = path;
this.method = method;
}

public RouterMethodInfo() {
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Method getMethod() {
return method;
}

public void setMethod(Method method) {
this.method = method;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RouterMethodInfo that = (RouterMethodInfo) o;
return Objects.equal(path, that.path) &&
Objects.equal(method, that.method);
}

@Override
public int hashCode() {
return Objects.hashCode(path, method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import top.crossoverjie.cicada.server.annotation.CicadaRoute;
import top.crossoverjie.cicada.server.bean.CicadaBeanManager;
import top.crossoverjie.cicada.server.context.CicadaContext;
import top.crossoverjie.cicada.server.enums.HttpMethod;

/**
* Function:
Expand Down Expand Up @@ -70,4 +71,9 @@ public void test(CicadaContext context){
context.html("<p>12345</p>");
}

@CicadaRoute(value = "method",method = HttpMethod.POST)
public void methodSupport(CicadaContext context){
context.text("POST only");
}

}