-
-
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
Draft
InAnYan
wants to merge
3
commits into
JabRef:main
Choose a base branch
from
InAnYan:feat/add-entry-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
jabsrv/src/main/java/org/jabref/http/server/LatestLibraryResource.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,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") | ||
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; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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 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
LibraryResource
.Conflicts with
@Path("libraries/{id}")
at librariesREST 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 comment
The reason will be displayed to describe this comment to others. Learn more.
A good read on REST https://github.com/stickfigure/blog/wiki/How-to-%28and-how-not-to%29-design-REST-APIs
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.
Well, ideal:
/libraries?sort_by=date&sort_direction=desc
/libraries/#{libraries[0].id}