增加自定义Exception

This commit is contained in:
Redkale
2023-01-09 10:37:30 +08:00
parent da96d2ef9f
commit 645ecc3a32
13 changed files with 296 additions and 194 deletions

View File

@@ -426,7 +426,7 @@ public class HttpDispatcherServlet extends DispatcherServlet<String, HttpContext
}
if (this.allMapStrings.containsKey(mappingPath)) {
Class old = this.allMapStrings.get(mappingPath);
throw new RuntimeException("mapping [" + mappingPath + "] repeat on " + old.getName() + " and " + servlet.getClass().getName());
throw new HttpException("mapping [" + mappingPath + "] repeat on " + old.getName() + " and " + servlet.getClass().getName());
}
this.allMapStrings.put(mappingPath, servlet.getClass());
}

View File

@@ -0,0 +1,33 @@
/*
*
*/
package org.redkale.net.http;
/**
* Http自定义异常类
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
public class HttpException extends RuntimeException {
public HttpException() {
super();
}
public HttpException(String s) {
super(s);
}
public HttpException(String message, Throwable cause) {
super(message, cause);
}
public HttpException(Throwable cause) {
super(cause);
}
}

View File

@@ -326,7 +326,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
}
mapfield.set(servlet, map);
} catch (Exception e) {
throw new RuntimeException(serviceType + " generate rest servlet error", e);
throw new HttpException(serviceType + " generate rest servlet error", e);
}
if (first) {
this.dispatcher.addServlet(servlet, prefix, sncp ? Sncp.getConf(service) : null);
@@ -514,19 +514,19 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
if (rpcAuthenticatorConfig != null) {
String impl = rpcAuthenticatorConfig.getValue("authenticator", "").trim();
if (impl.isEmpty()) {
throw new RuntimeException("init HttpRpcAuthenticator(" + impl + ") error");
throw new HttpException("init HttpRpcAuthenticator(" + impl + ") error");
}
try {
Class implClass = serverClassLoader.loadClass(impl);
if (!HttpRpcAuthenticator.class.isAssignableFrom(implClass)) {
throw new RuntimeException("" + impl + " not HttpRpcAuthenticator implement class");
throw new HttpException("" + impl + " not HttpRpcAuthenticator implement class");
}
RedkaleClassLoader.putReflectionPublicConstructors(implClass, implClass.getName());
contextConfig.rpcAuthenticator = (HttpRpcAuthenticator) implClass.getConstructor().newInstance();
} catch (RuntimeException ex) {
throw ex;
} catch (Exception e) {
throw new RuntimeException("init HttpRpcAuthenticator(" + impl + ") error", e);
throw new HttpException("init HttpRpcAuthenticator(" + impl + ") error", e);
}
}
return new HttpContext(contextConfig);

View File

@@ -282,7 +282,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
if (nameset.get(name) != clz) {
continue;
}
throw new RuntimeException(this.getClass().getSimpleName() + " have two same " + HttpMapping.class.getSimpleName() + "(" + name + ")");
throw new HttpException(this.getClass().getSimpleName() + " have two same " + HttpMapping.class.getSimpleName() + "(" + name + ")");
}
nameset.put(name, clz);
map.put(name, new ActionEntry(serviceid, actionid, name, methods, method, createActionServlet(method)));
@@ -480,7 +480,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), field);
return instance;
} catch (Exception ex) {
throw new RuntimeException(ex);
throw new HttpException(ex);
}
}

View File

@@ -188,7 +188,7 @@ public final class Rest {
return JsonFactory.create().skipAllIgnore(true);
}
if (types.contains(rc.type())) {
throw new RuntimeException("@RestConvert type(" + rc.type() + ") repeat");
throw new RestException("@RestConvert type(" + rc.type() + ") repeat");
}
if (rc.skipIgnore()) {
childFactory.registerSkipIgnore(rc.type());
@@ -310,17 +310,17 @@ public final class Rest {
public static <T extends WebSocketServlet> T createRestWebSocketServlet(final ClassLoader classLoader, final Class<? extends WebSocket> webSocketType, MessageAgent messageAgent) {
if (webSocketType == null) {
throw new RuntimeException("Rest WebSocket Class is null on createRestWebSocketServlet");
throw new RestException("Rest WebSocket Class is null on createRestWebSocketServlet");
}
if (Modifier.isAbstract(webSocketType.getModifiers())) {
throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") cannot abstract on createRestWebSocketServlet");
throw new RestException("Rest WebSocket Class(" + webSocketType + ") cannot abstract on createRestWebSocketServlet");
}
if (Modifier.isFinal(webSocketType.getModifiers())) {
throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") cannot final on createRestWebSocketServlet");
throw new RestException("Rest WebSocket Class(" + webSocketType + ") cannot final on createRestWebSocketServlet");
}
final RestWebSocket rws = webSocketType.getAnnotation(RestWebSocket.class);
if (rws == null || rws.ignore()) {
throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") have not @RestWebSocket or @RestWebSocket.ignore=true on createRestWebSocketServlet");
throw new RestException("Rest WebSocket Class(" + webSocketType + ") have not @RestWebSocket or @RestWebSocket.ignore=true on createRestWebSocketServlet");
}
boolean valid = false;
for (Constructor c : webSocketType.getDeclaredConstructors()) {
@@ -330,14 +330,14 @@ public final class Rest {
}
}
if (!valid) {
throw new RuntimeException("Rest WebSocket Class(" + webSocketType + ") must have public or protected Constructor on createRestWebSocketServlet");
throw new RestException("Rest WebSocket Class(" + webSocketType + ") must have public or protected Constructor on createRestWebSocketServlet");
}
final String rwsname = ResourceFactory.formatResourceName(rws.name());
if (!checkName(rws.catalog())) {
throw new RuntimeException(webSocketType.getName() + " have illegal " + RestWebSocket.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
throw new RestException(webSocketType.getName() + " have illegal " + RestWebSocket.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
}
if (!checkName(rwsname)) {
throw new RuntimeException(webSocketType.getName() + " have illegal " + RestWebSocket.class.getSimpleName() + ".name, only 0-9 a-z A-Z _ cannot begin 0-9");
throw new RestException(webSocketType.getName() + " have illegal " + RestWebSocket.class.getSimpleName() + ".name, only 0-9 a-z A-Z _ cannot begin 0-9");
}
//----------------------------------------------------------------------------------------
@@ -354,13 +354,13 @@ public final class Rest {
continue;
}
if (Modifier.isStatic(field.getModifiers())) {
throw new RuntimeException(field + " cannot static on createRestWebSocketServlet");
throw new RestException(field + " cannot static on createRestWebSocketServlet");
}
if (Modifier.isFinal(field.getModifiers())) {
throw new RuntimeException(field + " cannot final on createRestWebSocketServlet");
throw new RestException(field + " cannot final on createRestWebSocketServlet");
}
if (!Modifier.isPublic(field.getModifiers()) && !Modifier.isProtected(field.getModifiers())) {
throw new RuntimeException(field + " must be public or protected on createRestWebSocketServlet");
throw new RestException(field + " must be public or protected on createRestWebSocketServlet");
}
resourcesFieldNameSet.add(field.getName());
resourcesFieldSet.add(field);
@@ -391,25 +391,25 @@ public final class Rest {
}
String name = rom.name();
if (!"*".equals(name) && !checkName(name)) {
throw new RuntimeException("@RestOnMessage.name contains illegal characters on (" + method + ")");
throw new RestException("@RestOnMessage.name contains illegal characters on (" + method + ")");
}
if (Modifier.isFinal(method.getModifiers())) {
throw new RuntimeException("@RestOnMessage method can not final but (" + method + ")");
throw new RestException("@RestOnMessage method can not final but (" + method + ")");
}
if (Modifier.isStatic(method.getModifiers())) {
throw new RuntimeException("@RestOnMessage method can not static but (" + method + ")");
throw new RestException("@RestOnMessage method can not static but (" + method + ")");
}
if (method.getReturnType() != void.class) {
throw new RuntimeException("@RestOnMessage method must return void but (" + method + ")");
throw new RestException("@RestOnMessage method must return void but (" + method + ")");
}
if (method.getExceptionTypes().length > 0) {
throw new RuntimeException("@RestOnMessage method can not throw exception but (" + method + ")");
throw new RestException("@RestOnMessage method can not throw exception but (" + method + ")");
}
if (name.isEmpty()) {
throw new RuntimeException(method + " RestOnMessage.name is empty createRestWebSocketServlet");
throw new RestException(method + " RestOnMessage.name is empty createRestWebSocketServlet");
}
if (messageNames.contains(name)) {
throw new RuntimeException(method + " repeat RestOnMessage.name(" + name + ") createRestWebSocketServlet");
throw new RestException(method + " repeat RestOnMessage.name(" + name + ") createRestWebSocketServlet");
}
messageNames.add(name);
if ("*".equals(name)) {
@@ -649,7 +649,7 @@ public final class Rest {
paramname = names.get(j);
}
if (paramnames.contains(paramname)) {
throw new RuntimeException(method + " has same @RestParam.name");
throw new RestException(method + " has same @RestParam.name");
}
paramnames.add(paramname);
paramap.put(paramname, param);
@@ -971,7 +971,7 @@ public final class Rest {
}
return servlet;
} catch (Exception e) {
throw new RuntimeException(e);
throw new RestException(e);
}
}
@@ -979,19 +979,19 @@ public final class Rest {
final Class<T> baseServletType, final Class<? extends Service> serviceType) {
if (baseServletType == null || serviceType == null) {
throw new RuntimeException(" Servlet or Service is null Class on createRestServlet");
throw new RestException(" Servlet or Service is null Class on createRestServlet");
}
if (!HttpServlet.class.isAssignableFrom(baseServletType)) {
throw new RuntimeException(baseServletType + " is not HttpServlet Class on createRestServlet");
throw new RestException(baseServletType + " is not HttpServlet Class on createRestServlet");
}
int mod = baseServletType.getModifiers();
if (!java.lang.reflect.Modifier.isPublic(mod)) {
throw new RuntimeException(baseServletType + " is not Public Class on createRestServlet");
throw new RestException(baseServletType + " is not Public Class on createRestServlet");
}
if (java.lang.reflect.Modifier.isAbstract(mod)) {
for (Method m : baseServletType.getDeclaredMethods()) {
if (java.lang.reflect.Modifier.isAbstract(m.getModifiers())) { //@since 2.4.0
throw new RuntimeException(baseServletType + " cannot contains a abstract Method on " + baseServletType);
throw new RestException(baseServletType + " cannot contains a abstract Method on " + baseServletType);
}
}
}
@@ -1032,13 +1032,13 @@ public final class Rest {
HttpUserType hut = baseServletType.getAnnotation(HttpUserType.class);
final Class userType = (userType0 == null || userType0 == Object.class) ? (hut == null ? null : hut.value()) : userType0;
if (userType != null && (userType.isPrimitive() || userType.getName().startsWith("java.") || userType.getName().startsWith("javax."))) {
throw new RuntimeException(HttpUserType.class.getSimpleName() + " must be a JavaBean but found " + userType);
throw new RestException(HttpUserType.class.getSimpleName() + " must be a JavaBean but found " + userType);
}
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
throw new RestException(serviceType + " is ignore Rest Service Class"); //标记为ignore=true不创建Servlet
}
final boolean serrpconly = controller != null && controller.rpconly();
ClassLoader loader = classLoader == null ? Thread.currentThread().getContextClassLoader() : classLoader;
@@ -1485,10 +1485,10 @@ public final class Rest {
final String bigmodulename = getWebModuleName(serviceType);
final String catalog = controller == null ? "" : controller.catalog();
if (!checkName(catalog)) {
throw new RuntimeException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
throw new RestException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
}
if (!checkName(defmodulename)) {
throw new RuntimeException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9");
throw new RestException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9");
}
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
FieldVisitor fv;
@@ -1516,10 +1516,10 @@ public final class Rest {
int methodidex = 0;
final MessageMultiConsumer mmc = serviceType.getAnnotation(MessageMultiConsumer.class);
if (mmc != null && (mmc.module() == null || mmc.module().isEmpty())) {
throw new RuntimeException("@" + MessageMultiConsumer.class.getSimpleName() + ".module can not empty in " + serviceType.getName());
throw new RestException("@" + MessageMultiConsumer.class.getSimpleName() + ".module can not empty in " + serviceType.getName());
}
if (mmc != null && !checkName2(mmc.module())) {
throw new RuntimeException(serviceType.getName() + " have illegal " + MessageMultiConsumer.class.getSimpleName() + ".module, only 0-9 a-z A-Z _ - . cannot begin 0-9");
throw new RestException(serviceType.getName() + " have illegal " + MessageMultiConsumer.class.getSimpleName() + ".module, only 0-9 a-z A-Z _ - . cannot begin 0-9");
}
if (mmc != null) {
MethodDebugVisitor.visitAnnotation(cw.visitAnnotation(Type.getDescriptor(mmc.annotationType()), true), mmc);
@@ -1577,26 +1577,26 @@ public final class Rest {
if (extypes.length > 0) {
for (Class exp : extypes) {
if (!RuntimeException.class.isAssignableFrom(exp) && !IOException.class.isAssignableFrom(exp)) {
throw new RuntimeException("@" + RestMapping.class.getSimpleName() + " only for method(" + method + ") with throws IOException");
throw new RestException("@" + RestMapping.class.getSimpleName() + " only for method(" + method + ") with throws IOException");
}
}
}
if (mmc != null && method.getReturnType() != void.class) {
throw new RuntimeException("@" + RestMapping.class.getSimpleName() + " only for method(" + method + ") with return void by @" + MessageMultiConsumer.class.getSimpleName() + " Service");
throw new RestException("@" + RestMapping.class.getSimpleName() + " only for method(" + method + ") with return void by @" + MessageMultiConsumer.class.getSimpleName() + " Service");
}
paramTypes.add(TypeToken.getGenericType(method.getGenericParameterTypes(), serviceType));
retvalTypes.add(formatRestReturnType(method, serviceType));
if (mappings.length == 0) { //没有Mapping设置一个默认值
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");
throw new RestException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
}
entrys.add(entry);
} else {
for (RestMapping mapping : mappings) {
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");
throw new RestException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
}
entrys.add(entry);
}
@@ -1786,58 +1786,58 @@ public final class Rest {
RestHeader annhead = param.getAnnotation(RestHeader.class);
if (annhead != null) {
if (ptype != String.class && ptype != InetSocketAddress.class) {
throw new RuntimeException("@RestHeader must on String or InetSocketAddress Parameter in " + method);
throw new RestException("@RestHeader must on String or InetSocketAddress Parameter in " + method);
}
n = annhead.name();
radix = annhead.radix();
comment = annhead.comment();
required = false;
if (n.isEmpty()) {
throw new RuntimeException("@RestHeader.value is illegal in " + method);
throw new RestException("@RestHeader.value is illegal 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);
throw new RestException("@RestCookie and @RestHeader cannot on the same Parameter in " + method);
}
if (ptype != String.class) {
throw new RuntimeException("@RestCookie must on String Parameter in " + method);
throw new RestException("@RestCookie must on String Parameter in " + method);
}
n = anncookie.name();
radix = anncookie.radix();
comment = anncookie.comment();
required = false;
if (n.isEmpty()) {
throw new RuntimeException("@RestCookie.value is illegal in " + method);
throw new RestException("@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);
throw new RestException("@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);
throw new RestException("@RestSessionid and @RestCookie cannot on the same Parameter in " + method);
}
if (ptype != String.class) {
throw new RuntimeException("@RestSessionid must on String Parameter in " + method);
throw new RestException("@RestSessionid must on String Parameter in " + method);
}
required = false;
}
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);
throw new RestException("@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);
throw new RestException("@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);
throw new RestException("@RestAddress and @RestSessionid cannot on the same Parameter in " + method);
}
if (ptype != String.class) {
throw new RuntimeException("@RestAddress must on String Parameter in " + method);
throw new RestException("@RestAddress must on String Parameter in " + method);
}
comment = annaddr.comment();
required = false;
@@ -1845,19 +1845,19 @@ public final class Rest {
RestLocale annlocale = param.getAnnotation(RestLocale.class);
if (annlocale != null) {
if (annhead != null) {
throw new RuntimeException("@RestLocale and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestLocale and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestLocale and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestLocale and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestLocale and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestLocale and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestLocale and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestLocale and @RestAddress cannot on the same Parameter in " + method);
}
if (ptype != String.class) {
throw new RuntimeException("@RestAddress must on String Parameter in " + method);
throw new RestException("@RestAddress must on String Parameter in " + method);
}
comment = annlocale.comment();
required = false;
@@ -1865,52 +1865,52 @@ public final class Rest {
RestBody annbody = param.getAnnotation(RestBody.class);
if (annbody != null) {
if (annhead != null) {
throw new RuntimeException("@RestBody and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestBody and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestBody and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestBody and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestBody and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestBody and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestBody and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestBody and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestBody and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestBody and @RestLocale cannot on the same Parameter in " + method);
}
if (ptype.isPrimitive()) {
throw new RuntimeException("@RestBody cannot on primitive type Parameter in " + method);
throw new RestException("@RestBody cannot on primitive type Parameter in " + method);
}
comment = annbody.comment();
}
RestUploadFile annfile = param.getAnnotation(RestUploadFile.class);
if (annfile != null) {
if (mupload != null) {
throw new RuntimeException("@RestUploadFile repeat in " + method);
throw new RestException("@RestUploadFile repeat in " + method);
}
mupload = annfile;
muploadType = ptype;
if (annhead != null) {
throw new RuntimeException("@RestUploadFile and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestUploadFile and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestUploadFile and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestUploadFile and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestUploadFile and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestLocale cannot on the same Parameter in " + method);
}
if (annbody != null) {
throw new RuntimeException("@RestUploadFile and @RestBody cannot on the same Parameter in " + method);
throw new RestException("@RestUploadFile and @RestBody cannot on the same Parameter in " + method);
}
if (ptype != byte[].class && ptype != File.class && ptype != File[].class) {
throw new RuntimeException("@RestUploadFile must on byte[] or File or File[] Parameter in " + method);
throw new RestException("@RestUploadFile must on byte[] or File or File[] Parameter in " + method);
}
comment = annfile.comment();
}
@@ -1918,28 +1918,28 @@ public final class Rest {
RestURI annuri = param.getAnnotation(RestURI.class);
if (annuri != null) {
if (annhead != null) {
throw new RuntimeException("@RestURI and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestURI and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestURI and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestURI and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestURI and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestLocale cannot on the same Parameter in " + method);
}
if (annbody != null) {
throw new RuntimeException("@RestURI and @RestBody cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestBody cannot on the same Parameter in " + method);
}
if (annfile != null) {
throw new RuntimeException("@RestURI and @RestUploadFile cannot on the same Parameter in " + method);
throw new RestException("@RestURI and @RestUploadFile cannot on the same Parameter in " + method);
}
if (ptype != String.class) {
throw new RuntimeException("@RestURI must on String Parameter in " + method);
throw new RestException("@RestURI must on String Parameter in " + method);
}
comment = annuri.comment();
}
@@ -1947,28 +1947,28 @@ public final class Rest {
RestUserid userid = param.getAnnotation(RestUserid.class);
if (userid != null) {
if (annhead != null) {
throw new RuntimeException("@RestUserid and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestUserid and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestUserid and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestUserid and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestUserid and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestLocale cannot on the same Parameter in " + method);
}
if (annbody != null) {
throw new RuntimeException("@RestUserid and @RestBody cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestBody cannot on the same Parameter in " + method);
}
if (annfile != null) {
throw new RuntimeException("@RestUserid and @RestUploadFile cannot on the same Parameter in " + method);
throw new RestException("@RestUserid and @RestUploadFile cannot on the same Parameter in " + method);
}
if (!ptype.isPrimitive() && !java.io.Serializable.class.isAssignableFrom(ptype)) {
throw new RuntimeException("@RestUserid must on java.io.Serializable Parameter in " + method);
throw new RestException("@RestUserid must on java.io.Serializable Parameter in " + method);
}
comment = "";
required = false;
@@ -1977,31 +1977,31 @@ public final class Rest {
RestHeaders annheaders = param.getAnnotation(RestHeaders.class);
if (annheaders != null) {
if (annhead != null) {
throw new RuntimeException("@RestHeaders and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestHeaders and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestHeaders and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestHeaders and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestHeaders and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestLocale cannot on the same Parameter in " + method);
}
if (annbody != null) {
throw new RuntimeException("@RestHeaders and @RestBody cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestBody cannot on the same Parameter in " + method);
}
if (annfile != null) {
throw new RuntimeException("@RestHeaders and @RestUploadFile cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestUploadFile cannot on the same Parameter in " + method);
}
if (userid != null) {
throw new RuntimeException("@RestHeaders and @RestUserid cannot on the same Parameter in " + method);
throw new RestException("@RestHeaders and @RestUserid cannot on the same Parameter in " + method);
}
if (!TYPE_MAP_STRING_STRING.equals(param.getParameterizedType())) {
throw new RuntimeException("@RestHeaders must on Map<String, String> Parameter in " + method);
throw new RestException("@RestHeaders must on Map<String, String> Parameter in " + method);
}
comment = "";
required = false;
@@ -2009,34 +2009,34 @@ public final class Rest {
RestParams annparams = param.getAnnotation(RestParams.class);
if (annparams != null) {
if (annhead != null) {
throw new RuntimeException("@RestParams and @RestHeader cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestHeader cannot on the same Parameter in " + method);
}
if (anncookie != null) {
throw new RuntimeException("@RestParams and @RestCookie cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestCookie cannot on the same Parameter in " + method);
}
if (annsid != null) {
throw new RuntimeException("@RestParams and @RestSessionid cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestSessionid cannot on the same Parameter in " + method);
}
if (annaddr != null) {
throw new RuntimeException("@RestParams and @RestAddress cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestAddress cannot on the same Parameter in " + method);
}
if (annlocale != null) {
throw new RuntimeException("@RestParams and @RestLocale cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestLocale cannot on the same Parameter in " + method);
}
if (annbody != null) {
throw new RuntimeException("@RestParams and @RestBody cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestBody cannot on the same Parameter in " + method);
}
if (annfile != null) {
throw new RuntimeException("@RestParams and @RestUploadFile cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestUploadFile cannot on the same Parameter in " + method);
}
if (userid != null) {
throw new RuntimeException("@RestParams and @RestUserid cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestUserid cannot on the same Parameter in " + method);
}
if (annheaders != null) {
throw new RuntimeException("@RestParams and @RestHeaders cannot on the same Parameter in " + method);
throw new RestException("@RestParams and @RestHeaders cannot on the same Parameter in " + method);
}
if (!TYPE_MAP_STRING_STRING.equals(param.getParameterizedType())) {
throw new RuntimeException("@RestParams must on Map<String, String> Parameter in " + method);
throw new RestException("@RestParams must on Map<String, String> Parameter in " + method);
}
comment = "";
}
@@ -2066,7 +2066,7 @@ public final class Rest {
} else if (ptype == Flipper.class) {
n = "flipper";
} else {
throw new RuntimeException("Parameter " + param.getName() + " not found name by @RestParam in " + method);
throw new RestException("Parameter " + param.getName() + " not found name by @RestParam in " + method);
}
}
if (annhead == null && anncookie == null && annsid == null && annaddr == null && annlocale == null && annbody == null && annfile == null
@@ -2089,7 +2089,7 @@ public final class Rest {
continue;
}
if (mupload != null) {
throw new RuntimeException("@RestUploadFile repeat in " + method + " or field " + field);
throw new RestException("@RestUploadFile repeat in " + method + " or field " + field);
}
mupload = ruf;
muploadType = field.getType();
@@ -2508,7 +2508,7 @@ public final class Rest {
mv.visitVarInsn(ASTORE, maxLocals);
varInsns.add(new int[]{ALOAD, maxLocals});
} else {
throw new RuntimeException(method + " only " + RestParam.class.getSimpleName() + "(#) to Type(primitive class or String)");
throw new RestException(method + " only " + RestParam.class.getSimpleName() + "(#) to Type(primitive class or String)");
}
} else if (pname.charAt(0) == '#') { //从request.getRequstURIPath 中去参数
if (ptype == boolean.class) {
@@ -2590,7 +2590,7 @@ public final class Rest {
mv.visitVarInsn(ASTORE, maxLocals);
varInsns.add(new int[]{ALOAD, maxLocals});
} else {
throw new RuntimeException(method + " only " + RestParam.class.getSimpleName() + "(#) to Type(primitive class or String)");
throw new RestException(method + " only " + RestParam.class.getSimpleName() + "(#) to Type(primitive class or String)");
}
} else if ("&".equals(pname) && ptype == userType) { //当前用户对象的类名
mv.visitVarInsn(ALOAD, 1);
@@ -2736,29 +2736,29 @@ public final class Rest {
continue;
}
if (rh != null && field.getType() != String.class && field.getType() != InetSocketAddress.class) {
throw new RuntimeException("@RestHeader must on String Field in " + field);
throw new RestException("@RestHeader must on String Field in " + field);
}
if (rc != null && field.getType() != String.class) {
throw new RuntimeException("@RestCookie must on String Field in " + field);
throw new RestException("@RestCookie must on String Field in " + field);
}
if (rs != null && field.getType() != String.class) {
throw new RuntimeException("@RestSessionid must on String Field in " + field);
throw new RestException("@RestSessionid must on String Field in " + field);
}
if (ra != null && field.getType() != String.class) {
throw new RuntimeException("@RestAddress must on String Field in " + field);
throw new RestException("@RestAddress must on String Field in " + field);
}
if (rl != null && field.getType() != String.class) {
throw new RuntimeException("@RestLocale must on String Field in " + field);
throw new RestException("@RestLocale must on String Field in " + field);
}
if (rb != null && field.getType().isPrimitive()) {
throw new RuntimeException("@RestBody must on cannot on primitive type Field in " + field);
throw new RestException("@RestBody must on cannot on primitive type Field in " + field);
}
if (ru != null && field.getType() != byte[].class && field.getType() != File.class && field.getType() != File[].class) {
throw new RuntimeException("@RestUploadFile must on byte[] or File or File[] Field in " + field);
throw new RestException("@RestUploadFile must on byte[] or File or File[] Field in " + field);
}
if (ri != null && field.getType() != String.class) {
throw new RuntimeException("@RestURI must on String Field in " + field);
throw new RestException("@RestURI must on String Field in " + field);
}
org.redkale.util.Attribute attr = org.redkale.util.Attribute.create(loop, field);
String attrFieldName;
@@ -3330,7 +3330,7 @@ public final class Rest {
RedkaleClassLoader.putDynClass(n, bs, newLoader.findClass(n));
RedkaleClassLoader.putReflectionClass(n);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
throw new RestException(e);
}
});
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
@@ -3407,7 +3407,7 @@ public final class Rest {
RedkaleClassLoader.putReflectionField(HttpServlet.class.getName(), tmpentrysfield);
return obj;
} catch (Throwable e) {
throw new RuntimeException(e);
throw new RestException(e);
}
}

View File

@@ -0,0 +1,33 @@
/*
*
*/
package org.redkale.net.http;
/**
* Rest自定义异常类
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
public class RestException extends HttpException {
public RestException() {
super();
}
public RestException(String s) {
super(s);
}
public RestException(String message, Throwable cause) {
super(message, cause);
}
public RestException(Throwable cause) {
super(cause);
}
}

View File

@@ -137,7 +137,7 @@ public class WebSocketReadHandler implements CompletionHandler<Integer, ByteBuff
} else if (lengthCode == 0x7F) {//0x7E=127 长度>65535
length = (int) realbuf.getLong();
} else {
throw new RuntimeException("read webSocket packet lengthCode (" + (int) lengthCode + ") error");
throw new HttpException("read webSocket packet lengthCode (" + (int) lengthCode + ") error");
}
byte[] masks0 = null;
if (masked) {
@@ -194,7 +194,7 @@ public class WebSocketReadHandler implements CompletionHandler<Integer, ByteBuff
} else if (lengthCode == 0x7F) {//0x7E=127 长度>65535
length = (int) realbuf.getLong();
} else {
throw new RuntimeException("read webSocket packet lengthCode (" + (int) lengthCode + ") error");
throw new HttpException("read webSocket packet lengthCode (" + (int) lengthCode + ") error");
}
byte[] masks0 = null;
if (masked) {

View File

@@ -7,17 +7,17 @@ package org.redkale.net.http;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.security.*;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.*;
import java.util.logging.*;
import java.util.zip.*;
import org.redkale.annotation.*;
import org.redkale.annotation.Comment;
import org.redkale.annotation.*;
import org.redkale.boot.Application;
import static org.redkale.boot.Application.RESNAME_SERVER_RESFACTORY;
import org.redkale.convert.Convert;
@@ -166,7 +166,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
RedkaleClassLoader.putReflectionDeclaredConstructors(clazz, cryptorClass);
if (resourceFactory != null && this.cryptor != null) resourceFactory.inject(this.cryptor);
} catch (Exception e) {
throw new RuntimeException(e);
throw new HttpException(e);
}
}
}
@@ -389,7 +389,7 @@ public abstract class WebSocketServlet extends HttpServlet implements Resourcabl
try {
return MessageDigest.getInstance("SHA-1");
} catch (Exception e) {
throw new RuntimeException(e);
throw new HttpException(e);
}
}

View File

@@ -57,7 +57,7 @@ public abstract class Sncp {
private Sncp() {
}
public static Uint128 hash(final java.lang.reflect.Method method) {
public static Uint128 actionid(final java.lang.reflect.Method method) {
if (method == null) {
return Uint128.ZERO;
}
@@ -77,6 +77,10 @@ public abstract class Sncp {
return hash(sb.toString());
}
public static Uint128 serviceid(String serviceResourceName, Class serviceResourceType) {
return hash(serviceResourceType.getName() + ':' + serviceResourceName);
}
/**
* 对类名或者name字符串进行hash。
*
@@ -84,7 +88,7 @@ public abstract class Sncp {
*
* @return hash值
*/
public static Uint128 hash(final String name) {
private static Uint128 hash(final String name) {
if (name == null || name.isEmpty()) {
return Uint128.ZERO;
}
@@ -153,7 +157,7 @@ public abstract class Sncp {
ts.setAccessible(true);
return (AnyValue) ts.get(service);
} catch (Exception e) {
throw new RuntimeException(service + " not found " + FIELDPREFIX + "_conf");
throw new SncpException(service + " not found " + FIELDPREFIX + "_conf");
}
}
@@ -166,7 +170,7 @@ public abstract class Sncp {
ts.setAccessible(true);
return (SncpClient) ts.get(service);
} catch (Exception e) {
throw new RuntimeException(service + " not found " + FIELDPREFIX + "_client");
throw new SncpException(service + " not found " + FIELDPREFIX + "_client");
}
}
@@ -179,7 +183,7 @@ public abstract class Sncp {
ts.setAccessible(true);
return (MessageAgent) ts.get(service);
} catch (Exception e) {
throw new RuntimeException(service + " not found " + FIELDPREFIX + "_messageagent");
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageagent");
}
}
@@ -197,7 +201,7 @@ public abstract class Sncp {
c.set(service, messageAgent);
}
} catch (Exception e) {
throw new RuntimeException(service + " not found " + FIELDPREFIX + "_messageagent");
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageagent");
}
}
@@ -222,10 +226,10 @@ public abstract class Sncp {
return;
}
if (Modifier.isFinal(param.getModifiers())) {
throw new RuntimeException("CompletionHandler Type Parameter on {" + method + "} cannot final modifier");
throw new SncpException("CompletionHandler Type Parameter on {" + method + "} cannot final modifier");
}
if (!Modifier.isPublic(param.getModifiers())) {
throw new RuntimeException("CompletionHandler Type Parameter on {" + method + "} must be public modifier");
throw new SncpException("CompletionHandler Type Parameter on {" + method + "} must be public modifier");
}
if (param.isInterface()) {
return;
@@ -245,21 +249,21 @@ public abstract class Sncp {
constructorflag = true;
}
if (!constructorflag) {
throw new RuntimeException(param + " must have a empty parameter Constructor");
throw new SncpException(param + " must have a empty parameter Constructor");
}
for (Method m : param.getMethods()) {
if (m.getName().equals("completed") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s completed method cannot final modifier");
throw new SncpException(param + "'s completed method cannot final modifier");
} else if (m.getName().equals("failed") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s failed method cannot final modifier");
throw new SncpException(param + "'s failed method cannot final modifier");
} else if (m.getName().equals("sncp_getParams") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s sncp_getParams method cannot final modifier");
throw new SncpException(param + "'s sncp_getParams method cannot final modifier");
} else if (m.getName().equals("sncp_setParams") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s sncp_setParams method cannot final modifier");
throw new SncpException(param + "'s sncp_setParams method cannot final modifier");
} else if (m.getName().equals("sncp_setFuture") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s sncp_setFuture method cannot final modifier");
throw new SncpException(param + "'s sncp_setFuture method cannot final modifier");
} else if (m.getName().equals("sncp_getFuture") && Modifier.isFinal(m.getModifiers())) {
throw new RuntimeException(param + "'s sncp_getFuture method cannot final modifier");
throw new SncpException(param + "'s sncp_getFuture method cannot final modifier");
}
}
}
@@ -331,19 +335,17 @@ public abstract class Sncp {
*/
@SuppressWarnings("unchecked")
protected static <T extends Service> Class<? extends T> createLocalServiceClass(ClassLoader classLoader, final String name, final Class<T> serviceImplClass) {
if (serviceImplClass == null) {
return null;
}
Objects.requireNonNull(serviceImplClass);
if (!Service.class.isAssignableFrom(serviceImplClass)) {
return serviceImplClass;
throw new SncpException(serviceImplClass + " is not Service type");
}
ResourceFactory.checkResourceName(name);
int mod = serviceImplClass.getModifiers();
if (!java.lang.reflect.Modifier.isPublic(mod)) {
return serviceImplClass;
throw new SncpException(serviceImplClass + " is not public");
}
if (java.lang.reflect.Modifier.isAbstract(mod)) {
return serviceImplClass;
throw new SncpException(serviceImplClass + " is abstract");
}
final String supDynName = serviceImplClass.getName().replace('.', '/');
final String clientName = SncpClient.class.getName().replace('.', '/');
@@ -362,7 +364,7 @@ public abstract class Sncp {
}
}
if (!normal) {
throw new RuntimeException(serviceImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
throw new SncpException(serviceImplClass + "'s resource name is illegal, must be 0-9 _ a-z A-Z");
}
newDynName += "_" + (normal ? name : hash(name));
}
@@ -568,7 +570,7 @@ public abstract class Sncp {
} catch (RuntimeException rex) {
throw rex;
} catch (Exception ex) {
throw new RuntimeException(ex);
throw new SncpException(ex);
}
}
@@ -843,7 +845,7 @@ public abstract class Sncp {
java.lang.reflect.Method pm = bigPrimitiveClass.getMethod(returnclz.getSimpleName() + "Value");
mv.visitMethodInsn(INVOKEVIRTUAL, bigPrimitiveName, pm.getName(), Type.getMethodDescriptor(pm), false);
} catch (Exception ex) {
throw new RuntimeException(ex); //不可能会发生
throw new SncpException(ex); //不可能会发生
}
if (returnclz == long.class) {
mv.visitInsn(LRETURN);
@@ -908,7 +910,7 @@ public abstract class Sncp {
}
return service;
} catch (Exception ex) {
throw new RuntimeException(ex);
throw new SncpException(ex);
}
}

