Skip to content

Commit

Permalink
Enables passing files to a test Server
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Apr 20, 2023
1 parent 0d5a695 commit be12664
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 18 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
62 changes: 59 additions & 3 deletions src/main/java/org/jabref/http/server/Server.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,91 @@
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<br>
* More files can be provided as args.
*/
public static void main(final String[] args) throws InterruptedException, URISyntaxException {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

final ObservableList<String> 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<String> 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()
.sslContext(sslContext)
.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)));
Expand All @@ -37,7 +94,6 @@ static void startServer(CountDownLatch latch) {
instance.unwrap(Object.class));
System.out.println("Send SIGKILL to shutdown.");
serverInstance = instance;
latch.countDown();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/git/GitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/org/jabref/http/server/http-server-demo.bib
Original file line number Diff line number Diff line change
@@ -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;}
2 changes: 2 additions & 0 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions src/test/java/org/jabref/http/server/TestServer.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/test/resources/testbib/jabref-authors.bib
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit be12664

Please sign in to comment.