Export of server players_table.json

Breaks viewing of exported html without a webserver.
This is due to XMLHTTPRequests not working well with file:// protocol.
This commit is contained in:
Rsl1122 2019-05-09 15:29:38 +03:00
parent 344fb2a7ca
commit 290cb4d66b
5 changed files with 96 additions and 39 deletions

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.system.export;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.api.exceptions.ParseException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.container.BaseUser;
@ -34,6 +33,7 @@ import com.djrapitops.plan.system.settings.theme.Theme;
import com.djrapitops.plan.system.settings.theme.ThemeVal;
import com.djrapitops.plan.system.webserver.cache.PageId;
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.pages.json.JSONFactory;
import com.djrapitops.plan.system.webserver.response.pages.NetworkPageResponse;
import com.djrapitops.plan.utilities.html.pages.InspectPage;
import com.djrapitops.plan.utilities.html.pages.NetworkPage;
@ -58,7 +58,6 @@ import java.util.*;
@Singleton
public class HtmlExport extends SpecificExport {
private final PlanPlugin plugin;
private final PlanConfig config;
private final Theme theme;
private final PlanFiles files;
@ -69,18 +68,17 @@ public class HtmlExport extends SpecificExport {
@Inject
public HtmlExport(
PlanPlugin plugin,
PlanFiles files,
PlanConfig config,
Theme theme,
DBSystem dbSystem,
PageFactory pageFactory,
JSONFactory jsonFactory,
ServerInfo serverInfo,
ConnectionSystem connectionSystem,
ErrorHandler errorHandler
) {
super(files, serverInfo);
this.plugin = plugin;
super(files, jsonFactory, serverInfo);
this.config = config;
this.theme = theme;
this.files = files;

View File

@ -24,6 +24,7 @@ import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.ExportSettings;
import com.djrapitops.plan.system.webserver.pages.json.JSONFactory;
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
@ -55,10 +56,11 @@ public class JSONExport extends SpecificExport {
PlanConfig config,
DBSystem dbSystem,
ServerInfo serverInfo,
JSONFactory jsonFactory,
ResponseFactory responseFactory,
ErrorHandler errorHandler
) {
super(files, serverInfo);
super(files, jsonFactory, serverInfo);
this.config = config;
this.dbSystem = dbSystem;
this.responseFactory = responseFactory;

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.system.file.PlanFiles;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.cache.PageId;
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.pages.json.JSONFactory;
import com.djrapitops.plan.system.webserver.response.Response;
import java.io.File;
@ -29,6 +30,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
@ -40,13 +42,15 @@ import java.util.UUID;
public abstract class SpecificExport {
private final PlanFiles files;
private final JSONFactory jsonFactory; // Hacky, TODO export needs a rework
protected final ServerInfo serverInfo;
SpecificExport(
PlanFiles files,
ServerInfo serverInfo
JSONFactory jsonFactory, ServerInfo serverInfo
) {
this.files = files;
this.jsonFactory = jsonFactory;
this.serverInfo = serverInfo;
}
@ -117,7 +121,8 @@ public abstract class SpecificExport {
.replace("href=\"plugins/", "href=\"../plugins/")
.replace("href=\"css/", "href=\"../css/")
.replace("src=\"plugins/", "src=\"../plugins/")
.replace("src=\"js/", "src=\"../js/");
.replace("src=\"js/", "src=\"../js/")
.replace("../json/players?serverName=" + serverName, "./players_table.json");
File htmlLocation;
if (serverInfo.getServer().isProxy()) {
@ -126,10 +131,13 @@ public abstract class SpecificExport {
} else {
htmlLocation = new File(getServerFolder(), URLEncoder.encode(serverName, "UTF-8").replace(".", "%2E"));
html = html.replace("../", "../../");
exportPlayersTableJSON(htmlLocation, serverUUID);
}
} else {
htmlLocation = getServerFolder();
exportPlayersTableJSON(htmlLocation, serverUUID);
}
htmlLocation.mkdirs();
File exportFile = new File(htmlLocation, "index.html");
@ -137,4 +145,9 @@ public abstract class SpecificExport {
export(exportFile, lines);
}
private void exportPlayersTableJSON(File htmlLocation, UUID serverUUID) throws IOException {
File exportFile = new File(htmlLocation, "players_table.json");
export(exportFile, Collections.singletonList(jsonFactory.serverPlayersTableJSON(serverUUID)));
}
}

View File

@ -0,0 +1,71 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.webserver.pages.json;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.containers.ServerPlayersTableContainersQuery;
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerPlayerDataTableQuery;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.utilities.formatting.Formatters;
import com.djrapitops.plan.utilities.html.tables.PlayersTableJSONParser;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID;
/**
* Factory with different JSON parsing placed to a single class.
*
* @author Rsl1122
*/
@Singleton
public class JSONFactory {
private final PlanConfig config;
private final DBSystem dbSystem;
private final Formatters formatters;
@Inject
public JSONFactory(
PlanConfig config,
DBSystem dbSystem,
Formatters formatters
) {
this.config = config;
this.dbSystem = dbSystem;
this.formatters = formatters;
}
public String serverPlayersTableJSON(UUID serverUUID) {
Integer xMostRecentPlayers = config.get(DisplaySettings.PLAYERS_PER_SERVER_PAGE);
Integer loginThreshold = config.get(TimeSettings.ACTIVE_LOGIN_THRESHOLD);
Long playtimeThreshold = config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD);
Boolean openPlayerLinksInNewTab = config.get(DisplaySettings.OPEN_PLAYER_LINKS_IN_NEW_TAB);
Database database = dbSystem.getDatabase();
return new PlayersTableJSONParser(
database.query(new ServerPlayersTableContainersQuery(serverUUID)),
database.query(new ExtensionServerPlayerDataTableQuery(serverUUID, xMostRecentPlayers)),
xMostRecentPlayers, playtimeThreshold, loginThreshold, openPlayerLinksInNewTab,
formatters
).toJSONString();
}
}

View File

@ -19,23 +19,15 @@ package com.djrapitops.plan.system.webserver.pages.json;
import com.djrapitops.plan.api.exceptions.WebUserAuthException;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.queries.containers.ServerPlayersTableContainersQuery;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionServerPlayerDataTableQuery;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.DisplaySettings;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.webserver.Request;
import com.djrapitops.plan.system.webserver.RequestTarget;
import com.djrapitops.plan.system.webserver.auth.Authentication;
import com.djrapitops.plan.system.webserver.pages.PageHandler;
import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.data.JSONResponse;
import com.djrapitops.plan.utilities.formatting.Formatters;
import com.djrapitops.plan.utilities.html.tables.PlayersTableJSONParser;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -51,41 +43,22 @@ import java.util.UUID;
@Singleton
public class PlayersTableJSONHandler implements PageHandler {
private final PlanConfig config;
private final DBSystem dbSystem;
private final Formatters formatters;
private final JSONFactory jsonFactory;
@Inject
public PlayersTableJSONHandler(
PlanConfig config,
DBSystem dbSystem,
Formatters formatters
JSONFactory jsonFactory
) {
this.config = config;
this.jsonFactory = jsonFactory;
this.dbSystem = dbSystem;
this.formatters = formatters;
}
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = getServerUUID(target); // Can throw BadRequestException
return new JSONResponse(parseJSON(serverUUID));
}
public String parseJSON(UUID serverUUID) {
Integer xMostRecentPlayers = config.get(DisplaySettings.PLAYERS_PER_SERVER_PAGE);
Integer loginThreshold = config.get(TimeSettings.ACTIVE_LOGIN_THRESHOLD);
Long playtimeThreshold = config.get(TimeSettings.ACTIVE_PLAY_THRESHOLD);
Boolean openPlayerLinksInNewTab = config.get(DisplaySettings.OPEN_PLAYER_LINKS_IN_NEW_TAB);
Database database = dbSystem.getDatabase();
return new PlayersTableJSONParser(
database.query(new ServerPlayersTableContainersQuery(serverUUID)),
database.query(new ExtensionServerPlayerDataTableQuery(serverUUID, xMostRecentPlayers)),
xMostRecentPlayers, playtimeThreshold, loginThreshold, openPlayerLinksInNewTab,
formatters
).toJSONString();
return new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID));
}
private UUID getServerUUID(RequestTarget target) throws BadRequestException {