This commit is contained in:
@@ -130,7 +130,8 @@
|
|||||||
</services>
|
</services>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
REST的核心配置项, 存在[rest]节点则Server启动时会加载REST服务, 当Server为SNCP协议时,则SncpServer会变成REST的HttpServer, 节点可以多个
|
REST的核心配置项
|
||||||
|
当Server为HTTP协议时, rest节点才有效。存在[rest]节点则Server启动时会加载REST服务, 节点可以多个
|
||||||
base: REST服务的BaseServlet,必须是 org.redkale.net.http.RestHttpServlet 的子类,该属性值默认值为 org.redkale.net.http.DefaultRestServlet。
|
base: REST服务的BaseServlet,必须是 org.redkale.net.http.RestHttpServlet 的子类,该属性值默认值为 org.redkale.net.http.DefaultRestServlet。
|
||||||
autoload:默认值"true" 默认值. 加载当前server所能使用的Servce对象;
|
autoload:默认值"true" 默认值. 加载当前server所能使用的Servce对象;
|
||||||
mustsign:默认值"true" 是否只加载标记为RestService的Service类,默认只加载标记RestService且ignore=false的Service
|
mustsign:默认值"true" 是否只加载标记为RestService的Service类,默认只加载标记RestService且ignore=false的Service
|
||||||
|
|||||||
@@ -31,15 +31,12 @@ public class NodeHttpServer extends NodeServer {
|
|||||||
|
|
||||||
protected final boolean rest;
|
protected final boolean rest;
|
||||||
|
|
||||||
protected final boolean sncp;
|
|
||||||
|
|
||||||
protected final HttpServer httpServer;
|
protected final HttpServer httpServer;
|
||||||
|
|
||||||
public NodeHttpServer(Application application, AnyValue serconf) {
|
public NodeHttpServer(Application application, AnyValue serconf) {
|
||||||
super(application, createServer(application, serconf));
|
super(application, createServer(application, serconf));
|
||||||
this.httpServer = (HttpServer) server;
|
this.httpServer = (HttpServer) server;
|
||||||
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
this.rest = serconf == null ? false : serconf.getAnyValue("rest") != null;
|
||||||
this.sncp = serconf == null ? false : serconf.getBoolValue("_$sncp", false); //SNCP服务以REST启动时会赋值_$sncp=true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Server createServer(Application application, AnyValue serconf) {
|
private static Server createServer(Application application, AnyValue serconf) {
|
||||||
@@ -184,7 +181,7 @@ public class NodeHttpServer extends NodeServer {
|
|||||||
if (!autoload && !includeValues.contains(stypename)) return;
|
if (!autoload && !includeValues.contains(stypename)) return;
|
||||||
if (!restFilter.accept(stypename)) return;
|
if (!restFilter.accept(stypename)) return;
|
||||||
|
|
||||||
RestHttpServlet servlet = httpServer.addRestServlet(stype, wrapper.getName(), wrapper.getService(), baseServletClass, prefix, sncp, (AnyValue) null);
|
RestHttpServlet servlet = httpServer.addRestServlet(wrapper.getName(), stype, wrapper.getService(), baseServletClass, prefix, (AnyValue) null);
|
||||||
resourceFactory.inject(servlet, NodeHttpServer.this);
|
resourceFactory.inject(servlet, NodeHttpServer.this);
|
||||||
if (finest) logger.finest("Create RestServlet[resource=" + wrapper.getName() + "] = " + servlet);
|
if (finest) logger.finest("Create RestServlet[resource=" + wrapper.getName() + "] = " + servlet);
|
||||||
if (ss != null) {
|
if (ss != null) {
|
||||||
|
|||||||
@@ -41,13 +41,12 @@ public final class HttpServer extends Server<String, HttpContext, HttpRequest, H
|
|||||||
this.prepare.addServlet(servlet, prefix, conf, mappings);
|
this.prepare.addServlet(servlet, prefix, conf, mappings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S extends Service, T extends RestHttpServlet> RestHttpServlet addRestServlet(Class<S> serviceType,
|
public <S extends Service, T extends RestHttpServlet> RestHttpServlet addRestServlet(String name, Class<S> serviceType, S service, Class<T> baseServletClass, String prefix) {
|
||||||
final String name, final S service, final Class<T> baseServletClass, final String prefix) {
|
return addRestServlet(name, serviceType, service, baseServletClass, prefix, null);
|
||||||
return addRestServlet(serviceType, name, service, baseServletClass, prefix, false, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S extends Service, T extends RestHttpServlet> RestHttpServlet addRestServlet(Class<S> serviceType,
|
public <S extends Service, T extends RestHttpServlet> RestHttpServlet addRestServlet(
|
||||||
final String name, final S service, final Class<T> baseServletClass, final String prefix, final boolean sncp, AnyValue conf) {
|
final String name, Class<S> serviceType, final S service, final Class<T> baseServletClass, final String prefix, AnyValue conf) {
|
||||||
RestHttpServlet servlet = null;
|
RestHttpServlet servlet = null;
|
||||||
for (final HttpServlet item : ((HttpPrepareServlet) this.prepare).getServlets()) {
|
for (final HttpServlet item : ((HttpPrepareServlet) this.prepare).getServlets()) {
|
||||||
if (!(item instanceof RestHttpServlet)) continue;
|
if (!(item instanceof RestHttpServlet)) continue;
|
||||||
@@ -58,9 +57,11 @@ public final class HttpServer extends Server<String, HttpContext, HttpRequest, H
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (NoSuchFieldException | SecurityException e) {
|
} catch (NoSuchFieldException | SecurityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (servlet == null) servlet = Rest.createRestServlet(baseServletClass, serviceType, sncp);
|
final boolean first = servlet == null;
|
||||||
|
if (servlet == null) servlet = Rest.createRestServlet(baseServletClass, serviceType);
|
||||||
try { //若提供动态变更Service服务功能,则改Rest服务无法做出相应更新
|
try { //若提供动态变更Service服务功能,则改Rest服务无法做出相应更新
|
||||||
Field field = servlet.getClass().getDeclaredField(Rest.REST_SERVICE_FIELD_NAME);
|
Field field = servlet.getClass().getDeclaredField(Rest.REST_SERVICE_FIELD_NAME);
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
@@ -83,7 +84,7 @@ public final class HttpServer extends Server<String, HttpContext, HttpRequest, H
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(serviceType + " generate rest servlet error", e);
|
throw new RuntimeException(serviceType + " generate rest servlet error", e);
|
||||||
}
|
}
|
||||||
this.prepare.addServlet(servlet, prefix, conf);
|
if (first) this.prepare.addServlet(servlet, prefix, conf);
|
||||||
return servlet;
|
return servlet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public final class Rest {
|
|||||||
return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
return (!controller.name().isEmpty()) ? controller.name() : serviceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends RestHttpServlet> T createRestServlet(final Class<T> baseServletClass, final Class<? extends Service> serviceType, final boolean sncp) {
|
static <T extends RestHttpServlet> T createRestServlet(final Class<T> baseServletClass, final Class<? extends Service> serviceType) {
|
||||||
if (baseServletClass == null || serviceType == null) return null;
|
if (baseServletClass == null || serviceType == null) return null;
|
||||||
if (!RestHttpServlet.class.isAssignableFrom(baseServletClass)) return null;
|
if (!RestHttpServlet.class.isAssignableFrom(baseServletClass)) return null;
|
||||||
int mod = baseServletClass.getModifiers();
|
int mod = baseServletClass.getModifiers();
|
||||||
@@ -85,7 +85,7 @@ public final class Rest {
|
|||||||
|
|
||||||
final String reqInternalName = Type.getInternalName(HttpRequest.class);
|
final String reqInternalName = Type.getInternalName(HttpRequest.class);
|
||||||
final String respInternalName = Type.getInternalName(HttpResponse.class);
|
final String respInternalName = Type.getInternalName(HttpResponse.class);
|
||||||
final String attrInternalName = Type.getDescriptor(org.redkale.util.Attribute.class);
|
final String attrInternalName = Type.getInternalName(org.redkale.util.Attribute.class);
|
||||||
final String retInternalName = Type.getInternalName(RetResult.class);
|
final String retInternalName = Type.getInternalName(RetResult.class);
|
||||||
final String serviceTypeInternalName = Type.getInternalName(serviceType);
|
final String serviceTypeInternalName = Type.getInternalName(serviceType);
|
||||||
|
|
||||||
@@ -259,41 +259,37 @@ public final class Rest {
|
|||||||
String n = null;
|
String n = null;
|
||||||
String comment = "";
|
String comment = "";
|
||||||
int radix = 10;
|
int radix = 10;
|
||||||
RestHeader annhead = null;
|
|
||||||
RestSessionid annsid = null;
|
RestHeader annhead = param.getAnnotation(RestHeader.class);
|
||||||
RestCookie anncookie = null;
|
if (annhead != null) {
|
||||||
RestAddress annaddr = null;
|
n = annhead.name();
|
||||||
if (!sncp) { //SNCP协议中忽略参数中特定的注解,此处获取的只是SNCP请求端信息,并不是真实用户请求端的信息
|
radix = annhead.radix();
|
||||||
annhead = param.getAnnotation(RestHeader.class);
|
comment = annhead.comment();
|
||||||
if (annhead != null) {
|
if (n.isEmpty()) throw new RuntimeException("@RestHeader.value is illegal in " + method);
|
||||||
n = annhead.name();
|
|
||||||
radix = annhead.radix();
|
|
||||||
comment = annhead.comment();
|
|
||||||
if (n.isEmpty()) throw new RuntimeException("@RestHeader.value is illegal in " + method);
|
|
||||||
}
|
|
||||||
anncookie = param.getAnnotation(RestCookie.class);
|
|
||||||
if (anncookie != null) {
|
|
||||||
if (annhead != null) throw new RuntimeException("@RestCookie and @RestHeader cannot on the same Parameter in " + method);
|
|
||||||
if (ptype != String.class) throw new RuntimeException("@RestCookie must on String Parameter in " + method);
|
|
||||||
n = anncookie.name();
|
|
||||||
radix = anncookie.radix();
|
|
||||||
comment = anncookie.comment();
|
|
||||||
if (n.isEmpty()) throw new RuntimeException("@RestCookie.value is illegal in " + method);
|
|
||||||
}
|
|
||||||
annsid = param.getAnnotation(RestSessionid.class);
|
|
||||||
if (annsid != null) {
|
|
||||||
if (annhead != null) throw new RuntimeException("@RestSessionid and @RestHeader cannot on the same Parameter in " + method);
|
|
||||||
if (anncookie != null) throw new RuntimeException("@RestSessionid and @RestCookie cannot on the same Parameter in " + method);
|
|
||||||
if (ptype != String.class) throw new RuntimeException("@RestSessionid must on String Parameter in " + method);
|
|
||||||
}
|
|
||||||
annaddr = param.getAnnotation(RestAddress.class);
|
|
||||||
if (annaddr != null) {
|
|
||||||
if (annhead != null) throw new RuntimeException("@RestAddress and @RestHeader cannot on the same Parameter in " + method);
|
|
||||||
if (anncookie != null) throw new RuntimeException("@RestAddress and @RestCookie cannot on the same Parameter in " + method);
|
|
||||||
if (annsid != null) throw new RuntimeException("@RestAddress and @RestSessionid cannot on the same Parameter in " + method);
|
|
||||||
if (ptype != String.class) throw new RuntimeException("@RestAddress must on String Parameter in " + method);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
RestCookie anncookie = param.getAnnotation(RestCookie.class);
|
||||||
|
if (anncookie != null) {
|
||||||
|
if (annhead != null) throw new RuntimeException("@RestCookie and @RestHeader cannot on the same Parameter in " + method);
|
||||||
|
if (ptype != String.class) throw new RuntimeException("@RestCookie must on String Parameter in " + method);
|
||||||
|
n = anncookie.name();
|
||||||
|
radix = anncookie.radix();
|
||||||
|
comment = anncookie.comment();
|
||||||
|
if (n.isEmpty()) throw new RuntimeException("@RestCookie.value is illegal in " + method);
|
||||||
|
}
|
||||||
|
RestSessionid annsid = param.getAnnotation(RestSessionid.class);
|
||||||
|
if (annsid != null) {
|
||||||
|
if (annhead != null) throw new RuntimeException("@RestSessionid and @RestHeader cannot on the same Parameter in " + method);
|
||||||
|
if (anncookie != null) throw new RuntimeException("@RestSessionid and @RestCookie cannot on the same Parameter in " + method);
|
||||||
|
if (ptype != String.class) throw new RuntimeException("@RestSessionid must on String Parameter in " + method);
|
||||||
|
}
|
||||||
|
RestAddress annaddr = param.getAnnotation(RestAddress.class);
|
||||||
|
if (annaddr != null) {
|
||||||
|
if (annhead != null) throw new RuntimeException("@RestAddress and @RestHeader cannot on the same Parameter in " + method);
|
||||||
|
if (anncookie != null) throw new RuntimeException("@RestAddress and @RestCookie cannot on the same Parameter in " + method);
|
||||||
|
if (annsid != null) throw new RuntimeException("@RestAddress and @RestSessionid cannot on the same Parameter in " + method);
|
||||||
|
if (ptype != String.class) throw new RuntimeException("@RestAddress must on String Parameter in " + method);
|
||||||
|
}
|
||||||
|
|
||||||
RestParam annpara = param.getAnnotation(RestParam.class);
|
RestParam annpara = param.getAnnotation(RestParam.class);
|
||||||
if (annpara != null) radix = annpara.radix();
|
if (annpara != null) radix = annpara.radix();
|
||||||
if (annpara != null) comment = annpara.comment();
|
if (annpara != null) comment = annpara.comment();
|
||||||
@@ -889,7 +885,7 @@ public final class Rest {
|
|||||||
}
|
}
|
||||||
mv.visitInsn(RETURN);
|
mv.visitInsn(RETURN);
|
||||||
maxLocals++;
|
maxLocals++;
|
||||||
} else if (!sncp && RestOutput.class.isAssignableFrom(returnType)) {
|
} else if (RestOutput.class.isAssignableFrom(returnType)) {
|
||||||
mv.visitVarInsn(ASTORE, maxLocals);
|
mv.visitVarInsn(ASTORE, maxLocals);
|
||||||
if (jsvar == null) {
|
if (jsvar == null) {
|
||||||
mv.visitVarInsn(ALOAD, 0);
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
|
|||||||
@@ -625,7 +625,7 @@ public final class Utility {
|
|||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
return remoteHttpContent(ctx, method, newurl, headers, body);
|
return remoteHttpContent(ctx, method, newurl, headers, body);
|
||||||
}
|
}
|
||||||
InputStream in = rs < 400 ? conn.getInputStream() : conn.getErrorStream();
|
InputStream in = rs < 400 || rs == 404 ? conn.getInputStream() : conn.getErrorStream();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
|
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
|
||||||
byte[] bytes = new byte[1024];
|
byte[] bytes = new byte[1024];
|
||||||
int pos;
|
int pos;
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
|
|||||||
HelloService service = new HelloService();
|
HelloService service = new HelloService();
|
||||||
HttpServer server = new HttpServer();
|
HttpServer server = new HttpServer();
|
||||||
|
|
||||||
server.addRestServlet(HelloService.class, "", service, SimpleRestServlet.class, "/pipes");
|
System.out.println(server.addRestServlet("", HelloService.class, service, SimpleRestServlet.class, "/pipes"));
|
||||||
server.addRestServlet(HelloService.class, "my-res", new HelloService(3), SimpleRestServlet.class, "/pipes");
|
System.out.println(server.addRestServlet("my-res", HelloService.class, new HelloService(3), SimpleRestServlet.class, "/pipes"));
|
||||||
|
|
||||||
DefaultAnyValue conf = DefaultAnyValue.create("port", "" + port);
|
DefaultAnyValue conf = DefaultAnyValue.create("port", "" + port);
|
||||||
server.init(conf);
|
server.init(conf);
|
||||||
|
|||||||
Reference in New Issue
Block a user