From 07ba1255cea8ba7ee1b0b8b5f70ee250139dfd4c Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 22 Jun 2020 11:20:42 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20defaultClass=20=D0=B2=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manangers/LaunchServerGsonManager.java | 3 +- .../socket/response/UnknownResponse.java | 16 +++++++ .../gravit/utils/UniversalJsonAdapter.java | 42 +++++++++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/UnknownResponse.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java index 4b735948..f19ca95f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java @@ -15,6 +15,7 @@ import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager; import pro.gravit.launchserver.socket.WebSocketService; +import pro.gravit.launchserver.socket.response.UnknownResponse; import pro.gravit.launchserver.socket.response.WebSocketServerResponse; import pro.gravit.utils.UniversalJsonAdapter; @@ -34,7 +35,7 @@ public void registerAdapters(GsonBuilder builder) { builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers)); - builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers)); + builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers, UnknownResponse.class)); builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter()); builder.registerTypeAdapter(AuthRequest.AuthPasswordInterface.class, new UniversalJsonAdapter<>(AuthRequest.providers)); builder.registerTypeAdapter(HWIDProvider.class, new UniversalJsonAdapter<>(HWIDProvider.providers)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/UnknownResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/UnknownResponse.java new file mode 100644 index 00000000..af41a0dc --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/UnknownResponse.java @@ -0,0 +1,16 @@ +package pro.gravit.launchserver.socket.response; + +import io.netty.channel.ChannelHandlerContext; +import pro.gravit.launchserver.socket.Client; + +public class UnknownResponse extends SimpleResponse { + @Override + public String getType() { + return "unknown"; + } + + @Override + public void execute(ChannelHandlerContext ctx, Client client) { + sendError("This type of request is not supported"); + } +} diff --git a/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java b/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java index c7681e2d..1b289ce8 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/UniversalJsonAdapter.java @@ -15,41 +15,50 @@ public class UniversalJsonAdapter implements JsonSerializer, JsonDeseriali public final ProviderMap providerMap; public final String name; public final String PROP_NAME; - public final boolean printErrorIfUnknownType; + public Class defaultClass; public UniversalJsonAdapter(ProviderMap providerMap) { this.providerMap = providerMap; this.name = providerMap.getName(); this.PROP_NAME = "type"; - printErrorIfUnknownType = true; } public UniversalJsonAdapter(ProviderMap providerMap, String PROP_NAME) { this.providerMap = providerMap; this.name = providerMap.getName(); this.PROP_NAME = PROP_NAME; - printErrorIfUnknownType = true; } - public UniversalJsonAdapter(ProviderMap providerMap, String name, String PROP_NAME, boolean printErrorIfUnknownType) { + public UniversalJsonAdapter(ProviderMap providerMap, String name, Class defaultClass) { + this.providerMap = providerMap; + this.name = name; + this.defaultClass = defaultClass; + this.PROP_NAME = "type"; + } + + public UniversalJsonAdapter(ProviderMap providerMap, Class defaultClass) { + this.providerMap = providerMap; + this.defaultClass = defaultClass; + this.name = providerMap.getName(); + this.PROP_NAME = "type"; + } + + public UniversalJsonAdapter(ProviderMap providerMap, String name, String PROP_NAME, Class defaultClass) { this.providerMap = providerMap; this.name = name; this.PROP_NAME = PROP_NAME; - this.printErrorIfUnknownType = printErrorIfUnknownType; - } - - public UniversalJsonAdapter(ProviderMap providerMap, String name, boolean printErrorIfUnknownType) { - this.providerMap = providerMap; - this.name = name; - this.PROP_NAME = "type"; - this.printErrorIfUnknownType = printErrorIfUnknownType; + this.defaultClass = defaultClass; } public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = providerMap.getClass(typename); if (cls == null) { - if (printErrorIfUnknownType) LogHelper.error("%s %s not found", name, typename); + //if (printErrorIfUnknownType) LogHelper.error("%s %s not found", name, typename); + if(defaultClass != null) + { + return context.deserialize(json, defaultClass); + } return null; } return context.deserialize(json, cls); @@ -63,10 +72,9 @@ public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext con if (classPath == null && src instanceof TypeSerializeInterface) { classPath = ((TypeSerializeInterface) src).getType(); } - if (classPath == null) { - if (printErrorIfUnknownType) LogHelper.warning("Class %s type null", src.getClass()); - } else jo.add(PROP_NAME, new JsonPrimitive(classPath)); - + if (classPath != null) { + jo.add(PROP_NAME, new JsonPrimitive(classPath)); + } return jo; } }