Added JSON export to /player/<name>/raw #620 and /server/<name>/raw #506

This commit is contained in:
Rsl1122 2018-08-08 22:01:36 +03:00
parent 8da9dc643b
commit 59b0452d47
7 changed files with 133 additions and 5 deletions

View File

@ -118,4 +118,8 @@ public class DataContainer {
public void clear() {
map.clear();
}
public Map<Key, Supplier> getMap() {
return map;
}
}

View File

@ -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));
}

View File

@ -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<String> 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) {

View File

@ -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:"),

View File

@ -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.
* <p>
* 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<String, Object> values = mapToNormalMap(dataContainer);
super.setHeader("HTTP/1.1 200 OK");
Gson gson = new Gson();
super.setContent(gson.toJson(values));
}
private Map<String, Object> mapToNormalMap(DataContainer player) {
Map<String, Object> 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<Object> 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<Object, Object> map = object;
if (map.values().stream().findAny().orElse(null) instanceof DataContainer) {
Map<Object, Object> newMap = new HashMap<>();
map.forEach((key, value) -> newMap.put(key, mapToNormalMap((DataContainer) value)));
return newMap;
}
return map;
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}