Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces test http server #9748

Merged
merged 27 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7c1df88
Introduces test http server
koppor Apr 8, 2023
37dd6b9
Remove temporary code
koppor Apr 8, 2023
8f05677
Fix checkstyle
koppor Apr 8, 2023
057e982
Create http-server.md with a how-to for the SSL certificate generation
koppor Apr 10, 2023
0d5a695
Merge remote-tracking branch 'upstream/main' into min-http-api
koppor Apr 20, 2023
be12664
Enables passing files to a test Server
koppor Apr 20, 2023
817d77d
Merge branch 'main' into min-http-api
koppor Apr 22, 2023
9299917
Merge remote-tracking branch 'upstream/main' into min-http-api
Siedlerchr Sep 3, 2023
9cde0a6
fix some gradle tasks
Siedlerchr Sep 3, 2023
7fb5100
fix test build
Siedlerchr Sep 3, 2023
75e7de1
Fix broken JavaDoc
koppor Sep 3, 2023
ef1c34a
upgrade jersey to 3.1.3
Siedlerchr Sep 3, 2023
f003363
Merge branch 'min-http-api' of github.com:JabRef/jabref into min-http…
Siedlerchr Sep 3, 2023
94a05ca
Fix checkstyle
koppor Sep 3, 2023
e09ead0
Update http-server.md
koppor Sep 3, 2023
29072fc
rewrite run
Siedlerchr Sep 3, 2023
7f0172a
Merge branch 'min-http-api' of github.com:JabRef/jabref into min-http…
Siedlerchr Sep 3, 2023
59788d0
rename to avoid clash
Siedlerchr Sep 3, 2023
018277e
Hints on http server
koppor Sep 3, 2023
1c15c60
Update http-server.md
koppor Sep 3, 2023
2e9ae7d
Better parameter name
koppor Sep 3, 2023
2d9e8db
Add missing "requires"
koppor Sep 3, 2023
bccf22d
Create resource path only if required
koppor Sep 3, 2023
72d0593
Refine howto
koppor Sep 3, 2023
19e73e6
Add IntelliJ hint
koppor Sep 3, 2023
6f51b07
Add AllowedToUseStandardStreams
koppor Sep 4, 2023
8deec24
Also exclude TestBibFile
koppor Sep 4, 2023
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ gradlew text eol=lf