View File

@@ -73,29 +73,28 @@ public final class SncpClient {
//远程模式, 可能为null
protected Transport remoteGroupTransport;
public <T extends Service> SncpClient(final String serviceName, final Class<T> serviceTypeOrImplClass, final T service, MessageAgent messageAgent, final TransportFactory factory,
public <T extends Service> SncpClient(final String serviceResourceName, final Class<T> serviceTypeOrImplClass, final T service, MessageAgent messageAgent, final TransportFactory factory,
final boolean remote, final Class serviceClass, final InetSocketAddress clientSncpAddress) {
this.remote = remote;
this.messageAgent = messageAgent;
this.messageClient = messageAgent == null ? null : messageAgent.getSncpMessageClient();
this.topic = messageAgent == null ? null : messageAgent.generateSncpReqTopic(service);
Class<?> tn = serviceTypeOrImplClass;
this.serviceClass = serviceClass;
this.serviceVersion = 0; //暂不实现Version
this.clientSncpAddress = clientSncpAddress;
this.name = serviceName;
tn = ResourceFactory.getResourceType(tn);
this.serviceid = Sncp.hash(tn.getName() + ':' + serviceName);
this.name = serviceResourceName;
Class<?> serviceResourceType = ResourceFactory.getResourceType(serviceTypeOrImplClass); //serviceResourceType
this.serviceid = Sncp.serviceid(serviceResourceName, serviceResourceType);
final List<SncpAction> methodens = new ArrayList<>();
//------------------------------------------------------------------------------
for (java.lang.reflect.Method method : parseMethod(serviceClass)) {
methodens.add(new SncpAction(serviceClass, method, Sncp.hash(method)));
methodens.add(new SncpAction(serviceClass, method, Sncp.actionid(method)));
}
this.actions = methodens.toArray(new SncpAction[methodens.size()]);
this.addrBytes = clientSncpAddress == null ? new byte[4] : clientSncpAddress.getAddress().getAddress();
this.addrPort = clientSncpAddress == null ? 0 : clientSncpAddress.getPort();
if (this.addrBytes.length != 4) {
throw new RuntimeException("SNCP clientAddress only support IPv4");
throw new SncpException("SNCP clientAddress only support IPv4");
}
}
@@ -103,7 +102,7 @@ public final class SncpClient {
final List<SncpAction> actions = new ArrayList<>();
//------------------------------------------------------------------------------
for (java.lang.reflect.Method method : parseMethod(serviceClass)) {
actions.add(new SncpAction(serviceClass, method, Sncp.hash(method)));
actions.add(new SncpAction(serviceClass, method, Sncp.actionid(method)));
}
return actions;
}
@@ -204,11 +203,11 @@ public final class SncpClient {
}
//if (onlySncpDyn && method.getAnnotation(SncpDyn.class) == null) continue;
Uint128 actionid = Sncp.hash(method);
Uint128 actionid = Sncp.actionid(method);
Method old = actionids.get(actionid);
if (old != null) {
if (old.getDeclaringClass().equals(method.getDeclaringClass())) {
throw new RuntimeException(serviceClass.getName() + " have one more same action(Method=" + method + ", " + old + ", actionid=" + actionid + ")");
throw new SncpException(serviceClass.getName() + " have one more same action(Method=" + method + ", " + old + ", actionid=" + actionid + ")");
}
continue;
}
@@ -341,7 +340,7 @@ public final class SncpClient {
final int retcode = buffer.getInt();
if (retcode != 0) {
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + "), params=" + JsonConvert.root().convertTo(params));
throw new RuntimeException("remote service(" + action.method + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
throw new SncpException("remote service(" + action.method + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
}
byte[] body = new byte[respBodyLength];
buffer.get(body, 0, respBodyLength);
@@ -411,7 +410,7 @@ public final class SncpClient {
final int retcode = buffer.getInt();
if (retcode != 0) {
logger.log(Level.SEVERE, action.method + " sncp (params: " + convert.convertTo(params) + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + "), params=" + JsonConvert.root().convertTo(params));
throw new RuntimeException("remote service(" + action.method + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
throw new SncpException("remote service(" + action.method + ") deal error (retcode=" + retcode + ", retinfo=" + SncpResponse.getRetCodeInfo(retcode) + ")");
}
if (respBodyLength > buffer.remaining()) { // 数据不全,需要继续读取
@@ -494,23 +493,23 @@ public final class SncpClient {
private void checkResult(long seqid, final SncpAction action, ByteBuffer buffer) {
long rseqid = buffer.getLong();
if (rseqid != seqid) {
throw new RuntimeException("sncp(" + action.method + ") response.seqid = " + seqid + ", but request.seqid =" + rseqid);
throw new SncpException("sncp(" + action.method + ") response.seqid = " + seqid + ", but request.seqid =" + rseqid);
}
if (buffer.getChar() != HEADER_SIZE) {
throw new RuntimeException("sncp(" + action.method + ") buffer receive header.length not " + HEADER_SIZE);
throw new SncpException("sncp(" + action.method + ") buffer receive header.length not " + HEADER_SIZE);
}
Uint128 rserviceid = Uint128.read(buffer);
if (!rserviceid.equals(this.serviceid)) {
throw new RuntimeException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid);
throw new SncpException("sncp(" + action.method + ") response.serviceid = " + serviceid + ", but request.serviceid =" + rserviceid);
}
int version = buffer.getInt();
if (version != this.serviceVersion) {
throw new RuntimeException("sncp(" + action.method + ") response.serviceVersion = " + serviceVersion + ", but request.serviceVersion =" + version);
throw new SncpException("sncp(" + action.method + ") response.serviceVersion = " + serviceVersion + ", but request.serviceVersion =" + version);
}
Uint128 raction = Uint128.read(buffer);
Uint128 actid = action.actionid;
if (!actid.equals(raction)) {
throw new RuntimeException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")");
throw new SncpException("sncp(" + action.method + ") response.actionid = " + action.actionid + ", but request.actionid =(" + raction + ")");
}
buffer.getInt(); //地址
buffer.getChar(); //端口
@@ -551,7 +550,7 @@ public final class SncpClient {
@SuppressWarnings("unchecked")
public SncpAction(final Class clazz, Method method, Uint128 actionid) {
this.actionid = actionid == null ? Sncp.hash(method) : actionid;
this.actionid = actionid == null ? Sncp.actionid(method) : actionid;
Type rt = TypeToken.getGenericType(method.getGenericReturnType(), clazz);
this.resultTypes = rt == void.class ? null : rt;
this.boolReturnTypeFuture = CompletableFuture.class.isAssignableFrom(method.getReturnType());
@@ -572,10 +571,10 @@ public final class SncpClient {
for (int i = 0; i < params.length; i++) {
if (CompletionHandler.class.isAssignableFrom(params[i])) {
if (boolReturnTypeFuture) {
throw new RuntimeException(method + " have both CompletionHandler and CompletableFuture");
throw new SncpException(method + " have both CompletionHandler and CompletableFuture");
}
if (handlerFuncIndex >= 0) {
throw new RuntimeException(method + " have more than one CompletionHandler type parameter");
throw new SncpException(method + " have more than one CompletionHandler type parameter");
}
Sncp.checkAsyncModifier(params[i], method);
handlerFuncIndex = i;
@@ -587,7 +586,7 @@ public final class SncpClient {
for (Annotation ann : anns[i]) {
if (ann.annotationType() == RpcAttachment.class) {
if (handlerAttachIndex >= 0) {
throw new RuntimeException(method + " have more than one @RpcAttachment parameter");
throw new SncpException(method + " have more than one @RpcAttachment parameter");
}
handlerAttachIndex = i;
} else if (ann.annotationType() == RpcTargetAddress.class && SocketAddress.class.isAssignableFrom(params[i])) {
@@ -620,7 +619,7 @@ public final class SncpClient {
this.handlerAttachParamIndex = handlerAttachIndex;
this.paramAttrs = hasattr ? atts : null;
if (this.handlerFuncParamIndex >= 0 && method.getReturnType() != void.class) {
throw new RuntimeException(method + " have CompletionHandler type parameter but return type is not void");
throw new SncpException(method + " have CompletionHandler type parameter but return type is not void");
}
}

View File

@@ -43,12 +43,12 @@ public final class SncpDynServlet extends SncpServlet {
private final HashMap<Uint128, SncpServletAction> actions = new HashMap<>();
public SncpDynServlet(final BsonConvert convert, final String serviceName, final Class serviceOrSourceType, final Service service,
public SncpDynServlet(final BsonConvert convert, final String serviceResourceName, final Class serviceResourceType, final Service service,
final AtomicInteger maxTypeLength, AtomicInteger maxNameLength) {
super(serviceName, serviceOrSourceType, service);
super(serviceResourceName, serviceResourceType, service);
this.maxTypeLength = maxTypeLength;
this.maxNameLength = maxNameLength;
this.serviceid = Sncp.hash(type.getName() + ':' + serviceName);
this.serviceid = Sncp.serviceid(serviceResourceName, serviceResourceType);
Set<Uint128> actionids = new HashSet<>();
RedkaleClassLoader.putReflectionPublicMethods(service.getClass().getName());
for (java.lang.reflect.Method method : service.getClass().getMethods()) {
@@ -79,21 +79,21 @@ public final class SncpDynServlet extends SncpServlet {
}
}
final Uint128 actionid = Sncp.hash(method);
final Uint128 actionid = Sncp.actionid(method);
SncpServletAction action;
try {
action = SncpServletAction.create(service, actionid, method);
} catch (RuntimeException e) {
throw new RuntimeException(method + " create " + SncpServletAction.class.getSimpleName() + " error", e);
throw new SncpException(method + " create " + SncpServletAction.class.getSimpleName() + " error", e);
}
action.convert = convert;
if (actionids.contains(actionid)) {
throw new RuntimeException(type.getName() + " have action(Method=" + method + ", actionid=" + actionid + ") same to (" + actions.get(actionid).method + ")");
throw new SncpException(type.getName() + " have action(Method=" + method + ", actionid=" + actionid + ") same to (" + actions.get(actionid).method + ")");
}
actions.put(actionid, action);
actionids.add(actionid);
}
maxNameLength.set(Math.max(maxNameLength.get(), serviceName.length() + 1));
maxNameLength.set(Math.max(maxNameLength.get(), serviceResourceName.length() + 1));
maxTypeLength.set(Math.max(maxTypeLength.get(), type.getName().length()));
}
@@ -384,7 +384,7 @@ public final class SncpDynServlet extends SncpServlet {
try {
convertFromDesc = Type.getMethodDescriptor(BsonConvert.class.getMethod("convertFrom", java.lang.reflect.Type.class, BsonReader.class));
} catch (Exception ex) {
throw new RuntimeException(ex); //不可能会发生
throw new SncpException(ex); //不可能会发生
}
{ // action方法
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "action", "(" + convertReaderDesc + convertWriterDesc + asyncHandlerDesc + ")V", null, new String[]{"java/lang/Throwable"}));
@@ -397,10 +397,10 @@ public final class SncpDynServlet extends SncpServlet {
for (int i = 0; i < paramClasses.length; i++) { //反序列化方法的每个参数
if (CompletionHandler.class.isAssignableFrom(paramClasses[i])) {
if (boolReturnTypeFuture) {
throw new RuntimeException(method + " have both CompletionHandler and CompletableFuture");
throw new SncpException(method + " have both CompletionHandler and CompletableFuture");
}
if (handlerFuncIndex >= 0) {
throw new RuntimeException(method + " have more than one CompletionHandler type parameter");
throw new SncpException(method + " have more than one CompletionHandler type parameter");
}
Sncp.checkAsyncModifier(paramClasses[i], method);
handlerFuncIndex = i;
@@ -463,7 +463,7 @@ public final class SncpDynServlet extends SncpServlet {
mv.visitTypeInsn(CHECKCAST, bigPrimitiveName);
mv.visitMethodInsn(INVOKEVIRTUAL, bigPrimitiveName, pm.getName(), Type.getMethodDescriptor(pm), false);
} catch (Exception ex) {
throw new RuntimeException(ex); //不可能会发生
throw new SncpException(ex); //不可能会发生
}
mv.visitVarInsn(storecode, store);
} else {
@@ -539,7 +539,7 @@ public final class SncpDynServlet extends SncpServlet {
Method vo = bigClass.getMethod("valueOf", returnClass);
mv.visitMethodInsn(INVOKESTATIC, bigClass.getName().replace('.', '/'), vo.getName(), Type.getMethodDescriptor(vo), false);
} catch (Exception ex) {
throw new RuntimeException(ex); //不可能会发生
throw new SncpException(ex); //不可能会发生
}
}
mv.visitVarInsn(ASTORE, store); //11
@@ -688,7 +688,7 @@ public final class SncpDynServlet extends SncpServlet {
newClazz.getField("service").set(instance, service);
return instance;
} catch (Exception ex) {
throw new RuntimeException(ex); //不可能会发生
throw new SncpException(ex); //不可能会发生
}
}
}

View File

@@ -0,0 +1,33 @@
/*
*
*/
package org.redkale.net.sncp;
/**
* Sncp自定义异常类
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
public class SncpException extends RuntimeException {
public SncpException() {
super();
}
public SncpException(String s) {
super(s);
}
public SncpException(String message, Throwable cause) {
super(message, cause);
}
public SncpException(Throwable cause) {
super(cause);
}
}

View File

@@ -9,7 +9,7 @@ import java.util.Objects;
import java.util.concurrent.*;
import org.redkale.net.*;
import org.redkale.service.Service;
import org.redkale.util.*;
import org.redkale.util.Uint128;
/**
*
@@ -26,10 +26,10 @@ public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, Sncp
protected final Service service;
protected SncpServlet(String serviceName, Class serviceOrSourceType, Service service) {
this.type = serviceOrSourceType;
protected SncpServlet(String serviceResourceName, Class serviceResourceType, Service service) {
this.serviceName = serviceResourceName;
this.type = serviceResourceType;
this.service = service;
this.serviceName = serviceName;
}
public Service getService() {
@@ -56,7 +56,9 @@ public abstract class SncpServlet extends Servlet<SncpContext, SncpRequest, Sncp
@Override
public final boolean equals(Object obj) {
if (!(obj instanceof SncpServlet)) return false;
if (!(obj instanceof SncpServlet)) {
return false;
}
return Objects.equals(getServiceid(), ((SncpServlet) obj).getServiceid());
}