[Debt] Removed FileUtil.getStringFromResource

Because of PlanPlugin dependency this code needed to be moved elsewhere.
New method to use is FileSystem#readCustomizableResourceFlat
This commit is contained in:
Rsl1122 2018-09-24 19:38:39 +03:00
parent e7cbf10ee3
commit 594e9b4072
9 changed files with 79 additions and 35 deletions

View File

@ -39,10 +39,6 @@ public class FileSystem implements SubSystem {
this.configFile = getFileFromPluginFolder("config.yml"); this.configFile = getFileFromPluginFolder("config.yml");
} }
public List<String> readFromResource(String fileName) throws IOException {
return FileUtil.lines(plugin, fileName);
}
public File getDataFolder() { public File getDataFolder() {
return dataFolder; return dataFolder;
} }
@ -88,8 +84,42 @@ public class FileSystem implements SubSystem {
// No disable actions necessary. // No disable actions necessary.
} }
/**
* Read a file from jar as lines.
*
* @param fileName Name of the file.
* @return lines of the file
* @throws IOException If the resource can not be read.
*/
public List<String> readFromResource(String fileName) throws IOException {
return FileUtil.lines(plugin, fileName);
}
/**
* Read a file from jar as a flat String.
*
* @param fileName Name of the file
* @return Flattened lines with {@code \r\n} line separators.
* @throws IOException If the resource can not be read.
*/
public String readFromResourceFlat(String fileName) throws IOException { public String readFromResourceFlat(String fileName) throws IOException {
List<String> lines = readFromResource(fileName); return flatten(readFromResource(fileName));
}
/**
* Read a file from jar or /plugins/Plan/ folder.
*
* @param fileName Name of the file
* @return Flattened lines with {@code \r\n} line separators.
* @throws IOException If the resource can not be read.
*/
public String readCustomizableResourceFlat(String fileName) throws IOException {
return flatten(FileUtil.lines(
plugin, new File(plugin.getDataFolder(), fileName.replace("/", File.separator)), fileName
));
}
private String flatten(List<String> lines) {
StringBuilder flat = new StringBuilder(); StringBuilder flat = new StringBuilder();
for (String line : lines) { for (String line : lines) {
flat.append(line).append("\r\n"); flat.append(line).append("\r\n");

View File

@ -4,8 +4,8 @@
*/ */
package com.djrapitops.plan.system.webserver.response; package com.djrapitops.plan.system.webserver.response;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse; import com.djrapitops.plan.system.webserver.response.errors.NotFoundResponse;
import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import java.io.IOException; import java.io.IOException;
@ -20,10 +20,13 @@ import java.io.IOException;
*/ */
public class FileResponse extends Response { public class FileResponse extends Response {
// TODO
private FileSystem fileSystem;
public FileResponse(String fileName) { public FileResponse(String fileName) {
super.setHeader("HTTP/1.1 200 OK"); super.setHeader("HTTP/1.1 200 OK");
try { try {
super.setContent(FileUtil.getStringFromResource(fileName)); super.setContent(fileSystem.readCustomizableResourceFlat(fileName));
} catch (IOException e) { } catch (IOException e) {
super.setContent(new NotFoundResponse(fileName + " was not found inside the .jar or /plugins/Plan/ folder").getContent()); super.setContent(new NotFoundResponse(fileName + " was not found inside the .jar or /plugins/Plan/ folder").getContent());
} }

View File

@ -4,8 +4,8 @@
*/ */
package com.djrapitops.plan.system.webserver.response.errors; package com.djrapitops.plan.system.webserver.response.errors;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.logging.error.ErrorHandler;
import org.apache.commons.text.StringSubstitutor; import org.apache.commons.text.StringSubstitutor;
@ -26,11 +26,12 @@ public class ErrorResponse extends Response {
// TODO // TODO
private String version; private String version;
private FileSystem fileSystem;
private ErrorHandler errorHandler; private ErrorHandler errorHandler;
public ErrorResponse() { public ErrorResponse() {
try { try {
setContent(FileUtil.getStringFromResource("web/error.html")); setContent(fileSystem.readCustomizableResourceFlat("web/error.html"));
} catch (IOException e) { } catch (IOException e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);
} }

View File

@ -3,7 +3,6 @@ package com.djrapitops.plan.utilities.file;
import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.utilities.Verify;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -22,16 +21,6 @@ public class FileUtil {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
@Deprecated
public static String getStringFromResource(String fileName) throws IOException {
StringBuilder html = new StringBuilder();
PlanPlugin plugin = PlanPlugin.getInstance();
lines(PlanPlugin.getInstance(), new File(plugin.getDataFolder(), fileName.replace("/", File.separator)), fileName)
.forEach(line -> html.append(line).append("\r\n"));
return html.toString();
}
public static List<String> lines(PlanPlugin plugin, File savedFile, String defaults) throws IOException { public static List<String> lines(PlanPlugin plugin, File savedFile, String defaults) throws IOException {
if (savedFile.exists()) { if (savedFile.exists()) {
return lines(savedFile); return lines(savedFile);
@ -97,7 +86,7 @@ public class FileUtil {
public static List<String> lines(File file, Charset charset) throws IOException { public static List<String> lines(File file, Charset charset) throws IOException {
List<String> lines = new ArrayList<>(); List<String> lines = new ArrayList<>();
if (Verify.exists(file)) { if (file != null && file.exists()) {
try (Stream<String> linesStream = Files.lines(file.toPath(), charset)) { try (Stream<String> linesStream = Files.lines(file.toPath(), charset)) {
lines = linesStream.collect(Collectors.toList()); lines = linesStream.collect(Collectors.toList());
} }

View File

@ -6,8 +6,8 @@ package com.djrapitops.plan.utilities.html.pages;
import com.djrapitops.plan.api.exceptions.ParseException; import com.djrapitops.plan.api.exceptions.ParseException;
import com.djrapitops.plan.data.store.containers.AnalysisContainer; import com.djrapitops.plan.data.store.containers.AnalysisContainer;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plan.utilities.formatting.Formatter; import com.djrapitops.plan.utilities.formatting.Formatter;
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer; import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
import com.djrapitops.plugin.api.Benchmark; import com.djrapitops.plugin.api.Benchmark;
@ -28,10 +28,16 @@ public class AnalysisPage implements Page {
private final AnalysisContainer analysisContainer; private final AnalysisContainer analysisContainer;
private final FileSystem fileSystem;
private final Formatter<Double> decimalFormatter; private final Formatter<Double> decimalFormatter;
AnalysisPage(AnalysisContainer analysisContainer, Formatter<Double> decimalFormatter) { AnalysisPage(
AnalysisContainer analysisContainer,
FileSystem fileSystem,
Formatter<Double> decimalFormatter
) {
this.analysisContainer = analysisContainer; this.analysisContainer = analysisContainer;
this.fileSystem = fileSystem;
this.decimalFormatter = decimalFormatter; this.decimalFormatter = decimalFormatter;
} }
@ -67,7 +73,7 @@ public class AnalysisPage implements Page {
performanceNumbers(placeholderReplacer); performanceNumbers(placeholderReplacer);
try { try {
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/server.html")); return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/server.html"));
} catch (IOException e) { } catch (IOException e) {
throw new ParseException(e); throw new ParseException(e);
} finally { } finally {

View File

@ -12,13 +12,13 @@ import com.djrapitops.plan.data.store.keys.PlayerKeys;
import com.djrapitops.plan.data.store.mutators.*; import com.djrapitops.plan.data.store.mutators.*;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.theme.Theme; import com.djrapitops.plan.system.settings.theme.Theme;
import com.djrapitops.plan.system.settings.theme.ThemeVal; import com.djrapitops.plan.system.settings.theme.ThemeVal;
import com.djrapitops.plan.utilities.comparators.SessionStartComparator; import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plan.utilities.formatting.Formatter; import com.djrapitops.plan.utilities.formatting.Formatter;
import com.djrapitops.plan.utilities.formatting.Formatters; import com.djrapitops.plan.utilities.formatting.Formatters;
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer; import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
@ -48,6 +48,7 @@ public class InspectPage implements Page {
private final String version; private final String version;
private final FileSystem fileSystem;
private final PlanConfig config; private final PlanConfig config;
private final Theme theme; private final Theme theme;
private final Graphs graphs; private final Graphs graphs;
@ -64,6 +65,7 @@ public class InspectPage implements Page {
InspectPage( InspectPage(
PlayerContainer player, Map<UUID, String> serverNames, PlayerContainer player, Map<UUID, String> serverNames,
String version, String version,
FileSystem fileSystem,
PlanConfig config, PlanConfig config,
Theme theme, Theme theme,
Graphs graphs, Graphs graphs,
@ -76,6 +78,7 @@ public class InspectPage implements Page {
this.player = player; this.player = player;
this.serverNames = serverNames; this.serverNames = serverNames;
this.version = version; this.version = version;
this.fileSystem = fileSystem;
this.config = config; this.config = config;
this.theme = theme; this.theme = theme;
this.graphs = graphs; this.graphs = graphs;
@ -228,7 +231,7 @@ public class InspectPage implements Page {
: serverName : serverName
); );
return replacer.apply(FileUtil.getStringFromResource("web/player.html")); return replacer.apply(fileSystem.readCustomizableResourceFlat("web/player.html"));
} }
private void sessionsAndPlaytime(PlaceholderReplacer replacer, SessionsMutator sessionsMutator, SessionsMutator daySessionsMutator, SessionsMutator weekSessionsMutator, SessionsMutator monthSessionsMutator) { private void sessionsAndPlaytime(PlaceholderReplacer replacer, SessionsMutator sessionsMutator, SessionsMutator daySessionsMutator, SessionsMutator weekSessionsMutator, SessionsMutator monthSessionsMutator) {

View File

@ -7,12 +7,12 @@ package com.djrapitops.plan.utilities.html.pages;
import com.djrapitops.plan.api.exceptions.ParseException; import com.djrapitops.plan.api.exceptions.ParseException;
import com.djrapitops.plan.data.store.containers.NetworkContainer; import com.djrapitops.plan.data.store.containers.NetworkContainer;
import com.djrapitops.plan.data.store.keys.NetworkKeys; import com.djrapitops.plan.data.store.keys.NetworkKeys;
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer; import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties; import com.djrapitops.plan.system.info.server.properties.ServerProperties;
import com.djrapitops.plan.system.webserver.cache.PageId; import com.djrapitops.plan.system.webserver.cache.PageId;
import com.djrapitops.plan.system.webserver.cache.ResponseCache; import com.djrapitops.plan.system.webserver.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.response.pages.parts.NetworkPageContent; import com.djrapitops.plan.system.webserver.response.pages.parts.NetworkPageContent;
import com.djrapitops.plan.utilities.file.FileUtil; import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
import static com.djrapitops.plan.data.store.keys.NetworkKeys.*; import static com.djrapitops.plan.data.store.keys.NetworkKeys.*;
@ -25,13 +25,16 @@ public class NetworkPage implements Page {
private final NetworkContainer networkContainer; private final NetworkContainer networkContainer;
private final FileSystem fileSystem;
private final ServerProperties serverProperties; private final ServerProperties serverProperties;
public NetworkPage( public NetworkPage(
NetworkContainer networkContainer, NetworkContainer networkContainer,
FileSystem fileSystem,
ServerProperties serverProperties ServerProperties serverProperties
) { ) {
this.networkContainer = networkContainer; this.networkContainer = networkContainer;
this.fileSystem = fileSystem;
this.serverProperties = serverProperties; this.serverProperties = serverProperties;
} }
@ -57,7 +60,7 @@ public class NetworkPage implements Page {
ResponseCache.loadResponse(PageId.NETWORK_CONTENT.id(), NetworkPageContent::new); ResponseCache.loadResponse(PageId.NETWORK_CONTENT.id(), NetworkPageContent::new);
placeholderReplacer.put("tabContentServers", networkPageContent.getContents()); placeholderReplacer.put("tabContentServers", networkPageContent.getContents());
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/network.html")); return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/network.html"));
} catch (Exception e) { } catch (Exception e) {
throw new ParseException(e); throw new ParseException(e);
} }

View File

@ -5,6 +5,7 @@ import com.djrapitops.plan.data.store.containers.AnalysisContainer;
import com.djrapitops.plan.data.store.containers.NetworkContainer; import com.djrapitops.plan.data.store.containers.NetworkContainer;
import com.djrapitops.plan.data.store.containers.PlayerContainer; import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.info.connection.ConnectionSystem; import com.djrapitops.plan.system.info.connection.ConnectionSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
@ -34,6 +35,7 @@ import java.util.UUID;
public class PageFactory { public class PageFactory {
private final String version; private final String version;
private final Lazy<FileSystem> fileSystem;
private final Lazy<PlanConfig> config; private final Lazy<PlanConfig> config;
private final Lazy<Theme> theme; private final Lazy<Theme> theme;
private final Lazy<Database> database; private final Lazy<Database> database;
@ -51,6 +53,7 @@ public class PageFactory {
@Inject @Inject
public PageFactory( public PageFactory(
@Named("currentVersion") String version, @Named("currentVersion") String version,
Lazy<FileSystem> fileSystem,
Lazy<PlanConfig> config, Lazy<PlanConfig> config,
Lazy<Theme> theme, Lazy<Theme> theme,
Lazy<Database> database, Lazy<Database> database,
@ -66,6 +69,7 @@ public class PageFactory {
Lazy<ErrorHandler> errorHandler Lazy<ErrorHandler> errorHandler
) { ) {
this.version = version; this.version = version;
this.fileSystem = fileSystem;
this.config = config; this.config = config;
this.theme = theme; this.theme = theme;
this.database = database; this.database = database;
@ -90,12 +94,14 @@ public class PageFactory {
} }
public PlayersPage playersPage() { public PlayersPage playersPage() {
return new PlayersPage(version, config.get(), database.get(), serverInfo.get(), tables.get(), timings.get()); return new PlayersPage(version, fileSystem.get(), config.get(),
database.get(), serverInfo.get(), tables.get(),
timings.get());
} }
public AnalysisPage analysisPage(UUID serverUUID) { public AnalysisPage analysisPage(UUID serverUUID) {
AnalysisContainer analysisContainer = new AnalysisContainer(database.get().fetch().getServerContainer(serverUUID)); AnalysisContainer analysisContainer = new AnalysisContainer(database.get().fetch().getServerContainer(serverUUID));
return new AnalysisPage(analysisContainer, formatters.get().decimals()); return new AnalysisPage(analysisContainer, fileSystem.get(), formatters.get().decimals());
} }
public InspectPage inspectPage(UUID uuid) { public InspectPage inspectPage(UUID uuid) {
@ -104,7 +110,7 @@ public class PageFactory {
return new InspectPage( return new InspectPage(
player, serverNames, player, serverNames,
version, version,
config.get(), theme.get(), fileSystem.get(), config.get(), theme.get(),
graphs.get(), tables.get(), accordions.get(), formatters.get(), graphs.get(), tables.get(), accordions.get(), formatters.get(),
serverInfo.get(), timings.get() serverInfo.get(), timings.get()
); );
@ -116,6 +122,6 @@ public class PageFactory {
public NetworkPage networkPage() { public NetworkPage networkPage() {
NetworkContainer networkContainer = database.get().fetch().getNetworkContainer(); // Not cached, big. NetworkContainer networkContainer = database.get().fetch().getNetworkContainer(); // Not cached, big.
return new NetworkPage(networkContainer, serverInfo.get().getServerProperties()); return new NetworkPage(networkContainer, fileSystem.get(), serverInfo.get().getServerProperties());
} }
} }

View File

@ -3,10 +3,10 @@ package com.djrapitops.plan.utilities.html.pages;
import com.djrapitops.plan.api.exceptions.ParseException; import com.djrapitops.plan.api.exceptions.ParseException;
import com.djrapitops.plan.data.store.containers.PlayerContainer; import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.file.FileSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.utilities.file.FileUtil;
import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer; import com.djrapitops.plan.utilities.formatting.PlaceholderReplacer;
import com.djrapitops.plan.utilities.html.tables.HtmlTables; import com.djrapitops.plan.utilities.html.tables.HtmlTables;
import com.djrapitops.plugin.api.Check; import com.djrapitops.plugin.api.Check;
@ -22,6 +22,7 @@ import java.util.List;
public class PlayersPage implements Page { public class PlayersPage implements Page {
private final String version; private final String version;
private final FileSystem fileSystem;
private final PlanConfig config; private final PlanConfig config;
private final Database database; private final Database database;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
@ -32,6 +33,7 @@ public class PlayersPage implements Page {
PlayersPage( PlayersPage(
String version, String version,
FileSystem fileSystem,
PlanConfig config, PlanConfig config,
Database database, Database database,
ServerInfo serverInfo, ServerInfo serverInfo,
@ -39,6 +41,7 @@ public class PlayersPage implements Page {
Timings timings Timings timings
) { ) {
this.version = version; this.version = version;
this.fileSystem = fileSystem;
this.config = config; this.config = config;
this.database = database; this.database = database;
this.serverInfo = serverInfo; this.serverInfo = serverInfo;
@ -63,7 +66,7 @@ public class PlayersPage implements Page {
placeholderReplacer.put("playersTable", tables.playerTableForPlayersPage(playerContainers).parseHtml()); placeholderReplacer.put("playersTable", tables.playerTableForPlayersPage(playerContainers).parseHtml());
timings.end("Pages", "Players page players table parsing"); timings.end("Pages", "Players page players table parsing");
return placeholderReplacer.apply(FileUtil.getStringFromResource("web/players.html")); return placeholderReplacer.apply(fileSystem.readCustomizableResourceFlat("web/players.html"));
} catch (Exception e) { } catch (Exception e) {
throw new ParseException(e); throw new ParseException(e);
} }