diff --git a/src/org/redkale/mq/HttpSimpleRequestCoder.java b/src/org/redkale/mq/HttpSimpleRequestCoder.java index 8addd6805..4fbde8c06 100644 --- a/src/org/redkale/mq/HttpSimpleRequestCoder.java +++ b/src/org/redkale/mq/HttpSimpleRequestCoder.java @@ -36,10 +36,11 @@ public class HttpSimpleRequestCoder implements MessageCoder { byte[] headers = MessageCoder.getBytes(data.getHeaders()); byte[] params = MessageCoder.getBytes(data.getParams()); byte[] body = MessageCoder.getBytes(data.getBody()); - int count = 4 + requestURI.length + 2 + remoteAddr.length + 2 + sessionid.length + int count = 1 + 4 + requestURI.length + 2 + remoteAddr.length + 2 + sessionid.length + 2 + contentType.length + headers.length + params.length + 4 + body.length; byte[] bs = new byte[count]; ByteBuffer buffer = ByteBuffer.wrap(bs); + buffer.put((byte) (data.isRpc() ? 'T' : 'F')); buffer.putInt(requestURI.length); if (requestURI.length > 0) buffer.put(requestURI); buffer.putChar((char) remoteAddr.length); @@ -60,6 +61,7 @@ public class HttpSimpleRequestCoder implements MessageCoder { if (data == null) return null; ByteBuffer buffer = ByteBuffer.wrap(data); HttpSimpleRequest req = new HttpSimpleRequest(); + req.setRpc(buffer.get() == 'T'); req.setRequestURI(MessageCoder.getLongString(buffer)); req.setRemoteAddr(MessageCoder.getShortString(buffer)); req.setSessionid(MessageCoder.getShortString(buffer)); diff --git a/src/org/redkale/net/http/HttpMapping.java b/src/org/redkale/net/http/HttpMapping.java index 534d5147f..e12590df3 100644 --- a/src/org/redkale/net/http/HttpMapping.java +++ b/src/org/redkale/net/http/HttpMapping.java @@ -30,6 +30,11 @@ public @interface HttpMapping { */ int actionid() default 0; + /** + * 请求地址 + * + * @return String + */ String url(); /** @@ -41,6 +46,13 @@ public @interface HttpMapping { */ int cacheseconds() default 0; + /** + * 是否只接受RPC请求, 默认为false + * + * @return 默认false + */ + boolean rpconly() default false; + /** * 是否鉴权,默认需要鉴权
* diff --git a/src/org/redkale/net/http/HttpRequest.java b/src/org/redkale/net/http/HttpRequest.java index d182ca41f..94665de75 100644 --- a/src/org/redkale/net/http/HttpRequest.java +++ b/src/org/redkale/net/http/HttpRequest.java @@ -36,6 +36,8 @@ public class HttpRequest extends Request { public static final String SESSIONID_NAME = "JSESSIONID"; + protected boolean rpc; + @Comment("Method GET/POST/...") protected String method; @@ -97,6 +99,7 @@ public class HttpRequest extends Request { super(context, null); this.remoteAddrHeader = null; if (req != null) { + this.rpc = req.rpc; if (req.getBody() != null) this.array.write(req.getBody()); if (req.getHeaders() != null) this.headers.putAll(req.getHeaders()); if (req.getParams() != null) this.params.putAll(req.getParams()); @@ -122,6 +125,7 @@ public class HttpRequest extends Request { } req.setRequestURI(uri); req.setSessionid(getSessionid(false)); + req.setRpc(this.rpc); return req; } diff --git a/src/org/redkale/net/http/HttpServlet.java b/src/org/redkale/net/http/HttpServlet.java index 70c88051c..670d25a64 100644 --- a/src/org/redkale/net/http/HttpServlet.java +++ b/src/org/redkale/net/http/HttpServlet.java @@ -44,6 +44,10 @@ public class HttpServlet extends Servlet @Override public void execute(HttpRequest request, HttpResponse response) throws IOException { InnerActionEntry entry = (InnerActionEntry) request.attachment; + if (entry.rpconly && !request.rpc) { + response.finish(503, null); + return; + } if (entry.cacheseconds > 0) {//有缓存设置 CacheEntry ce = entry.cache.get(request.getRequestURI()); if (ce != null && ce.time + entry.cacheseconds > System.currentTimeMillis()) { //缓存有效 @@ -216,18 +220,19 @@ public class HttpServlet extends Servlet protected static final class InnerActionEntry { InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, HttpServlet servlet) { - this(moduleid, actionid, name, methods, method, auth(method), cacheseconds(method), servlet); + this(moduleid, actionid, name, methods, method, rpconly(method), auth(method), cacheseconds(method), servlet); this.annotations = annotations(method); } //供Rest类使用,参数不能随便更改 - public InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, boolean auth, int cacheseconds, HttpServlet servlet) { + public InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, boolean rpconly, boolean auth, int cacheseconds, HttpServlet servlet) { this.moduleid = moduleid; this.actionid = actionid; this.name = name; this.methods = methods; this.method = method; //rest构建会为null this.servlet = servlet; + this.rpconly = rpconly; this.auth = auth; this.cacheseconds = cacheseconds; this.cache = cacheseconds > 0 ? new ConcurrentHashMap<>() : null; @@ -245,6 +250,11 @@ public class HttpServlet extends Servlet return mapping == null || mapping.auth(); } + protected static boolean rpconly(Method method) { + HttpMapping mapping = method.getAnnotation(HttpMapping.class); + return mapping == null || mapping.rpconly(); + } + protected static int cacheseconds(Method method) { HttpMapping mapping = method.getAnnotation(HttpMapping.class); return mapping == null ? 0 : mapping.cacheseconds(); @@ -273,6 +283,8 @@ public class HttpServlet extends Servlet final int cacheseconds; + final boolean rpconly; + final boolean auth; final int moduleid; diff --git a/src/org/redkale/net/http/HttpSimpleRequest.java b/src/org/redkale/net/http/HttpSimpleRequest.java index 55bcfca96..3a254e117 100644 --- a/src/org/redkale/net/http/HttpSimpleRequest.java +++ b/src/org/redkale/net/http/HttpSimpleRequest.java @@ -23,30 +23,34 @@ import org.redkale.util.*; public class HttpSimpleRequest implements java.io.Serializable { @ConvertColumn(index = 1) + @Comment("是否RPC请求, 该类通常是为RPC创建的,故默认是true") + protected boolean rpc = true; + + @ConvertColumn(index = 2) @Comment("请求的URI") protected String requestURI; - @ConvertColumn(index = 2) + @ConvertColumn(index = 3) @Comment("客户端IP") protected String remoteAddr; - @ConvertColumn(index = 3) + @ConvertColumn(index = 4) @Comment("会话ID") protected String sessionid; - @ConvertColumn(index = 4) + @ConvertColumn(index = 5) @Comment("Content-Type") protected String contentType; - @ConvertColumn(index = 5) + @ConvertColumn(index = 6) @Comment("http header信息") protected Map headers; - @ConvertColumn(index = 6) + @ConvertColumn(index = 7) @Comment("参数信息") protected Map params; - @ConvertColumn(index = 7) + @ConvertColumn(index = 8) @Comment("http body信息") protected byte[] body; //对应HttpRequest.array @@ -63,6 +67,11 @@ public class HttpSimpleRequest implements java.io.Serializable { return req; } + public HttpSimpleRequest rpc(boolean rpc) { + this.rpc = rpc; + return this; + } + public HttpSimpleRequest requestURI(String requestURI) { this.requestURI = requestURI; return this; @@ -175,6 +184,14 @@ public class HttpSimpleRequest implements java.io.Serializable { return this; } + public boolean isRpc() { + return rpc; + } + + public void setRpc(boolean rpc) { + this.rpc = rpc; + } + public String getRequestURI() { return requestURI; } diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index 484fa66ad..f9f1bcc99 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -781,6 +781,7 @@ public final class Rest { final String supDynName = baseServletType.getName().replace('.', '/'); final RestService controller = serviceType.getAnnotation(RestService.class); if (controller != null && controller.ignore()) throw new RuntimeException(serviceType + " is ignore Rest Service Class"); //标记为ignore=true不创建Servlet + final boolean serrpconly = controller != null && controller.rpconly(); ClassLoader loader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader; String stname = serviceType.getSimpleName(); if (stname.startsWith("Service")) { //类似ServiceWatchService这样的类保留第一个Service字样 @@ -858,12 +859,12 @@ public final class Rest { } paramtypes.add(TypeToken.getGenericType(method.getGenericParameterTypes(), serviceType)); if (mappings.length == 0) { //没有Mapping,设置一个默认值 - MappingEntry entry = new MappingEntry(methodidex, null, bigmodulename, method); + MappingEntry entry = new MappingEntry(serrpconly, methodidex, null, bigmodulename, method); if (entrys.contains(entry)) throw new RuntimeException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat"); entrys.add(entry); } else { for (RestMapping mapping : mappings) { - MappingEntry entry = new MappingEntry(methodidex, mapping, defmodulename, method); + MappingEntry entry = new MappingEntry(serrpconly, methodidex, mapping, defmodulename, method); if (entrys.contains(entry)) throw new RuntimeException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat"); entrys.add(entry); } @@ -1169,6 +1170,7 @@ public final class Rest { av0 = mv.visitAnnotation(mappingDesc, true); String url = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name + (reqpath ? "/" : ""); av0.visit("url", url); + av0.visit("rpconly", entry.rpconly); av0.visit("auth", entry.auth); av0.visit("cacheseconds", entry.cacheseconds); av0.visit("actionid", entry.actionid); @@ -1185,6 +1187,7 @@ public final class Rest { av0.visitEnd(); mappingMap.put("url", url); + mappingMap.put("rpconly", entry.rpconly); mappingMap.put("auth", entry.auth); mappingMap.put("cacheseconds", entry.cacheseconds); mappingMap.put("actionid", entry.actionid); @@ -2134,7 +2137,7 @@ public final class Rest { } } - public MappingEntry(int methodidx, RestMapping mapping, final String defmodulename, Method method) { + public MappingEntry(final boolean serrpconly, int methodidx, RestMapping mapping, final String defmodulename, Method method) { if (mapping == null) mapping = DEFAULT__MAPPING; this.methodidx = methodidx; this.ignore = mapping.ignore(); @@ -2148,6 +2151,7 @@ public final class Rest { this.mappingMethod = method; this.methods = mapping.methods(); this.auth = mapping.auth(); + this.rpconly = serrpconly || mapping.rpconly(); this.actionid = mapping.actionid(); this.cacheseconds = mapping.cacheseconds(); this.comment = mapping.comment(); @@ -2181,6 +2185,8 @@ public final class Rest { public final String[] methods; + public final boolean rpconly; + public final boolean auth; public final int actionid; diff --git a/src/org/redkale/net/http/RestMapping.java b/src/org/redkale/net/http/RestMapping.java index 0b025bb1a..056ef97b6 100644 --- a/src/org/redkale/net/http/RestMapping.java +++ b/src/org/redkale/net/http/RestMapping.java @@ -46,6 +46,13 @@ public @interface RestMapping { */ String comment() default ""; + /** + * 是否只接收RPC请求, 对应@HttpMapping.rpconly + * + * @return boolean + */ + boolean rpconly() default true; + /** * 是否鉴权,默认需要鉴权, 对应@HttpMapping.auth * diff --git a/src/org/redkale/net/http/RestService.java b/src/org/redkale/net/http/RestService.java index 77f91cf82..174f735e9 100644 --- a/src/org/redkale/net/http/RestService.java +++ b/src/org/redkale/net/http/RestService.java @@ -43,6 +43,13 @@ public @interface RestService { */ int moduleid() default 0; + /** + * 是否只接受RPC请求, 默认为false, 为true则覆盖所有@RestMapping的方法的rpconly值,都转为true + * + * @return 默认false + */ + boolean rpconly() default false; + /** * 没有标记@RestMapping的方法是否转换, 默认为false *