From 59b0452d476a252768a1d8dee5d973fe0c90b84a Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 8 Aug 2018 22:01:36 +0300 Subject: [PATCH] Added JSON export to /player//raw #620 and /server//raw #506 --- .../data/store/containers/DataContainer.java | 4 ++ .../webserver/pages/PlayerPageHandler.java | 20 ++++-- .../webserver/pages/ServerPageHandler.java | 7 ++ .../webserver/response/cache/PageId.java | 2 + .../response/pages/RawDataResponse.java | 71 +++++++++++++++++++ .../response/pages/RawPlayerDataResponse.java | 17 +++++ .../response/pages/RawServerDataResponse.java | 17 +++++ 7 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawPlayerDataResponse.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawServerDataResponse.java diff --git a/Plan/src/main/java/com/djrapitops/plan/data/store/containers/DataContainer.java b/Plan/src/main/java/com/djrapitops/plan/data/store/containers/DataContainer.java index c2cefb496..96d7ca388 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/store/containers/DataContainer.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/store/containers/DataContainer.java @@ -118,4 +118,8 @@ public class DataContainer { public void clear() { map.clear(); } + + public Map getMap() { + return map; + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/PlayerPageHandler.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/PlayerPageHandler.java index bead14036..922550a12 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/PlayerPageHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/PlayerPageHandler.java @@ -19,6 +19,7 @@ import com.djrapitops.plan.system.webserver.response.cache.PageId; import com.djrapitops.plan.system.webserver.response.cache.ResponseCache; import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse; import com.djrapitops.plan.system.webserver.response.pages.InspectPageResponse; +import com.djrapitops.plan.system.webserver.response.pages.RawPlayerDataResponse; import com.djrapitops.plan.utilities.uuid.UUIDUtility; import java.util.List; @@ -41,17 +42,17 @@ public class PlayerPageHandler extends PageHandler { UUID uuid = UUIDUtility.getUUIDOf(playerName); Locale locale = request.getLocale(); + boolean raw = target.size() >= 2 && target.get(1).equalsIgnoreCase("raw"); + if (uuid == null) { return notFound(locale.getString(ErrorPageLang.UUID_404)); } try { if (Database.getActive().check().isPlayerRegistered(uuid)) { - Response response = ResponseCache.loadResponse(PageId.PLAYER.of(uuid)); - if (!(response instanceof InspectPageResponse)) { - InfoSystem.getInstance().generateAndCachePlayerPage(uuid); - response = ResponseCache.loadResponse(PageId.PLAYER.of(uuid)); + if (raw) { + return ResponseCache.loadResponse(PageId.RAW_PLAYER.of(uuid), () -> new RawPlayerDataResponse(uuid)); } - return response != null ? response : notFound(locale.getString(ErrorPageLang.NO_SERVERS_404)); + return playerResponseOrNotFound(uuid, locale); } else { return notFound(locale.getString(ErrorPageLang.NOT_PLAYED_404)); } @@ -61,6 +62,15 @@ public class PlayerPageHandler extends PageHandler { return InspectPageResponse.getRefreshing(); } + private Response playerResponseOrNotFound(UUID uuid, Locale locale) throws WebException { + Response response = ResponseCache.loadResponse(PageId.PLAYER.of(uuid)); + if (!(response instanceof InspectPageResponse)) { + InfoSystem.getInstance().generateAndCachePlayerPage(uuid); + response = ResponseCache.loadResponse(PageId.PLAYER.of(uuid)); + } + return response != null ? response : notFound(locale.getString(ErrorPageLang.NO_SERVERS_404)); + } + private Response notFound(String error) { return ResponseCache.loadResponse(PageId.NOT_FOUND.of(error), () -> new NotFoundResponse(error)); } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java index c59029c42..9cd01151a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java @@ -15,6 +15,7 @@ import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.cache.PageId; import com.djrapitops.plan.system.webserver.response.cache.ResponseCache; import com.djrapitops.plan.system.webserver.response.pages.AnalysisPageResponse; +import com.djrapitops.plan.system.webserver.response.pages.RawServerDataResponse; import com.djrapitops.plugin.api.Check; import java.util.List; @@ -31,6 +32,12 @@ public class ServerPageHandler extends PageHandler { @Override public Response getResponse(Request request, List target) { UUID serverUUID = getServerUUID(target); + + boolean raw = target.size() >= 2 && target.get(1).equalsIgnoreCase("raw"); + if (raw) { + return ResponseCache.loadResponse(PageId.RAW_SERVER.of(serverUUID), () -> new RawServerDataResponse(serverUUID)); + } + Response response = ResponseCache.loadResponse(PageId.SERVER.of(serverUUID)); if (response != null) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/cache/PageId.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/cache/PageId.java index f1b657004..a5417b6c9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/cache/PageId.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/cache/PageId.java @@ -14,7 +14,9 @@ import java.util.UUID; public enum PageId { SERVER("serverPage:"), + RAW_SERVER("rawServer:"), PLAYER("playerPage:"), + RAW_PLAYER("rawPlayer:"), PLAYERS("playersPage"), ERROR("error:"), diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java new file mode 100644 index 000000000..cf8ec3446 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawDataResponse.java @@ -0,0 +1,71 @@ +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.google.gson.Gson; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Response for sending raw data as JSON when it is inside a DataContainer. + *

