Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/de/igslandstuhl/database/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public Topic[] readFile(String file) throws SerializationException, SQLException

public static void main(String[] args) throws Exception {
instance = new Application(args);


PluginLoader.getInstance().preloadPlugins();

if (!getInstance().suppressCmd()) {
Command.registerCommands();
CommandLineUtils.setup();
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/de/igslandstuhl/database/plugins/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.yaml.snakeyaml.Yaml;

import de.igslandstuhl.database.Registry;
import de.igslandstuhl.database.plugins.config.BoolSetting;

public class PluginLoader {
private final List<PreLoadedPlugin> pluginInfos = new ArrayList<>();
Expand All @@ -42,11 +41,16 @@ private Map<String, Object> loadYaml(URLClassLoader classLoader) {
}
public PreLoadedPlugin loadPluginFromJar(File jarFile) {
URLClassLoader classLoader;
URLClassLoader resourceLoader;
try {
classLoader = new URLClassLoader(
new URL[]{jarFile.toURI().toURL()},
getClass().getClassLoader()
);
resourceLoader = new URLClassLoader(
new URL[]{jarFile.toURI().toURL()},
null
);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
Expand Down Expand Up @@ -85,7 +89,7 @@ public PreLoadedPlugin loadPluginFromJar(File jarFile) {
throw new IllegalStateException("Main class does not extend Plugin");
}

return new PreLoadedPlugin(new PluginDescription(id, name, description, mainClassName, depends), clazz, classLoader);
return new PreLoadedPlugin(new PluginDescription(id, name, description, mainClassName, depends), clazz, classLoader, resourceLoader);

} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -115,7 +119,7 @@ public void load(PreLoadedPlugin preload) {
e.printStackTrace();
}
}
public void loadAllPlugins(File folder) {
public void preloadAllPlugins(File folder) {
File[] jars = folder.listFiles((dir, name) -> name.endsWith(".jar"));
if (jars == null) return;

Expand All @@ -137,6 +141,8 @@ public void loadAllPlugins(File folder) {
}

PluginSort.sortPlugins(plugins).forEach((p) -> pluginInfos.add(p));
}
public void loadAllPlugins(File folder) {
pluginInfos.forEach(this::load);
}
public void enablePlugins() {
Expand All @@ -159,6 +165,7 @@ public void unloadPlugins() {

try {
p.classLoader().close();
p.resourceLoader().close();
} catch (IOException e) {
throw new RuntimeException("Problem while unloading", e);
}
Expand All @@ -174,12 +181,10 @@ private void registerPlugin(Plugin plugin) {
}
Registry.pluginRegistry().register(plugin.getId(), plugin);
}
public void preloadPlugins() {
preloadAllPlugins(new File("plugins"));
}
public void registerPlugins() {
registerPlugin(new Plugin.DummyModule("result_view", "Student Results View", "The view displaying the student's current progress and prognoses for the final result", List.of(
new BoolSetting("show_prognosis", "Show Prognosis", "Whether to display the prognosis for the final result", true),
new BoolSetting("show_current_progress", "Show Current", "Whether to display the current progress to the subject (in percent)", true),
new BoolSetting("show_current_grade", "Show Currently Achieved Grade", "Whether to display the grade the student would achieve when they decide to immediately stop working", false)
)));
loadAllPlugins(new File("plugins"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public InputStream open(ResourceLocation location) {
String path = location.context() + "/" + location.namespace() + "/" + location.resource();

for (PreLoadedPlugin module : PluginLoader.getInstance().getPluginInfos()) {
ClassLoader cl = module.classLoader();
ClassLoader cl = module.resourceLoader();
InputStream stream = cl.getResourceAsStream(path);

if (stream != null) {
Expand All @@ -40,7 +40,7 @@ public List<InputStream> openAll(ResourceLocation location) {
List<InputStream> inputStreams = new ArrayList<>();

for (PreLoadedPlugin module : PluginLoader.getInstance().getPluginInfos()) {
ClassLoader cl = module.classLoader();
ClassLoader cl = module.resourceLoader();
InputStream stream = cl.getResourceAsStream(path);

if (stream != null) {
Expand All @@ -58,7 +58,7 @@ public Collection<ResourceLocation> list(Pattern pattern) {
final Path virtualRoot = Paths.get("").toAbsolutePath().normalize();

for (PreLoadedPlugin module : PluginLoader.getInstance().getPluginInfos()) {
try (ZipFile zip = new ZipFile(new File(module.classLoader().getURLs()[0].toURI()))) {
try (ZipFile zip = new ZipFile(new File(module.resourceLoader().getURLs()[0].toURI()))) {

Enumeration<? extends ZipEntry> entries = zip.entries();

Expand All @@ -69,7 +69,10 @@ public Collection<ResourceLocation> list(Pattern pattern) {

String name = entry.getName();

if (!CoreResourceProvider.isSafeZipEntryName(name, virtualRoot))
if (!CoreResourceProvider.isSafeZipEntryName(name, virtualRoot)) {
continue;
}

if (pattern.matcher(name).matches()) {
ResourceLocation loc = ResourceLocation.fromPath(name);
if (loc != null) result.add(loc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
record PreLoadedPlugin (
PluginDescription description,
Class<?> clazz,
URLClassLoader classLoader
URLClassLoader classLoader,
URLClassLoader resourceLoader
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public static GetResponse handleTemplatingFileRequest(GetRequest request) {
String user = getUser(request).getUsername();
return GetResponse.getResource(request, request.toResourceLocation(user), user, true);
}
public static GetResponse handleMergingFileRequest(GetRequest request) {
String user = getUser(request).getUsername();
return GetResponse.getResource(request, request.toResourceLocation(user), user, false, true);
}
public static GetResponse handleSQLRequest(GetRequest request) {
String user = getUser(request).getUsername();
return GetResponse.getResource(request, request.toResourceLocation(user), user, false);
Expand All @@ -68,6 +72,7 @@ public final void registerHandlers() {
ThrowingFunction<GetRequest, HttpResponse> handlerFunction = switch (webPath.handlerType()) {
case "FileRequestHandler" -> GetRequestHandler::handleFileRequest;
case "TemplatingFileRequestHandler" -> GetRequestHandler::handleTemplatingFileRequest;
case "MergingFileRequestHandler" -> GetRequestHandler::handleMergingFileRequest;
case "SQLRequestHandler" -> GetRequestHandler::handleSQLRequest;
case "PluginRequestHandler" -> GetRequestHandler::handlePluginRequest;
default -> throw new IllegalArgumentException("Unknown handler type: " + webPath.handlerType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ public class GetResponse implements HttpResponse {
* @return the GetResponse object
*/
public static GetResponse notFound(HttpRequest request) {
return new GetResponse(request, Status.NOT_FOUND, new ResourceLocation("html", "errors", "404.html"), ContentType.HTML, "", false);
return new GetResponse(request, Status.NOT_FOUND, new ResourceLocation("html", "errors", "404.html"), ContentType.HTML, "", false, false);
}
/**
* Returns a response for a GET request that resulted in an internal error.
* @return the GetResponse object
*/
public static GetResponse internalServerError(HttpRequest request) {
return new GetResponse(request, Status.INTERNAL_SERVER_ERROR, new ResourceLocation("html", "errors", "500.html"), ContentType.HTML, "", false);
return new GetResponse(request, Status.INTERNAL_SERVER_ERROR, new ResourceLocation("html", "errors", "500.html"), ContentType.HTML, "", false, false);
}
/**
* Returns a response for a GET request the user has no access to.
* @return the HttpRequest object
*/
public static GetResponse forbidden(HttpRequest request) {
return new GetResponse(request, Status.FORBIDDEN, new ResourceLocation("html", "errors", "403.html"), ContentType.HTML, "", false);
return new GetResponse(request, Status.FORBIDDEN, new ResourceLocation("html", "errors", "403.html"), ContentType.HTML, "", false, false);
}
/**
* Returns a response for a GET request the user must be logged in for.
* @return the GetResponse object
*/
public static GetResponse unauthorized(HttpRequest request) {
return new GetResponse(request, Status.UNAUTHORIZED, new ResourceLocation("html", "errors", "401.html"), ContentType.HTML, "", false);
return new GetResponse(request, Status.UNAUTHORIZED, new ResourceLocation("html", "errors", "401.html"), ContentType.HTML, "", false, false);
}
/**
* Returns a response for a GET request the user must be logged in for.
Expand All @@ -59,6 +59,7 @@ public static GetResponse unauthorized(String message, HttpRequest request) {
new ResourceLocation("html", "errors", "401.html"),
ContentType.HTML,
message,
false,
false
);
}
Expand All @@ -75,6 +76,7 @@ public static GetResponse tooManyRequests(String message, HttpRequest request) {
new ResourceLocation("html", "errors", "429.html"),
ContentType.HTML,
message,
false,
false
);
}
Expand Down Expand Up @@ -115,6 +117,7 @@ public static GetResponse tooManyRequests(HttpRequest request) {
private final HttpRequest request;

private final boolean isTemplating;
private final boolean isMerging;

/**
* Creates a new GetResponse with the given parameters.
Expand All @@ -125,13 +128,14 @@ public static GetResponse tooManyRequests(HttpRequest request) {
* @param user the user who made the request
* @param isTemplating whether the resource is a templated resource
*/
public GetResponse(HttpRequest request, Status status, ResourceLocation resourceLocation, ContentType contentType, String user, boolean isTemplating) {
public GetResponse(HttpRequest request, Status status, ResourceLocation resourceLocation, ContentType contentType, String user, boolean isTemplating, boolean isMerging) {
this.status = status;
this.resourceLocation = resourceLocation;
this.contentType = contentType;
this.user = user;
this.request = request;
this.isTemplating = isTemplating;
this.isMerging = isMerging;
}
/**
* Returns a response for a GET request for the given resource.
Expand All @@ -143,9 +147,15 @@ public static GetResponse getResource(HttpRequest request, ResourceLocation reso
return getResource(request, resourceLocation, user, isTemplating, request.getPath());
}
public static GetResponse getResource(HttpRequest request, ResourceLocation resourceLocation, String user, boolean isTemplating, String path) {
return getResource(request, resourceLocation, user, isTemplating, false, path);
}
public static GetResponse getResource(HttpRequest request, ResourceLocation resourceLocation, String user, boolean isTemplating, boolean isMerging) {
return getResource(request, resourceLocation, user, isTemplating, isMerging, request.getPath());
}
public static GetResponse getResource(HttpRequest request, ResourceLocation resourceLocation, String user, boolean isTemplating, boolean isMerging, String path) {
try {
if (AccessManager.getInstance().hasAccess(user, path)) {
return new GetResponse(request, Status.OK, resourceLocation, ContentType.ofResourceLocation(resourceLocation), user, isTemplating);
return new GetResponse(request, Status.OK, resourceLocation, ContentType.ofResourceLocation(resourceLocation), user, isTemplating, isMerging);
} else {
return unauthorized(request);
}
Expand Down Expand Up @@ -175,7 +185,11 @@ public void respond(PrintStream out) {
String resource = "";
if (resourceLocation != null) {
if (!resourceLocation.isVirtual()) {
resource = Server.getInstance().getResourceManager().readResourceCompletely(resourceLocation);
if (!isMerging) {
resource = Server.getInstance().getResourceManager().readResourceCompletely(resourceLocation);
} else {
resource = Server.getInstance().getResourceManager().readCodeMerged(resourceLocation);
}
} else {
if (resourceLocation.namespace().equals("plugin")) {
resource = PluginRequestHandler.getPluginResource(resourceLocation.resource());
Expand Down
35 changes: 0 additions & 35 deletions src/main/resources/css/site/results.css

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/html/admin/student-results.html

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/resources/html/teacher/student-results.html

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/resources/html/user/results.html

This file was deleted.

Loading
Loading