-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Implement BibEntryResource — Fixes #13588 #14292
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c4db0b1
BitEntryClass created
Mbungai-Francesco 964e561
Add and update test files for BibEntry and Library resources
Mbungai-Francesco 170cb5e
Merge branch 'JabRef:main' into bibTest
UserEstrella 046be67
Merge branch 'JabRef:main' into bibTest
UserEstrella 474a824
fix checkstyle issue PR
UserEstrella e5a01c0
Fix: added missing newline at end of file and remove unused import (C…
UserEstrella 2221d0a
Merge branch 'JabRef:main' into bibTest
UserEstrella 4d3c8c5
adding BibEntryResource as source to jbang
UserEstrella 40a6714
Merge branch 'bibTest' of https://github.com/paulamalvarador-commits/…
UserEstrella ad3607f
Merge branch 'JabRef:main' into bibTest
UserEstrella 567356c
Merge branch 'JabRef:main' into bibTest
UserEstrella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
jabsrv/src/main/java/org/jabref/http/server/resources/BibEntryResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package org.jabref.http.server.resources; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import org.jabref.http.JabrefMediaType; | ||
| import org.jabref.http.SrvStateManager; | ||
| import org.jabref.http.dto.BibEntryDTO; | ||
| import org.jabref.http.server.services.FilesToServe; | ||
| import org.jabref.http.server.services.ServerUtils; | ||
| import org.jabref.logic.citationstyle.JabRefItemDataProvider; | ||
| import org.jabref.logic.preferences.CliPreferences; | ||
| import org.jabref.model.database.BibDatabase; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
| import org.jabref.model.entry.BibEntryTypesManager; | ||
|
|
||
| import com.airhacks.afterburner.injection.Injector; | ||
| import com.google.gson.Gson; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.GET; | ||
| import jakarta.ws.rs.InternalServerErrorException; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.PathParam; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
| import jakarta.ws.rs.core.StreamingOutput; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @Path("libraries/{id}") | ||
| public class BibEntryResource { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(BibEntryResource.class); | ||
|
|
||
| @Inject | ||
| CliPreferences preferences; | ||
|
|
||
| @Inject | ||
| SrvStateManager srvStateManager; | ||
|
|
||
| @Inject | ||
| FilesToServe filesToServe; | ||
|
|
||
| @Inject | ||
| Gson gson; | ||
|
|
||
| /** | ||
| * At http://localhost:23119/libraries/{id} | ||
| * | ||
| * @param id The specified library | ||
| * @return specified library in JSON format | ||
| * @throws IOException | ||
| */ | ||
| @GET | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public String getJson(@PathParam("id") String id) throws IOException { | ||
| BibDatabaseContext databaseContext = getDatabaseContext(id); | ||
| BibEntryTypesManager entryTypesManager = Injector.instantiateModelOrService(BibEntryTypesManager.class); | ||
| List<BibEntryDTO> list = databaseContext.getDatabase().getEntries().stream() | ||
| .peek(bibEntry -> bibEntry.getSharedBibEntryData().setSharedID(Objects.hash(bibEntry))) | ||
| .map(entry -> new BibEntryDTO(entry, databaseContext.getMode(), preferences.getFieldPreferences(), entryTypesManager)) | ||
| .toList(); | ||
| return gson.toJson(list); | ||
| } | ||
|
|
||
| @GET | ||
| @Produces(JabrefMediaType.JSON_CSL_ITEM) | ||
| public String getClsItemJson(@PathParam("id") String id) throws IOException { | ||
| BibDatabaseContext databaseContext = getDatabaseContext(id); | ||
| JabRefItemDataProvider jabRefItemDataProvider = new JabRefItemDataProvider(); | ||
| jabRefItemDataProvider.setData(databaseContext, new BibEntryTypesManager()); | ||
| return jabRefItemDataProvider.toJson(); | ||
| } | ||
|
|
||
| @GET | ||
| @Produces(JabrefMediaType.BIBTEX) | ||
| public Response getBibtex(@PathParam("id") String id) { | ||
| if ("demo".equals(id)) { | ||
| StreamingOutput stream = output -> { | ||
| try (InputStream in = getChocolateBibAsStream()) { | ||
| in.transferTo(output); | ||
| } | ||
| }; | ||
|
|
||
| return Response.ok(stream) | ||
| // org.glassfish.jersey.media would be required for a "nice" Java to create ContentDisposition; we avoid this | ||
| .header("Content-Disposition", "attachment; filename=\"Chocolate.bib\"") | ||
| .build(); | ||
| } | ||
|
|
||
| java.nio.file.Path library = ServerUtils.getLibraryPath(id, filesToServe, srvStateManager); | ||
| String libraryAsString; | ||
| try { | ||
| libraryAsString = Files.readString(library); | ||
| } catch (IOException e) { | ||
| LOGGER.error("Could not read library {}", library, e); | ||
| throw new InternalServerErrorException("Could not read library " + library, e); | ||
| } | ||
| return Response.ok() | ||
| .header("Content-Disposition", "attachment; filename=\"" + library.getFileName() + "\"") | ||
| .entity(libraryAsString) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * @return a stream to the Chocolate.bib file in the classpath (is null only if the file was moved or there are issues with the classpath) | ||
| */ | ||
| private @Nullable InputStream getChocolateBibAsStream() { | ||
| return BibDatabase.class.getResourceAsStream("/Chocolate.bib"); | ||
| } | ||
|
|
||
| /// @param id - also "demo" for the Chocolate.bib file | ||
| private BibDatabaseContext getDatabaseContext(String id) throws IOException { | ||
| return ServerUtils.getBibDatabaseContext(id, filesToServe, srvStateManager, preferences.getImportFormatPreferences()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
jabsrv/src/test/java/org/jabref/http/server/BibEntryResourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.jabref.http.server; | ||
|
|
||
| import org.jabref.http.JabrefMediaType; | ||
| import org.jabref.http.server.resources.BibEntryResource; | ||
|
|
||
| import jakarta.ws.rs.core.Application; | ||
| import org.glassfish.jersey.server.ResourceConfig; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class BibEntryResourceTest extends ServerTest { | ||
|
|
||
| @Override | ||
| protected Application configure() { | ||
| ResourceConfig resourceConfig = new ResourceConfig(BibEntryResource.class); | ||
| addFilesToServeToResourceConfig(resourceConfig); | ||
| addGuiBridgeToResourceConfig(resourceConfig); | ||
| addPreferencesToResourceConfig(resourceConfig); | ||
| addGsonToResourceConfig(resourceConfig); | ||
| return resourceConfig.getApplication(); | ||
| } | ||
|
|
||
| @Test | ||
| void getJson() { | ||
| String response = target("/libraries/" + TestBibFile.GENERAL_SERVER_TEST.id).request().get(String.class); | ||
| // Basic sanity check: response is a JSON array and contains the entry id | ||
| assertTrue(response.trim().startsWith("[")); | ||
| assertTrue(response.contains("\"Author2023test\"")); | ||
| } | ||
|
|
||
| @Test | ||
| void getCslItemJson() { | ||
| String response = target("/libraries/" + TestBibFile.GENERAL_SERVER_TEST.id).request(JabrefMediaType.JSON_CSL_ITEM).get(String.class); | ||
| assertEquals(""" | ||
| [{"id":"Author2023test","type":"article","author":[{"family":"Author","given":"Demo"}],"event-date":{"date-parts":[[2023]]},"issued":{"date-parts":[[2023]]},"title":"Demo Title"}]""", response); | ||
| } | ||
|
|
||
| @Test | ||
| void getBibtex() { | ||
| // For non-demo libraries the test reads the test file content; this asserts we get a BibTeX-like response | ||
| String response = target("/libraries/" + TestBibFile.GENERAL_SERVER_TEST.id).request(JabrefMediaType.BIBTEX).get(String.class); | ||
| assertTrue(response.contains("@Misc{Author2023test")); | ||
| assertTrue(response.contains("jabref-meta: databaseType:bibtex")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, this is comment wrong, we are at the BibEntry resource
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback! Do you mean we should create a new specific end-point for the BibEntry resource? Something like this : <http://localhost:23119/librairies/demo/entries/{id}>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope you already did.
Note: It already worked like this. The only issue was that the methods handling that were nested in the library class.