From 648cdb1a0c580ccf0a14f26831c245cac38c5dbc Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sun, 15 Mar 2020 16:43:13 +0200 Subject: [PATCH] Added a ResourceService - Allows customizable files - Allows js and css addition to html files --- .../plan/delivery/web/ResourceService.java | 84 +++++++++++++++++++ .../plan/delivery/web/resource/Resource.java | 82 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 Plan/api/src/main/java/com/djrapitops/plan/delivery/web/ResourceService.java create mode 100644 Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resource/Resource.java diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/ResourceService.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/ResourceService.java new file mode 100644 index 000000000..56ddd9aca --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/ResourceService.java @@ -0,0 +1,84 @@ +/* + * 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 . + */ +package com.djrapitops.plan.delivery.web; + +import com.djrapitops.plan.delivery.web.resource.Resource; + +import java.util.function.Supplier; + +/** + * Service for making plugin resources customizable by user or Plan API. + * + * @author Rsl1122 + */ +public interface ResourceService { + + /** + * Make one of your web resources customizable by user or Plan API. + * + * @param pluginName Name of your plugin (for config purposes) + * @param fileName Name of the file (for customization) + * @param source Supplier to use to get the original resource. + * @return Resource of the customized file. + */ + Resource getResource(String pluginName, String fileName, Supplier source); + + /** + * Add javascript to load in an existing html resource. + *

+ * Adds {@code } or multiple to the resource. + * + * @param pluginName Name of your plugin (for config purposes) + * @param fileName Name of the .html file being modified + * @param position Where to place the script tag on the page. + * @param jsSrcs Source URLs. + * @throws IllegalArgumentException If fileName does not end with .html or .htm + */ + void addScriptsToResource(String pluginName, String fileName, Position position, String... jsSrcs); + + /** + * Add css to load in an existing html resource. + *

+ * Adds {@code } or multiple to the resource. + * + * @param pluginName Name of your plugin (for config purposes) + * @param fileName Name of the .html file being modified + * @param position Where to place the link tag on the page. + * @param cssSrcs Source URLs. + * @throws IllegalArgumentException If fileName does not end with .html or .htm + */ + void addStylesToResource(String pluginName, String fileName, Position position, String... cssSrcs); + + enum Position { + /** + * Loaded before page contents. + */ + HEAD, + /** + * Loaded after page contents. + *

+ * Recommended for modifying the structure of the page or loading libraries. + */ + BODY, + /** + * Loaded after script execution. + *

+ * Recommended for loading data to custom structure on the page. + */ + BODY_END + } +} diff --git a/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resource/Resource.java b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resource/Resource.java new file mode 100644 index 000000000..c58576bc9 --- /dev/null +++ b/Plan/api/src/main/java/com/djrapitops/plan/delivery/web/resource/Resource.java @@ -0,0 +1,82 @@ +/* + * 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 . + */ +package com.djrapitops.plan.delivery.web.resource; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +/** + * Represents a customizable resource. + *

+ * You can use the create methods for simple resources when using {@link com.djrapitops.plan.delivery.web.ResourceService}. + */ +public interface Resource { + + static Resource create(byte[] content) { + return new ByteResource(content); + } + + static Resource create(String utf8String) { + return new ByteResource(utf8String.getBytes(StandardCharsets.UTF_8)); + } + + static Resource create(InputStream in) throws IOException { + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { + int read; + byte[] bytes = new byte[1024]; + while ((read = in.read(bytes)) != -1) { + out.write(bytes, 0, read); + } + + return new ByteResource(out.toByteArray()); + } finally { + in.close(); + } + } + + byte[] asBytes(); + + String asString(); + + InputStream asStream(); + + final class ByteResource implements Resource { + private final byte[] content; + + public ByteResource(byte[] content) { + this.content = content; + } + + @Override + public byte[] asBytes() { + return content; + } + + @Override + public String asString() { + return new String(content, StandardCharsets.UTF_8); + } + + @Override + public InputStream asStream() { + return new ByteArrayInputStream(content); + } + } +} \ No newline at end of file