# .bib files have to be written using OS specific line endings to enable our tests working
*.bib text !eol
# Exception: The files used for the http server test - they should have linux line endings
src/test/resources/org/jabref/http/server/*.bib text eol=lf

# Citavi needs to be LF line ending
# This overwrites the setting of "*.bib"
Expand Down
44 changes: 36 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ dependencies {
implementation "org.tinylog:slf4j-tinylog:2.6.2"
implementation "org.tinylog:tinylog-impl:2.6.2"

// route all requests to java.util.logging to SLF4J (which in turn routes to tinylog)
implementation 'org.slf4j:jul-to-slf4j:2.0.7'

implementation 'de.undercouch:citeproc-java:3.0.0-beta.2'

// jakarta.activation is already dependency of glassfish
Expand All @@ -213,6 +216,23 @@ dependencies {
implementation group: 'net.harawata', name: 'appdirs', version: '1.2.1'

implementation group: 'org.jooq', name: 'jool', version: '0.9.15'
// JAX-RS implemented by Jersey
// API
implementation 'jakarta.ws.rs:jakarta.ws.rs-api:3.1.0'
// Implementation of the API
implementation 'org.glassfish.jersey.core:jersey-server:3.1.1'
// injection framework
implementation 'org.glassfish.jersey.inject:jersey-hk2:3.1.3'
implementation 'org.glassfish.hk2:hk2-api:2.6.1'
// testImplementation 'org.glassfish.hk2:hk2-testing:3.0.4'
// implementation 'org.glassfish.hk2:hk2-testing-jersey:3.0.4'
// testImplementation 'org.glassfish.hk2:hk2-junitrunner:3.0.4'
// HTTP server
// implementation 'org.glassfish.jersey.containers:jersey-container-netty-http:3.1.1'
implementation 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:3.1.3'
testImplementation 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:3.1.3'
// Allow objects "magically" to be mapped to JSON using GSON
// implementation 'org.glassfish.jersey.media:jersey-media-json-gson:3.1.1'

testImplementation 'io.github.classgraph:classgraph:4.8.162'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
Expand Down Expand Up @@ -372,6 +392,12 @@ run {
'javafx.graphics/javafx.scene' : 'org.controlsfx.controls'
]
}

if (project.hasProperty('component')){
if (component == 'httpserver'){
main = 'org.jabref.http.server.Server'
}
}
}

javadoc {
Expand Down Expand Up @@ -410,7 +436,7 @@ testlogger {
showSkipped false
}

task databaseTest(type: Test) {
tasks.register('databaseTest', Test) {
useJUnitPlatform {
includeTags 'DatabaseTest'
}
Expand All @@ -422,7 +448,7 @@ task databaseTest(type: Test) {
}
}

task fetcherTest(type: Test) {
tasks.register('fetcherTest', Test) {
useJUnitPlatform {
includeTags 'FetcherTest'
}
Expand All @@ -434,7 +460,7 @@ task fetcherTest(type: Test) {
}
}

task guiTest(type: Test) {
tasks.register('guiTest', Test) {
useJUnitPlatform {
includeTags 'GUITest'
}
Expand All @@ -447,7 +473,7 @@ task guiTest(type: Test) {
}

// Test result tasks
task copyTestResources(type: Copy) {
tasks.register('copyTestResources', Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
Expand All @@ -457,11 +483,13 @@ tasks.withType(Test) {
reports.html.outputLocation.set(file("${reporting.baseDir}/${name}"))
}

task jacocoPrepare() {
tasks.register('jacocoPrepare') {
doFirst {
// Ignore failures of tests
tasks.withType(Test) {
ignoreFailures = true
tasks.withType(Test).tap {
configureEach {
ignoreFailures = true
}
}
}
}
Expand Down Expand Up @@ -519,7 +547,7 @@ modernizer {
}

// Release tasks
task deleteInstallerTemp(type: Delete) {
tasks.register('deleteInstallerTemp', Delete) {
delete "$buildDir/installer"
}

Expand Down
67 changes: 67 additions & 0 deletions docs/code-howtos/http-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
parent: Code Howtos
---
# HTTP Server

## Get SSL Working

(Based on <https://stackoverflow.com/a/57511038/873282>)

Howto for Windows - other operating systems work similar:

1. As admin `choco install mkcert`
2. As admin: `mkcert -install`
3. `cd %APPDATA%\..\local\org.jabref\jabref\ssl`
4. `mkcert -pkcs12 jabref.desktop jabref localhost 127.0.0.1 ::1`
5. Rename the file to `server.p12`

Note: If you do not do this, you get following error message:

Could not find server key store C:\Users\USERNAME\AppData\Local\org.jabref\jabref\ssl\server.p12.

## Start http server

The class starting the server is `org.jabref.http.server.Server`.

Test files to server can be passed as arguments.
If no files are passed, the last opened files are served.
If that list is also empty, the file `src/main/resources/org/jabref/http/server/http-server-demo.bib` is served.

### Starting with gradle

Does not work.

Current try:

./gradlew run -Pcomment=httpserver

However, there are with `ForkJoin` (discussion at https://discuss.gradle.org/t/is-it-ok-to-use-collection-parallelstream-or-other-potentially-multi-threaded-code-within-gradle-plugin-code/28003)

Gradle output:

```
> Task :run
2023-04-22 11:30:59 [main] org.jabref.http.server.Server.main()
DEBUG: Libraries served: [C:\git-repositories\jabref-all\jabref\src\main\resources\org\jabref\http\server\http-server-demo.bib]
2023-04-22 11:30:59 [main] org.jabref.http.server.Server.startServer()
DEBUG: Starting server...
<============-> 92% EXECUTING [2m 27s]
> :run
```

IntelliJ output, if `org.jabref.http.server.Server#main` is executed:

```
DEBUG: Starting server...
2023-04-22 11:44:59 [ForkJoinPool.commonPool-worker-1] org.glassfish.grizzly.http.server.NetworkListener.start()
INFO: Started listener bound to [localhost:6051]
2023-04-22 11:44:59 [ForkJoinPool.commonPool-worker-1] org.glassfish.grizzly.http.server.HttpServer.start()
INFO: [HttpServer] Started.
2023-04-22 11:44:59 [ForkJoinPool.commonPool-worker-1] org.jabref.http.server.Server.lambda$startServer$4()
DEBUG: Server started.
```

## Developing with IntelliJ

IntelliJ Ultimate offers a Markdown-based http-client. One has to open the file `src/test/java/org/jabref/testutils/interactive/http/rest-api.http`.
Then, there are play buttons appearing for interacting with the server.
53 changes: 53 additions & 0 deletions docs/decisions/0027-http-return-bibtex-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
nav_order: 27
parent: Decision Records
---
<!-- we need to disable MD025, because we use the different heading "ADR Template" in the homepage (see above) than it is foreseen in the template -->
<!-- markdownlint-disable-next-line MD025 -->
# Return BibTeX string and CSL Item JSON in the API

## Context and Problem Statement

In the context of an http server, when a http client `GETs` a JSON data structure containing BibTeX data, which format should that have?

## Considered Options

* Offer both, BibTeX string and CSL JSON
* Return BibTeX as is as string
* Convert BibTeX to JSON

## Decision Outcome

Chosen option: "Offer both, BibTeX string and CSL JSON", because there are many browser libraries out there being able to parse BibTeX. Thus, we don't need to convert it.

## Pros and Cons of the Options

### Offer both, BibTeX string and CSL JSON

- Good, because this follows "Backend for Frontend"
- Good, because Word Addin works seamless with the data provided (and does not need another dependency)
- Good, because other clients can work with BibTeX data
- Bad, because two serializations have to be kept

### Return BibTeX as is as string

- Good, because we don't need to think about any conversion
- Bad, because it is unclear how to ship BibTeX data where the entry is dependent on
- Bad, because client needs add additional parsing logic

### Convert BibTeX to JSON

More thought has to be done when converting to JSON.
There seems to be a JSON format from [@citation-js/plugin-bibtex](https://www.npmjs.com/package/@citation-js/plugin-bibtex).
We could do an additional self-made JSON format, but this increases the number of available JSON serializations for BibTeX.

- Good, because it could flatten BibTeX data (example: `author = first # " and " # second`)
- Bad, because conversion is difficult in BibTeX special cases. For instance, if Strings are used (example: `author = first # " and " # second`) and one doesn't want to flatten ("normalize") this.

## More Information

Existing JavaScript BibTeX libraries:

* [bibtex-js](https://github.com/digitalheir/bibtex-js)
* [bibtexParseJS](https://github.com/ORCID/bibtexParseJs)
* [@citation-js/plugin-bibtex](https://www.npmjs.com/package/@citation-js/plugin-bibtex)
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Then double click inside the cell "Compilation options" and enter following para
```text
--add-exports=javafx.controls/com.sun.javafx.scene.control=org.jabref
--add-exports=org.controlsfx.controls/impl.org.controlsfx.skin=org.jabref
--add-reads org.jabref=org.fxmisc.flowless
--add-reads org.jabref=org.apache.commons.csv
```

Press <kbd>Enter</kbd> to have the value really stored.
Expand Down
68 changes: 43 additions & 25 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
requires com.dlsc.gemsfx;
uses com.dlsc.gemsfx.TagsField;
requires de.saxsys.mvvmfx;
requires reactfx;
requires org.fxmisc.flowless;

requires org.kordamp.ikonli.core;
requires org.kordamp.ikonli.javafx;
Expand All @@ -39,6 +41,7 @@

// Logging
requires org.slf4j;
requires jul.to.slf4j;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this jul adapter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The Grizzly http server uses java-util-logging. To be able to handle the log output with our logging framework tinylog, we need jul-slf4j-tinylog.

More detailed:

  • We opted for SLF4J as the JabRef logging framework. We decided against direct calls to tinylog.
  • For log output of an application, there should be one logging framework
  • When using the http server, JabRef internal methods are used. They might log. Thus, we have logs using of SLF4J
  • Grizzly uses Java-Util-Logging - and outputs are produced.

The alternative can be to disable logging of Grizzly completely and bring it back (with the bridge) if needed.

requires org.tinylog.api;
requires org.tinylog.api.slf4j;
requires org.tinylog.impl;
Expand All @@ -49,45 +52,57 @@

// Preferences and XML
requires java.prefs;

// Annotations (@PostConstruct)
requires jakarta.annotation;
requires jakarta.inject;

// http server and client exchange
requires java.net.http;
requires jakarta.ws.rs;
requires grizzly.framework;

// data mapping
requires jakarta.xml.bind;
requires jdk.xml.dom;
requires com.google.gson;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.yaml;
requires com.fasterxml.jackson.datatype.jsr310;
// needs to be loaded here as it's otherwise not found at runtime
requires org.glassfish.jaxb.runtime;
requires jdk.xml.dom;

// Annotations (@PostConstruct)
requires jakarta.annotation;
// dependency injection using HK2
requires org.glassfish.hk2.api;

// Microsoft application insights
requires applicationinsights.core;
requires applicationinsights.logging.log4j2;

// Libre Office
requires org.libreoffice.uno;

// Other modules
requires com.google.common;
requires jakarta.inject;
requires reactfx;
requires commons.cli;
requires com.github.tomtung.latex2unicode;
requires fastparse;
requires jbibtex;
requires citeproc.java;
requires de.saxsys.mvvmfx.validation;
requires com.google.gson;
// http clients
requires unirest.java;
requires org.apache.httpcomponents.httpclient;
requires org.jsoup;
requires org.apache.commons.csv;
requires io.github.javadiffutils;
requires java.string.similarity;

// SQL databases
requires ojdbc10;
requires org.postgresql.jdbc;
requires org.mariadb.jdbc;
uses org.mariadb.jdbc.credential.CredentialPlugin;

// Apache Commons and other (similar) helper libraries
requires commons.cli;
requires org.apache.commons.csv;
requires org.apache.commons.lang3;
requires org.antlr.antlr4.runtime;
requires org.fxmisc.flowless;
requires com.google.common;
requires io.github.javadiffutils;
requires java.string.similarity;

requires com.github.tomtung.latex2unicode;
requires fastparse;

requires jbibtex;
requires citeproc.java;

requires org.apache.pdfbox;
requires org.apache.xmpbox;
Expand All @@ -113,14 +128,17 @@
requires org.apache.lucene.analysis.common;
requires org.apache.lucene.highlighter;

requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.yaml;
requires com.fasterxml.jackson.datatype.jsr310;
requires net.harawata.appdirs;
requires com.sun.jna;
requires com.sun.jna.platform;

requires org.eclipse.jgit;
uses org.eclipse.jgit.transport.SshSessionFactory;
uses org.eclipse.jgit.lib.GpgSigner;

// other libraries
requires org.antlr.antlr4.runtime;
requires org.libreoffice.uno;
requires de.saxsys.mvvmfx.validation;

}
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/cli/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.tinylog.configuration.Configuration;

/**
Expand All @@ -46,6 +47,7 @@ public class Launcher {
private static boolean isDebugEnabled;

public static void main(String[] args) {
routeLoggingToSlf4J();
ARGUMENTS = args;

// We must configure logging as soon as possible, which is why we cannot wait for the usual
Expand Down Expand Up @@ -103,6 +105,11 @@ public static void main(String[] args) {
}
}

private static void routeLoggingToSlf4J() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

/**
* This needs to be called as early as possible. After the first log write, it
* is not possible to alter
Expand Down
Loading
Loading