From 7b5739995eed260fa7927d76f6babb982dd74ec6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 30 Apr 2019 15:35:31 +0300 Subject: [PATCH] JSONResponse --- .../results/ExtensionNumberData.java | 4 +++ .../webserver/response/data/JSONResponse.java | 30 +++++++++++++++++++ .../response/pages/RawDataResponse.java | 27 ++++------------- 3 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/data/JSONResponse.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java index a4907b7ac..a8e784325 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/results/ExtensionNumberData.java @@ -47,4 +47,8 @@ public class ExtensionNumberData implements ExtensionData { public String getFormattedValue(Formatter formatter) { return formatter.apply(value); } + + public long getRawValue() { + return value; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/data/JSONResponse.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/data/JSONResponse.java new file mode 100644 index 000000000..3a8354511 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/data/JSONResponse.java @@ -0,0 +1,30 @@ +package com.djrapitops.plan.system.webserver.response.data; + +import com.djrapitops.plan.system.webserver.response.Response; +import com.djrapitops.plan.system.webserver.response.ResponseType; + +/** + * Generic JSON response implemented using Gson. + *

+ * Returns a JSON version of the given object. + * + * @author Rsl1122 + */ +public class JSONResponse extends Response { + + public JSONResponse(T object) { + super(ResponseType.JSON); + + super.setHeader("HTTP/1.1 200 OK"); + + try { + Class gsonClass = Class.forName("com.google.gson.Gson"); + Object gson = gsonClass.getConstructor().newInstance(); + Object json = gsonClass.getMethod("toJson", Object.class).invoke(gson, object); + + super.setContent(json.toString()); + } catch (ReflectiveOperationException e) { + super.setContent("{\"error\":\"Gson for json responses not available on this server: " + e.toString() + "\"}"); + } + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java index c4ea76d41..a64425136 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java @@ -17,8 +17,7 @@ package com.djrapitops.plan.system.webserver.response.pages; import com.djrapitops.plan.data.store.containers.DataContainer; -import com.djrapitops.plan.system.webserver.response.Response; -import com.djrapitops.plan.system.webserver.response.ResponseType; +import com.djrapitops.plan.system.webserver.response.data.JSONResponse; import java.util.HashMap; import java.util.List; @@ -33,27 +32,13 @@ import java.util.stream.Collectors; * * @author Rsl1122 */ -public class RawDataResponse extends Response { +public class RawDataResponse extends JSONResponse> { public RawDataResponse(DataContainer dataContainer) { - super(ResponseType.JSON); - - Map values = mapToNormalMap(dataContainer); - - super.setHeader("HTTP/1.1 200 OK"); - - try { - Class gsonClass = Class.forName("com.google.gson.Gson"); - Object gson = gsonClass.getConstructor().newInstance(); - Object json = gsonClass.getMethod("toJson", Object.class).invoke(gson, values); - - super.setContent(json.toString()); - } catch (ReflectiveOperationException e) { - super.setContent("{\"error\":\"Gson for raw json responses not available on this server: " + e.toString() + "\"}"); - } + super(mapToNormalMap(dataContainer)); } - private Map mapToNormalMap(DataContainer player) { + private static Map mapToNormalMap(DataContainer player) { Map values = new HashMap<>(); player.getMap().forEach((key, value) -> { @@ -72,14 +57,14 @@ public class RawDataResponse extends Response { return values; } - private List handleList(List list) { + private static List handleList(List list) { if (list.stream().findAny().orElse(null) instanceof DataContainer) { return (List) list.stream().map((obj) -> mapToNormalMap((DataContainer) obj)).collect(Collectors.toList()); } return list; } - private Map handleMap(Map map) { + private static Map handleMap(Map map) { if (map.values().stream().findAny().orElse(null) instanceof DataContainer) { Map newMap = new HashMap<>(); map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value)));