diff --git a/src/org/redkale/boot/NodeHttpServer.java b/src/org/redkale/boot/NodeHttpServer.java index 8a2c338b7..0a0d10ee0 100644 --- a/src/org/redkale/boot/NodeHttpServer.java +++ b/src/org/redkale/boot/NodeHttpServer.java @@ -256,7 +256,14 @@ public class NodeHttpServer extends NodeServer { List> list = new ArrayList(webSocketFilter.getFilterEntrys()); for (FilterEntry en : list) { Class clazz = (Class) en.getType(); - if (Modifier.isAbstract(clazz.getModifiers())) continue; + if (Modifier.isAbstract(clazz.getModifiers())) { + logger.log(Level.FINE, clazz.getName() + " cannot abstract on rest websocket, so ignore"); + continue; + } + if (Modifier.isFinal(clazz.getModifiers())) { + logger.log(Level.FINE, clazz.getName() + " cannot final on rest websocket, so ignore"); + continue; + } final Class stype = en.getType(); RestWebSocket rs = stype.getAnnotation(RestWebSocket.class); if (rs == null || rs.ignore()) return; diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index a916579e6..12edc40f6 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -12,6 +12,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.reflect.*; import java.util.*; import java.util.concurrent.CompletableFuture; +import javax.annotation.Resource; import jdk.internal.org.objectweb.asm.*; import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; import static jdk.internal.org.objectweb.asm.Opcodes.*; @@ -105,6 +106,34 @@ public final class Rest { } static T createRestWebSocketServlet(final Class webSocketType) { + if (webSocketType == null) throw new RuntimeException("Rest WebSocket Class is null on createRestWebSocketServlet"); + if (Modifier.isAbstract(webSocketType.getModifiers())) throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") cannot abstract on createRestWebSocketServlet"); + if (Modifier.isFinal(webSocketType.getModifiers())) throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") cannot final on createRestWebSocketServlet"); + boolean valid = false; + for (Constructor c : webSocketType.getDeclaredConstructors()) { + if (c.getParameterCount() == 0 && (Modifier.isPublic(c.getModifiers()) || Modifier.isProtected(c.getModifiers()))) { + valid = true; + break; + } + } + if (!valid) throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") must have public or protected Constructor on createRestWebSocketServlet"); + + final Set resourcesField = new HashSet<>(); + Class clzz = webSocketType; + do { + for (Field field : webSocketType.getDeclaredFields()) { + if (field.getAnnotation(Resource.class) == null) continue; + if (Modifier.isStatic(webSocketType.getModifiers())) throw new RuntimeException(field + " cannot static on createRestWebSocketServlet"); + if (Modifier.isFinal(webSocketType.getModifiers())) throw new RuntimeException(field + " cannot final on createRestWebSocketServlet"); + if (!Modifier.isPublic(webSocketType.getModifiers()) && !Modifier.isProtected(webSocketType.getModifiers())) { + throw new RuntimeException(field + " must be public or protected on createRestWebSocketServlet"); + } + resourcesField.add(field); + } + } while ((clzz = clzz.getSuperclass()) != Object.class); + for (Method method : webSocketType.getMethods()) { + + } return null; //待实现 } @@ -147,17 +176,8 @@ public final class Rest { //------------------------------------------------------------------------------ final String defmodulename = getWebModuleName(serviceType); final String catalog = controller == null ? "" : controller.catalog(); - for (char ch : catalog.toCharArray()) { - if (!((ch >= '0' && ch <= '9') || ch == '$' || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { //不能含特殊字符 - throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ $"); - } - } - for (char ch : defmodulename.toCharArray()) { - if (!((ch >= '0' && ch <= '9') || ch == '$' || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { //不能含特殊字符 - throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ $"); - } - } - + if (!checkName(catalog)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9"); + if (!checkName(defmodulename)) throw new RuntimeException(serviceType.getName() + " have illeal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9"); ClassWriter cw = new ClassWriter(COMPUTE_FRAMES); FieldVisitor fv; AsmMethodVisitor mv; @@ -1169,6 +1189,17 @@ public final class Rest { } } + private static boolean checkName(String name) { //不能含特殊字符 + if (name.isEmpty()) return true; + if (name.charAt(0) >= '0' && name.charAt(0) <= '9') return false; + for (char ch : name.toCharArray()) { + if (!((ch >= '0' && ch <= '9') || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) { //不能含特殊字符 + return false; + } + } + return true; + } + private static class MappingEntry { private static final RestMapping DEFAULT__MAPPING; diff --git a/src/org/redkale/net/http/RestWebSocket.java b/src/org/redkale/net/http/RestWebSocket.java index ff1874e27..d996fdda0 100644 --- a/src/org/redkale/net/http/RestWebSocket.java +++ b/src/org/redkale/net/http/RestWebSocket.java @@ -10,7 +10,8 @@ import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * 只能依附在WebSocket类上,name默认为Service的类名小写并去掉Service字样及后面的字符串 (如HelloWebSocket/HelloWebSocketImpl,的默认路径为 hello)。 + * 只能依附在WebSocket类上,name默认为Service的类名小写并去掉Service字样及后面的字符串 (如HelloWebSocket/HelloWebSocketImpl,的默认路径为 hello)。
+ * 注意: 被标记@RestWebSocket的WebSocket不能被修饰为abstract或final,且其内部标记为@Resource的字段只能是protected或public,且必须要有一个protected或public的空参数构造函数。
*

* 详情见: https://redkale.org *