From 0bf7ccd7018a8edb7cd00674a224fb9c360c7743 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 13 Apr 2018 16:04:12 +0800 Subject: [PATCH] --- src/org/redkale/net/http/HttpResponse.java | 2 + src/org/redkale/net/http/HttpResult.java | 5 + src/org/redkale/net/http/Rest.java | 124 +++++++++++---------- 3 files changed, 75 insertions(+), 56 deletions(-) diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index 1ce16ce04..313eb3ec6 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -482,6 +482,8 @@ public class HttpResponse extends Response { addHeader(result.getHeaders()).addCookie(result.getCookies()).setStatus(result.getStatus() < 1 ? 200 : result.getStatus()); if (result.getResult() == null) { finish(""); + } else if (result.getResult() instanceof CharSequence) { + finish(result.getResult().toString()); } else { finish(convert, result.getResult()); } diff --git a/src/org/redkale/net/http/HttpResult.java b/src/org/redkale/net/http/HttpResult.java index f1a1177e3..f7352858f 100644 --- a/src/org/redkale/net/http/HttpResult.java +++ b/src/org/redkale/net/http/HttpResult.java @@ -41,6 +41,11 @@ public class HttpResult { this.result = result; } + public HttpResult(String contentType, T result) { + this.contentType = contentType; + this.result = result; + } + public HttpResult header(String name, Serializable value) { if (this.headers == null) this.headers = new HashMap<>(); this.headers.put(name, String.valueOf(value)); diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index 0fe202d48..58d0a33b3 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -152,14 +152,14 @@ public final class Rest { final RestService controller = serviceType.getAnnotation(RestService.class); if (controller == null) return serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase(); if (controller.ignore()) return null; - return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase(); + return (!controller.name().isEmpty()) ? controller.name().trim() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase(); } static String getWebModuleName(Class serviceType) { final RestService controller = serviceType.getAnnotation(RestService.class); if (controller == null) return serviceType.getSimpleName().replaceAll("Service.*$", ""); if (controller.ignore()) return null; - return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", ""); + return (!controller.name().isEmpty()) ? controller.name().trim() : serviceType.getSimpleName().replaceAll("Service.*$", ""); } static boolean isRestDyn(HttpServlet servlet) { @@ -720,59 +720,6 @@ public final class Rest { av0.visit("value", Type.getType(Type.getDescriptor(serviceType))); av0.visitEnd(); } - { //注入 @WebServlet 注解 - String urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/*"; - int moduleid = controller == null ? 0 : controller.moduleid(); - boolean repair = controller == null ? true : controller.repair(); - String comment = controller == null ? "" : controller.comment(); - av0 = cw.visitAnnotation(webServletDesc, true); - { - AnnotationVisitor av1 = av0.visitArray("value"); - av1.visit(null, urlpath); - av1.visitEnd(); - } - av0.visit("moduleid", moduleid); - av0.visit("repair", repair); - av0.visit("comment", comment); - av0.visitEnd(); - classMap.put("type", serviceType.getName()); - classMap.put("url", urlpath); - classMap.put("moduleid", moduleid); - classMap.put("repair", repair); - //classMap.put("comment", comment); //不显示太多信息 - } - - { //注入 @Resource private XXXService _service; - fv = cw.visitField(ACC_PRIVATE, REST_SERVICE_FIELD_NAME, serviceDesc, null, null); - av0 = fv.visitAnnotation(resDesc, true); - av0.visit("name", ""); - av0.visitEnd(); - fv.visitEnd(); - } - { //注入 @Resource(name = "APP_HOME") private File _redkale_home; - fv = cw.visitField(ACC_PRIVATE, "_redkale_home", Type.getDescriptor(File.class), null, null); - av0 = fv.visitAnnotation(resDesc, true); - av0.visit("name", "APP_HOME"); - av0.visitEnd(); - fv.visitEnd(); - } - { //_servicemap字段 Map - fv = cw.visitField(ACC_PRIVATE, REST_SERVICEMAP_FIELD_NAME, "Ljava/util/Map;", "Ljava/util/Map;", null); - fv.visitEnd(); - } - { //_paramtypes字段 java.lang.reflect.Type[][] - fv = cw.visitField(ACC_PRIVATE, REST_PARAMTYPES_FIELD_NAME, "[[Ljava/lang/reflect/Type;", null, null); - fv.visitEnd(); - } - { //构造函数 - mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "", "()V", null, null)); - //mv.setDebug(true); - mv.visitVarInsn(ALOAD, 0); - mv.visitMethodInsn(INVOKESPECIAL, supDynName, "", "()V", false); - mv.visitInsn(RETURN); - mv.visitMaxs(1, 1); - mv.visitEnd(); - } final List entrys = new ArrayList<>(); final Map restAttributes = new LinkedHashMap<>(); @@ -823,6 +770,71 @@ public final class Rest { methodidex++; } if (entrys.isEmpty()) return null; //没有可HttpMapping的方法 + + { //注入 @WebServlet 注解 + String urlpath = ""; + int moduleid = controller == null ? 0 : controller.moduleid(); + boolean repair = controller == null ? true : controller.repair(); + String comment = controller == null ? "" : controller.comment(); + av0 = cw.visitAnnotation(webServletDesc, true); + { + AnnotationVisitor av1 = av0.visitArray("value"); + if (defmodulename.isEmpty()) { + for (MappingEntry entry : entrys) { + String suburl = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name; + urlpath += "," + suburl; + av1.visit(null, suburl); + } + if (urlpath.length() > 0) urlpath = urlpath.substring(1); + } else { + urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/*"; + av1.visit(null, urlpath); + } + av1.visitEnd(); + } + av0.visit("moduleid", moduleid); + av0.visit("repair", repair); + av0.visit("comment", comment); + av0.visitEnd(); + classMap.put("type", serviceType.getName()); + classMap.put("url", urlpath); + classMap.put("moduleid", moduleid); + classMap.put("repair", repair); + //classMap.put("comment", comment); //不显示太多信息 + } + + { //注入 @Resource private XXXService _service; + fv = cw.visitField(ACC_PRIVATE, REST_SERVICE_FIELD_NAME, serviceDesc, null, null); + av0 = fv.visitAnnotation(resDesc, true); + av0.visit("name", ""); + av0.visitEnd(); + fv.visitEnd(); + } + { //注入 @Resource(name = "APP_HOME") private File _redkale_home; + fv = cw.visitField(ACC_PRIVATE, "_redkale_home", Type.getDescriptor(File.class), null, null); + av0 = fv.visitAnnotation(resDesc, true); + av0.visit("name", "APP_HOME"); + av0.visitEnd(); + fv.visitEnd(); + } + { //_servicemap字段 Map + fv = cw.visitField(ACC_PRIVATE, REST_SERVICEMAP_FIELD_NAME, "Ljava/util/Map;", "Ljava/util/Map;", null); + fv.visitEnd(); + } + { //_paramtypes字段 java.lang.reflect.Type[][] + fv = cw.visitField(ACC_PRIVATE, REST_PARAMTYPES_FIELD_NAME, "[[Ljava/lang/reflect/Type;", null, null); + fv.visitEnd(); + } + { //构造函数 + mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "", "()V", null, null)); + //mv.setDebug(true); + mv.visitVarInsn(ALOAD, 0); + mv.visitMethodInsn(INVOKESPECIAL, supDynName, "", "()V", false); + mv.visitInsn(RETURN); + mv.visitMaxs(1, 1); + mv.visitEnd(); + } + //将每个Service可转换的方法生成HttpServlet对应的HttpMapping方法 final Map> asmParamMap = MethodParamClassVisitor.getMethodParamNames(new HashMap<>(), serviceType); final Map bodyTypes = new HashMap<>(); @@ -999,7 +1011,7 @@ public final class Rest { } } av0 = mv.visitAnnotation(mappingDesc, true); - String url = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/" + entry.name + (reqpath ? "/" : ""); + String url = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name + (reqpath ? "/" : ""); av0.visit("url", url); av0.visit("auth", entry.auth); av0.visit("cacheseconds", entry.cacheseconds);