This commit is contained in:
redkale
2024-09-20 20:43:49 +08:00
parent 8ad28e8932
commit 82d375b517
3 changed files with 25 additions and 70 deletions

View File

@@ -7,7 +7,6 @@ package org.redkale.net;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.function.Function;
@@ -176,6 +175,12 @@ public abstract class Request<C extends Context> {
return createTime;
}
/**
* 获取当前操作Method上的注解集合
*
* @return Annotation[]
*/
@ConvertDisabled
public Annotation[] getAnnotations() {
if (annotations == null) {
return new Annotation[0];
@@ -183,6 +188,13 @@ public abstract class Request<C extends Context> {
return Arrays.copyOfRange(annotations, 0, annotations.length);
}
/**
* 获取当前操作Method上的注解
*
* @param <T> 注解泛型
* @param annotationClass 注解类型
* @return Annotation
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
if (annotations != null) {
for (Annotation ann : annotations) {
@@ -194,9 +206,16 @@ public abstract class Request<C extends Context> {
return null;
}
/**
* 获取当前操作Method上的注解集合
*
* @param <T> 注解泛型
* @param annotationClass 注解类型
* @return Annotation[]
*/
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
if (annotations == null) {
return (T[]) Array.newInstance(annotationClass, 0);
return Creator.newArray(annotationClass, 0);
} else {
List<T> list = new ArrayList<>();
for (Annotation ann : annotations) {

View File

@@ -167,8 +167,6 @@ public class HttpRequest extends Request<HttpContext> {
protected int actionid;
protected Annotation[] annotations;
protected String remoteAddr;
protected String locale;
@@ -354,7 +352,7 @@ public class HttpRequest extends Request<HttpContext> {
return 1;
}
hbytes.put(buffer, remainHalf);
this.headerLength = this.headerBytes.length();
this.headerLength = hbytes.length();
this.headerHalfLen = this.headerLength;
this.headerParsed = false;
} else if (context.lazyHeader && getmethod) { // 非GET必须要读header会有Content-Length
@@ -390,8 +388,8 @@ public class HttpRequest extends Request<HttpContext> {
buffer.clear();
return rs;
}
this.headerParsed = true;
this.headerHalfLen = this.headerLength;
this.headerParsed = true;
}
if (this.contentType != null && this.contentType.contains("boundary=")) {
this.boundary = true;
@@ -1313,7 +1311,6 @@ public class HttpRequest extends Request<HttpContext> {
this.boundary = false;
this.moduleid = 0;
this.actionid = 0;
this.annotations = null;
this.remoteAddr = null;
this.params.clear();
this.bodyBytes.clear();
@@ -1667,67 +1664,6 @@ public class HttpRequest extends Request<HttpContext> {
return this.actionid;
}
/**
* 获取当前操作Method上的注解集合
*
* @return Annotation[]
*/
@Override
@ConvertDisabled
public Annotation[] getAnnotations() {
if (this.annotations == null) {
return new Annotation[0];
}
Annotation[] newanns = new Annotation[this.annotations.length];
System.arraycopy(this.annotations, 0, newanns, 0, newanns.length);
return newanns;
}
/**
* 获取当前操作Method上的注解
*
* @param <T> 注解泛型
* @param annotationClass 注解类型
* @return Annotation
*/
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
if (this.annotations == null) {
return null;
}
for (Annotation ann : this.annotations) {
if (ann.getClass() == annotationClass) {
return (T) ann;
}
}
return null;
}
/**
* 获取当前操作Method上的注解集合
*
* @param <T> 注解泛型
* @param annotationClass 注解类型
* @return Annotation[]
*/
@Override
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
if (this.annotations == null) {
return Creator.newArray(annotationClass, 0);
}
T[] news = Creator.newArray(annotationClass, this.annotations.length);
int index = 0;
for (Annotation ann : this.annotations) {
if (ann.getClass() == annotationClass) {
news[index++] = (T) ann;
}
}
if (index < 1) {
return Creator.newArray(annotationClass, 0);
}
return Arrays.copyOf(news, index);
}
/**
* 获取客户端地址IP
*

View File

@@ -124,7 +124,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
}
request.moduleid = entry.moduleid;
request.actionid = entry.actionid;
request.annotations = entry.annotations;
request.setAnnotations(entry.annotations);
if (entry.auth) {
response.thenEvent(authSuccessServlet);
authenticate(request, response);
@@ -143,7 +143,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
request.actionEntry = entry;
request.moduleid = entry.moduleid;
request.actionid = entry.actionid;
request.annotations = entry.annotations;
request.setAnnotations(entry.annotations);
if (entry.auth) {
response.thenEvent(authSuccessServlet);
authenticate(request, response);