forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from guenesaydin/grobid-request-server-async
Grobid request server async
- Loading branch information
Showing
11 changed files
with
186 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,42 @@ | ||
package org.jabref.logic.net; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpEntity; | ||
import org.jabref.Globals; | ||
import org.jabref.preferences.JabRefPreferences; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
|
||
/** | ||
* Implements an API to a GROBID server, as described at | ||
* https://grobid.readthedocs.io/en/latest/Grobid-service/#grobid-web-services | ||
* | ||
* The methods are structured to match the GROBID server api. | ||
* Each method corresponds to a GROBID service request. Only the ones already used are already implemented. | ||
*/ | ||
public class GrobidClient { | ||
|
||
public static String processCitation(String rawCitation, int consolidateCitations) throws GrobidClientException { | ||
try { | ||
if (consolidateCitations < 0 || consolidateCitations > 2) { | ||
throw new GrobidClientException(""); | ||
} | ||
HttpEntity httpResponse = HttpPostService.sendPostAndWait( | ||
Globals.prefs.get(JabRefPreferences.CUSTOM_GROBID_SERVER), | ||
"/api/processCitation", | ||
Map.of("citations", rawCitation, "consolidateCitations", String.valueOf(consolidateCitations)) | ||
).getEntity(); | ||
if (httpResponse == null) { | ||
throw new GrobidClientException("The GROBID server response does not contain anything."); | ||
} | ||
InputStream serverResponseAsStream = httpResponse.getContent(); | ||
return IOUtils.toString(serverResponseAsStream, StandardCharsets.UTF_8); | ||
} catch (HttpPostServiceException | IOException e) { | ||
throw new GrobidClientException(); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/jabref/logic/net/GrobidClientException.java
This file contains 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,30 @@ | ||
package org.jabref.logic.net; | ||
|
||
import org.jabref.JabRefException; | ||
|
||
public class GrobidClientException extends JabRefException { | ||
|
||
public GrobidClientException() { | ||
super("An error occurred while processing your query."); | ||
} | ||
|
||
public GrobidClientException(String message) { | ||
super(message); | ||
} | ||
|
||
public static GrobidClientException getNewGrobidClientExceptionByCode(int httpCode) { | ||
switch(httpCode) { | ||
case 204: | ||
return new GrobidClientException("The GROBID service could not extract any Information from this String."); | ||
case 400: | ||
return new GrobidClientException("The generated Request was wrong."); | ||
case 500: | ||
return new GrobidClientException("An internal GROBID service error occured."); | ||
case 503: | ||
return new GrobidClientException("There are too many requests at a time, please try again later."); | ||
default: | ||
return new GrobidClientException("An error occured processing your GROBID request."); | ||
} | ||
} | ||
|
||
} |
This file contains 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
15 changes: 15 additions & 0 deletions
15
src/main/java/org/jabref/logic/net/HttpPostServiceException.java
This file contains 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,15 @@ | ||
package org.jabref.logic.net; | ||
|
||
import org.jabref.JabRefException; | ||
|
||
public class HttpPostServiceException extends JabRefException { | ||
|
||
public HttpPostServiceException() { | ||
super("An error occurred connecting to the external http server."); | ||
} | ||
|
||
public HttpPostServiceException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
Oops, something went wrong.