-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(jabsrc): add endpoint for adding an entry to the currently opened library #13905
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.jabref.http.dto; | ||
|
|
||
| public class AddEntryDTO { | ||
| private String text; | ||
|
|
||
| public AddEntryDTO() {} | ||
|
|
||
| public AddEntryDTO(String text) { | ||
| this.text = text; | ||
| } | ||
|
|
||
| public String getText() { | ||
| return text; | ||
| } | ||
|
|
||
| public void setText(String text) { | ||
| this.text = text; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package org.jabref.http.server; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.jabref.http.SrvStateManager; | ||
| import org.jabref.http.dto.AddEntryDTO; | ||
| import org.jabref.http.dto.BibEntryDTO; | ||
| import org.jabref.http.server.services.FilesToServe; | ||
| import org.jabref.logic.importer.ParseException; | ||
| import org.jabref.logic.importer.fileformat.BibtexParser; | ||
| import org.jabref.logic.preferences.CliPreferences; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
| import org.jabref.model.entry.BibEntry; | ||
| import org.jabref.model.entry.BibEntryTypesManager; | ||
| import org.jabref.model.entry.event.EntriesEventSource; | ||
|
|
||
| import com.airhacks.afterburner.injection.Injector; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonSyntaxException; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.ws.rs.POST; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @Path("libraries/latest") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, this won't work... REST works differently... a) redirect to path where this is stored -- I don't know how the client works or b) direct handling in Conflicts with REST background: ROA -> https://en.wikipedia.org/wiki/Resource-oriented_architecture - everything is a resource, not a command endpoint (if possible) i will dive into later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, ideal:
|
||
| public class LatestLibraryResource { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(LatestLibraryResource.class); | ||
|
|
||
| @Inject | ||
| private SrvStateManager srvStateManager; | ||
|
|
||
| @Inject | ||
| private FilesToServe filesToServe; | ||
|
|
||
| @Inject | ||
| private CliPreferences preferences; | ||
|
|
||
| @Inject | ||
| private Gson gson; | ||
|
|
||
| @POST | ||
| @Path("entries") | ||
| public Response addEntry(String jsonInput) { | ||
| // Manual JSON parsing with gson.fromJson | ||
| AddEntryDTO request; | ||
| try { | ||
| if (jsonInput == null || jsonInput.trim().isEmpty()) { | ||
|
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The try block covers too many statements, which can make it harder to identify which specific statement caused an exception. |
||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("Missing JSON input"))) | ||
Siedlerchr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
|
|
||
| request = gson.fromJson(jsonInput, AddEntryDTO.class); | ||
| } catch (JsonSyntaxException e) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("Invalid JSON format: " + e.getMessage()))) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
|
|
||
| if (request == null || request.getText() == null || request.getText().trim().isEmpty()) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("Missing or empty 'text' field"))) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
|
|
||
| Optional<BibDatabaseContext> activeDb = srvStateManager.getActiveDatabase(); | ||
| if (activeDb.isEmpty()) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("No active library. Please open a library first."))) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
|
|
||
| String bibtexSource = request.getText(); | ||
|
|
||
| BibtexParser parser = new BibtexParser(preferences.getImportFormatPreferences()); | ||
|
|
||
| try { | ||
| Optional<BibEntry> entry = parser.parseSingleEntry(bibtexSource); | ||
| if (entry.isEmpty()) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("No valid BibTeX entry found"))) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
|
|
||
| activeDb.get().getDatabase().insertEntry(entry.get(), EntriesEventSource.SHARED); | ||
|
|
||
| BibEntryTypesManager entryTypesManager = Injector.instantiateModelOrService(BibEntryTypesManager.class); | ||
| BibEntryDTO dto = new BibEntryDTO(entry.get(), activeDb.get().getMode(), preferences.getFieldPreferences(), entryTypesManager); | ||
|
|
||
| // Manual JSON serialization with gson.toJson | ||
| return Response.ok(gson.toJson(dto)) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } catch (ParseException e) { | ||
| return Response.status(Response.Status.BAD_REQUEST) | ||
| .entity(gson.toJson(new ErrorResponse("Error parsing BibTeX entry: " + e.getMessage()))) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| // Helper class for error responses | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use three slashes 😅 |
||
| private static class ErrorResponse { | ||
| private final String error; | ||
|
|
||
| public ErrorResponse(String error) { | ||
| this.error = error; | ||
| } | ||
|
|
||
| public String getError() { | ||
| return error; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.