+ * This transform class is required to remove Key-Supplier object pollution in the resulting JSON, as well as to remove + * the effects of the caching layer. + * + * @author Rsl1122 + */ +public class RawDataResponse extends Response { + + public RawDataResponse(DataContainer dataContainer) { + super(ResponseType.JSON); + + Map values = mapToNormalMap(dataContainer); + + super.setHeader("HTTP/1.1 200 OK"); + + Gson gson = new Gson(); + super.setContent(gson.toJson(values)); + } + + private Map mapToNormalMap(DataContainer player) { + Map values = new HashMap<>(); + player.getMap().forEach((key, value) -> + { + Object object = value.get(); + if (object instanceof DataContainer) { + object = mapToNormalMap((DataContainer) object); + } + if (object instanceof Map) { + object = handleMap((Map) object); + } + if (object instanceof List) { + object = handleList((List) object); + } + values.put(key.getKeyName(), object); + } + ); + return values; + } + + private List handleList(List object) { + List list = object; + if (list.stream().findAny().orElse(null) instanceof DataContainer) { + return list.stream().map((obj) -> mapToNormalMap((DataContainer) obj)).collect(Collectors.toList()); + } + return list; + } + + private Map handleMap(Map object) { + Map map = object; + if (map.values().stream().findAny().orElse(null) instanceof DataContainer) { + Map newMap = new HashMap<>(); + map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value))); + return newMap; + } + return map; + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawPlayerDataResponse.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawPlayerDataResponse.java new file mode 100644 index 000000000..5e5970c71 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawPlayerDataResponse.java @@ -0,0 +1,17 @@ +package com.djrapitops.plan.system.webserver.response.pages; + +import com.djrapitops.plan.system.cache.CacheSystem; + +import java.util.UUID; + +/** + * Raw Data JSON response for a Player. + * + * @author Rsl1122 + */ +public class RawPlayerDataResponse extends RawDataResponse { + + public RawPlayerDataResponse(UUID uuid) { + super(CacheSystem.getInstance().getDataContainerCache().getPlayerContainer(uuid)); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawServerDataResponse.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawServerDataResponse.java new file mode 100644 index 000000000..814198d15 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/RawServerDataResponse.java @@ -0,0 +1,17 @@ +package com.djrapitops.plan.system.webserver.response.pages; + +import com.djrapitops.plan.system.cache.CacheSystem; + +import java.util.UUID; + +/** + * Raw Data JSON response for a Server. + * + * @author Rsl1122 + */ +public class RawServerDataResponse extends RawDataResponse { + + public RawServerDataResponse(UUID serverUUID) { + super(CacheSystem.getInstance().getDataContainerCache().getAnalysisContainer(serverUUID).getServerContainer()); + } +} \ No newline at end of file