Skip to content

Commit

Permalink
RESTful supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaleguo committed Aug 29, 2021
1 parent ec5310f commit 5f08a25
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ methodinterceptor,methodbeforeadvice, afterreturningadvice supported.
implements Pointcut and AutoProxyCreator.

Using annotation + aop implements @Async

REFTful supported

25 changes: 25 additions & 0 deletions src/com/minis/util/PatternMatchUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.minis.util;

import java.util.Arrays;

public abstract class PatternMatchUtils {

/**
Expand Down Expand Up @@ -66,5 +68,28 @@ public static boolean simpleMatch( String[] patterns, String str) {
return false;
}

public static boolean URIMatch( String patterns, String uri) {
if (patterns != null) {
String[] patternArr= patterns.split("/");
String[] uriArr= uri.split("/");

System.out.println(Arrays.toString(patternArr));
System.out.println(Arrays.toString(uriArr));

if (patternArr.length != uriArr.length) {
return false;
}
for (int i=0; i<patternArr.length; i++) {
if (patternArr[i].equals(uriArr[i])) {
} else if(patternArr[i].startsWith("{") && patternArr[i].endsWith("}")) {
} else {
return false;
}
}
return true;
}
return false;
}

}

11 changes: 11 additions & 0 deletions src/com/minis/web/bind/annotation/PathVariable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.minis.web.bind.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface PathVariable {
}
1 change: 1 addition & 0 deletions src/com/minis/web/bind/annotation/RequestMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
String value() default "";
String method() default "GET";
}
14 changes: 14 additions & 0 deletions src/com/minis/web/method/annotation/MappingRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class MappingRegistry {
private List<String> urlMappingNames = new ArrayList<>();
private List<String> requestMethods = new ArrayList<>();
private List<String> qualifiedNames = new ArrayList<>();
private Map<String,Object> mappingObjs = new HashMap<>();
private Map<String,Method> mappingMethods = new HashMap<>();
private Map<String,String> mappingMethodNames = new HashMap<>();
Expand Down Expand Up @@ -43,5 +45,17 @@ public Map<String,String> getMappingMethodNames() {
public void setMappingMethodNames(Map<String,String> mappingMethodNames) {
this.mappingMethodNames = mappingMethodNames;
}
public List<String> getRequestMethods() {
return requestMethods;
}
public void setRequestMethods(List<String> requestMethods) {
this.requestMethods = requestMethods;
}
public List<String> getQualifiedNames() {
return qualifiedNames;
}
public void setQualifiedNames(List<String> qualifiedNames) {
this.qualifiedNames = qualifiedNames;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.minis.context.ApplicationContextAware;
import com.minis.http.converter.HttpMessageConverter;
import com.minis.web.bind.WebDataBinder;
import com.minis.web.bind.annotation.PathVariable;
import com.minis.web.bind.annotation.ResponseBody;
import com.minis.web.bind.support.WebBindingInitializer;
import com.minis.web.bind.support.WebDataBinderFactory;
Expand Down Expand Up @@ -67,19 +68,30 @@ protected ModelAndView invokeHandlerMethod(HttpServletRequest request,

int i = 0;
for (Parameter methodParameter : methodParameters) {
if (methodParameter.getType()!=HttpServletRequest.class && methodParameter.getType()!=HttpServletResponse.class) {
if (methodParameter.getType()==HttpServletRequest.class) {
methodParamObjs[i] = request;
}
else if (methodParameter.getType()==HttpServletResponse.class) {
methodParamObjs[i] = response;
}
else if (methodParameter.isAnnotationPresent(PathVariable.class)) {
String sServletPath = request.getServletPath();
int index = sServletPath.lastIndexOf("/");
String sParam = sServletPath.substring(index+1);
if (int.class.isAssignableFrom(methodParameter.getType())) {
methodParamObjs[i] = Integer.parseInt(sParam);
} else if (String.class.isAssignableFrom(methodParameter.getType())) {
methodParamObjs[i] = sParam;
}
}
else if (methodParameter.getType()!=HttpServletRequest.class && methodParameter.getType()!=HttpServletResponse.class) {
Object methodParamObj = methodParameter.getType().newInstance();
WebDataBinder wdb = binderFactory.createBinder(request, methodParamObj, methodParameter.getName());
webBindingInitializer.initBinder(wdb);
wdb.bind(request);
methodParamObjs[i] = methodParamObj;
}
else if (methodParameter.getType()==HttpServletRequest.class) {
methodParamObjs[i] = request;
}
else if (methodParameter.getType()==HttpServletResponse.class) {
methodParamObjs[i] = response;
}

i++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.minis.beans.BeansException;
import com.minis.context.ApplicationContext;
import com.minis.context.ApplicationContextAware;
import com.minis.util.PatternMatchUtils;
import com.minis.web.bind.annotation.RequestMapping;
import com.minis.web.context.WebApplicationContext;
import com.minis.web.method.HandlerMethod;
Expand Down Expand Up @@ -43,16 +44,25 @@ protected void initMappings() {
if (isRequestMapping){
String methodName = method.getName();
String urlmapping = method.getAnnotation(RequestMapping.class).value();
String requestMethod = method.getAnnotation(RequestMapping.class).method();
if (requestMethod.equals("")) {
requestMethod="GET";
}
String qualifiedName = requestMethod + ":" + urlmapping;

this.mappingRegistry.getUrlMappingNames().add(urlmapping);
this.mappingRegistry.getMappingObjs().put(urlmapping, obj);
this.mappingRegistry.getMappingMethods().put(urlmapping, method);
this.mappingRegistry.getMappingMethodNames().put(urlmapping, methodName);
this.mappingRegistry.getMappingClasses().put(urlmapping, clz);
this.mappingRegistry.getRequestMethods().add(requestMethod);
this.mappingRegistry.getQualifiedNames().add(qualifiedName);

this.mappingRegistry.getMappingObjs().put(qualifiedName, obj);
this.mappingRegistry.getMappingMethods().put(qualifiedName, method);
this.mappingRegistry.getMappingMethodNames().put(qualifiedName, methodName);
this.mappingRegistry.getMappingClasses().put(qualifiedName, clz);

}
}
}
}

}


Expand All @@ -63,16 +73,35 @@ public HandlerMethod getHandler(HttpServletRequest request) throws Exception {
initMappings();
}

for (int i=0; i<this.mappingRegistry.getQualifiedNames().size();i++) {
System.out.println(this.mappingRegistry.getQualifiedNames().get(i));
}


String sPath = request.getServletPath();

if (!this.mappingRegistry.getUrlMappingNames().contains(sPath)) {
System.out.println("sPath:"+sPath);
String sRequestMethod = request.getMethod();
String qualifiedName = sRequestMethod+":"+sPath;
System.out.println("qualifiedName:"+qualifiedName);

String sPattern = "";

for (int i=0; i<this.mappingRegistry.getQualifiedNames().size();i++) {
if (PatternMatchUtils.URIMatch(this.mappingRegistry.getQualifiedNames().get(i), qualifiedName)) {
sPattern = this.mappingRegistry.getQualifiedNames().get(i);
break;
}
}
if (sPattern.equals("")) {
return null;
}

Method method = this.mappingRegistry.getMappingMethods().get(sPath);
Object obj = this.mappingRegistry.getMappingObjs().get(sPath);
Class<?> clz = this.mappingRegistry.getMappingClasses().get(sPath);
String methodName = this.mappingRegistry.getMappingMethodNames().get(sPath);
System.out.println("sPattern:"+sPattern);

Method method = this.mappingRegistry.getMappingMethods().get(sPattern);
Object obj = this.mappingRegistry.getMappingObjs().get(sPattern);
Class<?> clz = this.mappingRegistry.getMappingClasses().get(sPattern);
String methodName = this.mappingRegistry.getMappingMethodNames().get(sPattern);

HandlerMethod handlerMethod = new HandlerMethod(method, obj, clz, methodName);

Expand Down

0 comments on commit 5f08a25

Please sign in to comment.