From be1266495086b9057b2b155a10c9bf1b4b185a10 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Thu, 20 Apr 2023 11:41:31 +0200 Subject: [PATCH] Enables passing files to a test Server --- build.gradle | 6 ++ .../java/org/jabref/http/server/Server.java | 62 ++++++++++++++++++- .../java/org/jabref/logic/git/GitHandler.java | 2 +- .../jabref/http/server/http-server-demo.bib | 14 +++++ src/main/resources/tinylog.properties | 2 + .../org/jabref/http/server/TestServer.java | 14 ----- src/test/resources/testbib/jabref-authors.bib | 13 ++++ 7 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/main/resources/org/jabref/http/server/http-server-demo.bib delete mode 100644 src/test/java/org/jabref/http/server/TestServer.java diff --git a/build.gradle b/build.gradle index ac0799e23ee..5c012f27903 100644 --- a/build.gradle +++ b/build.gradle @@ -401,6 +401,12 @@ run { 'javafx.base/com.sun.javafx.event' : 'com.jfoenix' ] } + + if (project.hasProperty('application')){ + if (application == 'httpserver'){ + main = 'org.jabref.http.server.Server' + } + } } javadoc { diff --git a/src/main/java/org/jabref/http/server/Server.java b/src/main/java/org/jabref/http/server/Server.java index 88d57869da4..dc8411e8aa9 100644 --- a/src/main/java/org/jabref/http/server/Server.java +++ b/src/main/java/org/jabref/http/server/Server.java @@ -1,26 +1,81 @@ package org.jabref.http.server; import java.net.URI; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.concurrent.CountDownLatch; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import javax.net.ssl.SSLContext; +import javafx.collections.ObservableList; + import org.jabref.logic.util.OS; +import org.jabref.preferences.JabRefPreferences; import jakarta.ws.rs.SeBootstrap; import net.harawata.appdirs.AppDirsFactory; import org.glassfish.grizzly.ssl.SSLContextConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.bridge.SLF4JBridgeHandler; public class Server { private static final Logger LOGGER = LoggerFactory.getLogger(Server.class); private static SeBootstrap.Instance serverInstance; - static void startServer(CountDownLatch latch) { + /** + * Starts an http server serving the last files opened in JabRef
+ * More files can be provided as args. + */ + public static void main(final String[] args) throws InterruptedException, URISyntaxException { + SLF4JBridgeHandler.removeHandlersForRootLogger(); + SLF4JBridgeHandler.install(); + + final ObservableList lastFilesOpened = JabRefPreferences.getInstance().getGuiPreferences().getLastFilesOpened(); + + // The server serves the last opened files (see org.jabref.http.server.LibraryResource.getLibraryPath) + // In a testing environment, this might be difficult to handle + // This is a quick solution. The architectural fine solution would use some http context or other @Inject_ed variables in org.jabref.http.server.LibraryResource + if (args.length > 0) { + LOGGER.debug("Command line parameters passed"); + List filesToAdd = Arrays.stream(args) + .map(Path::of) + .filter(Files::exists) + .map(Path::toString) + .filter(path -> !lastFilesOpened.contains(path)) + .toList(); + + LOGGER.debug("Adding following files to the list of opened libraries: {}", filesToAdd); + + // add the files in the front of the last opened libraries + Collections.reverse(filesToAdd); + for (String path : filesToAdd) { + lastFilesOpened.add(0, path); + } + } + + if (lastFilesOpened.isEmpty()) { + LOGGER.debug("still no library available to serve, serve the demo library"); + // Server.class.getResource("...") is always null here, thus trying relative path + // Path bibPath = Path.of(Server.class.getResource("http-server-demo.bib").toURI()); + Path bibPath = Path.of("src/main/resources/org/jabref/http/server/http-server-demo.bib").toAbsolutePath(); + LOGGER.debug("Location of demo library: {}", bibPath); + lastFilesOpened.add(bibPath.toString()); + } + + LOGGER.debug("Libraries served: {}", lastFilesOpened); + + Server.startServer(); + + // Keep the http server running until user kills the process (e.g., presses Ctrl+C) + Thread.currentThread().join(); + } + + private static void startServer() { SSLContext sslContext = getSslContext(); SeBootstrap.Configuration configuration = SeBootstrap.Configuration .builder() @@ -28,7 +83,9 @@ static void startServer(CountDownLatch latch) { .protocol("HTTPS") .port(6051) .build(); + LOGGER.debug("Starting server..."); SeBootstrap.start(Application.class, configuration).thenAccept(instance -> { + LOGGER.debug("Server started."); instance.stopOnShutdown(stopResult -> System.out.printf("Stop result: %s [Native stop result: %s].%n", stopResult, stopResult.unwrap(Object.class))); @@ -37,7 +94,6 @@ static void startServer(CountDownLatch latch) { instance.unwrap(Object.class)); System.out.println("Send SIGKILL to shutdown."); serverInstance = instance; - latch.countDown(); }); } diff --git a/src/main/java/org/jabref/logic/git/GitHandler.java b/src/main/java/org/jabref/logic/git/GitHandler.java index 498e5c5f3df..e865e9b9299 100644 --- a/src/main/java/org/jabref/logic/git/GitHandler.java +++ b/src/main/java/org/jabref/logic/git/GitHandler.java @@ -71,7 +71,7 @@ void setupGitIgnore() { FileUtil.copyFile(Path.of(this.getClass().getResource("git.gitignore").toURI()), gitignore, false); } } catch (URISyntaxException e) { - LOGGER.error("Error occurred during copying of the gitignore file into the git repository."); + LOGGER.error("Error occurred during copying of the gitignore file into the git repository.", e); } } diff --git a/src/main/resources/org/jabref/http/server/http-server-demo.bib b/src/main/resources/org/jabref/http/server/http-server-demo.bib new file mode 100644 index 00000000000..f5374fef3ad --- /dev/null +++ b/src/main/resources/org/jabref/http/server/http-server-demo.bib @@ -0,0 +1,14 @@ +@InProceedings{Kopp2018, + author = {Kopp, Oliver and Armbruster, Anita and Zimmermann, Olaf}, + booktitle = {Proceedings of the 10th Central European Workshop on Services and their Composition ({ZEUS} 2018)}, + title = {Markdown Architectural Decision Records: Format and Tool Support}, + year = {2018}, + editor = {Nico Herzberg and Christoph Hochreiner and Oliver Kopp and J{\"{o}}rg Lenhard}, + pages = {55--62}, + publisher = {CEUR-WS.org}, + series = {{CEUR} Workshop Proceedings}, + volume = {2072}, + keywords = {ADR, MADR, architecture decision records, architectural decision records, Nygard}, +} + +@Comment{jabref-meta: databaseType:bibtex;} diff --git a/src/main/resources/tinylog.properties b/src/main/resources/tinylog.properties index b4340fa32e9..5347cea4ca5 100644 --- a/src/main/resources/tinylog.properties +++ b/src/main/resources/tinylog.properties @@ -7,3 +7,5 @@ writerAzure = application insights exception = strip: jdk.internal #level@org.jabref.model.entry.BibEntry = debug + +level@org.jabref.http.server.Server = debug diff --git a/src/test/java/org/jabref/http/server/TestServer.java b/src/test/java/org/jabref/http/server/TestServer.java deleted file mode 100644 index 6ad1edcd970..00000000000 --- a/src/test/java/org/jabref/http/server/TestServer.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.jabref.http.server; - -import java.util.concurrent.CountDownLatch; - -import org.slf4j.bridge.SLF4JBridgeHandler; - -public class TestServer { - public static void main(final String[] args) throws InterruptedException { - SLF4JBridgeHandler.removeHandlersForRootLogger(); - SLF4JBridgeHandler.install(); - Server.startServer(new CountDownLatch(1)); - Thread.currentThread().join(); - } -} diff --git a/src/test/resources/testbib/jabref-authors.bib b/src/test/resources/testbib/jabref-authors.bib index 87a7c5eeb46..8687068e425 100644 --- a/src/test/resources/testbib/jabref-authors.bib +++ b/src/test/resources/testbib/jabref-authors.bib @@ -3003,6 +3003,19 @@ @InProceedings{SimonDietzDiezEtAl2019 priority = {prio1}, } +@InProceedings{Kopp2018, + author = {Kopp, Oliver and Armbruster, Anita and Zimmermann, Olaf}, + booktitle = {Proceedings of the 10th Central European Workshop on Services and their Composition ({ZEUS} 2018)}, + date = {2018}, + title = {Markdown Architectural Decision Records: Format and Tool Support}, + editor = {Nico Herzberg and Christoph Hochreiner and Oliver Kopp and J{\"{o}}rg Lenhard}, + pages = {55--62}, + publisher = {CEUR-WS.org}, + series = {{CEUR} Workshop Proceedings}, + volume = {2072}, + keywords = {ADR, MADR, architecture decision records, architectural decision records, Nygard}, +} + @Comment{jabref-meta: databaseType:biblatex;} @Comment{jabref-meta: grouping: