Skip to content

Commit

Permalink
Replace new instantiation of BuildInfo with calls to Injector (JabRef…
Browse files Browse the repository at this point in the history
…#11954)

* Replace new instantiation of BuildInfo with calls to Injector

* Extract call to default keys out of fetchers

* Inject buildInfo

* Integrate SemanticScholarFetcher

* Integrate IEEE

* Add warning log

* Adapt devdocs

* Integrate SpringerLink

* Add comment

* Enhance doc

* Enhance doc
  • Loading branch information
calixtus authored and ExrosZ-Alt committed Oct 21, 2024
1 parent 790a47e commit b60d898
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 66 deletions.
10 changes: 8 additions & 2 deletions docs/code-howtos/fetchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ In `build.gradle`, these variables are filled:
"springerNatureAPIKey" : System.getenv('SpringerNatureAPIKey')
```

The `BuildInfo` class reads from that file.
The `BuildInfo` class reads from that file and the key needs to be put into the map of default API keys in `JabRefCliPreferences::getDefaultFetcherKeys`.

```java
new BuildInfo().springerNatureAPIKey
keys.put(SpringerFetcher.FETCHER_NAME, buildInfo.springerNatureAPIKey);
```

The fetcher api key can then be obtained by calling the preferences.

```java
importerPreferences.getApiKey(SpringerFetcher.FETCHER_NAME);
```

When executing `./gradlew run`, gradle executes `processResources` and populates `build/build.properties` accordingly. However, when working directly in the IDE, Eclipse keeps reading `build.properties` from `src/main/resources`. In IntelliJ, the task `JabRef Main` is executing `./gradlew processResources` before running JabRef from the IDE to ensure the `build.properties` is properly populated.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.Directories;
import org.jabref.logic.util.HeadlessExecutorService;
import org.jabref.logic.util.Version;
import org.jabref.migrations.PreferencesMigrations;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.util.DirectoryMonitor;
Expand Down Expand Up @@ -140,7 +141,8 @@ private static void initLogging(String[] args) {
}

// addLogToDisk
Path directory = Directories.getLogDirectory();
Version version = Injector.instantiateModelOrService(BuildInfo.class).version;
Path directory = Directories.getLogDirectory(version);
try {
Files.createDirectories(directory);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.importer.fetcher.CustomizableKeyFetcher;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;

import com.google.gson.Gson;

public class SemanticScholarFetcher implements CitationFetcher, CustomizableKeyFetcher {
private static final String SEMANTIC_SCHOLAR_API = "https://api.semanticscholar.org/graph/v1/";
public static final String FETCHER_NAME = "Semantic Scholar Citations Fetcher";

private static final String API_KEY = new BuildInfo().semanticScholarApiKey;
private static final String SEMANTIC_SCHOLAR_API = "https://api.semanticscholar.org/graph/v1/";

private final ImporterPreferences importerPreferences;

Expand Down Expand Up @@ -45,10 +44,8 @@ public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException {
}
URLDownload urlDownload = new URLDownload(citationsUrl);

String apiKey = getApiKey();
if (!apiKey.isEmpty()) {
urlDownload.addHeader("x-api-key", apiKey);
}
importerPreferences.getApiKey(getName()).ifPresent(apiKey -> urlDownload.addHeader("x-api-key", apiKey));

CitationsResponse citationsResponse = new Gson()
.fromJson(urlDownload.asString(), CitationsResponse.class);

Expand All @@ -71,10 +68,7 @@ public List<BibEntry> searchCiting(BibEntry entry) throws FetcherException {
}

URLDownload urlDownload = new URLDownload(referencesUrl);
String apiKey = getApiKey();
if (!apiKey.isEmpty()) {
urlDownload.addHeader("x-api-key", apiKey);
}
importerPreferences.getApiKey(getName()).ifPresent(apiKey -> urlDownload.addHeader("x-api-key", apiKey));
ReferencesResponse referencesResponse = new Gson()
.fromJson(urlDownload.asString(), ReferencesResponse.class);

Expand All @@ -86,10 +80,6 @@ public List<BibEntry> searchCiting(BibEntry entry) throws FetcherException {

@Override
public String getName() {
return "Semantic Scholar Citations Fetcher";
}

private String getApiKey() {
return importerPreferences.getApiKey(getName()).orElse(API_KEY);
return FETCHER_NAME;
}
}
11 changes: 10 additions & 1 deletion src/main/java/org/jabref/logic/importer/ImporterPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

Expand All @@ -23,6 +24,7 @@ public class ImporterPreferences {
private final BooleanProperty warnAboutDuplicatesOnImport;
private final ObjectProperty<Path> importWorkingDirectory;
private final ObservableSet<FetcherApiKey> apiKeys;
private final Map<String, String> defaultApiKeys;
private final ObservableSet<CustomImporter> customImporters;
private final BooleanProperty persistCustomKeys;
private final ObservableList<String> catalogs;
Expand All @@ -34,6 +36,7 @@ public ImporterPreferences(boolean importerEnabled,
boolean warnAboutDuplicatesOnImport,
Set<CustomImporter> customImporters,
Set<FetcherApiKey> apiKeys,
Map<String, String> defaultApiKeys,
boolean persistCustomKeys,
List<String> catalogs,
PlainCitationParserChoice defaultPlainCitationParser
Expand All @@ -44,6 +47,7 @@ public ImporterPreferences(boolean importerEnabled,
this.warnAboutDuplicatesOnImport = new SimpleBooleanProperty(warnAboutDuplicatesOnImport);
this.customImporters = FXCollections.observableSet(customImporters);
this.apiKeys = FXCollections.observableSet(apiKeys);
this.defaultApiKeys = defaultApiKeys;
this.persistCustomKeys = new SimpleBooleanProperty(persistCustomKeys);
this.catalogs = FXCollections.observableArrayList(catalogs);
this.defaultPlainCitationParser = new SimpleObjectProperty<>(defaultPlainCitationParser);
Expand Down Expand Up @@ -122,12 +126,17 @@ public void setPersistCustomKeys(boolean persistCustomKeys) {
this.persistCustomKeys.set(persistCustomKeys);
}

/**
* @param name of the fetcher
* @return either a customized API key if configured or the default key
*/
public Optional<String> getApiKey(String name) {
return apiKeys.stream()
.filter(key -> key.getName().equalsIgnoreCase(name))
.filter(FetcherApiKey::shouldUse)
.findFirst()
.map(FetcherApiKey::getKey);
.map(FetcherApiKey::getKey)
.or(() -> Optional.ofNullable(defaultApiKeys.get(name)));
}

public void setCatalogs(List<String> catalogs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.jabref.logic.importer.fetcher.transformers.DefaultQueryTransformer;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
Expand All @@ -48,11 +47,11 @@
*/
public class AstrophysicsDataSystem
implements IdBasedParserFetcher, PagedSearchBasedParserFetcher, EntryBasedParserFetcher, CustomizableKeyFetcher {
public static final String FETCHER_NAME = "SAO/NASA ADS";

private static final String API_SEARCH_URL = "https://api.adsabs.harvard.edu/v1/search/query";
private static final String API_EXPORT_URL = "https://api.adsabs.harvard.edu/v1/export/bibtexabs";

private static final String API_KEY = new BuildInfo().astrophysicsDataSystemAPIKey;
private final ImportFormatPreferences preferences;
private final ImporterPreferences importerPreferences;

Expand All @@ -79,7 +78,7 @@ private static URL getURLforExport() throws URISyntaxException, MalformedURLExce

@Override
public String getName() {
return "SAO/NASA ADS";
return FETCHER_NAME;
}

/**
Expand Down Expand Up @@ -255,7 +254,7 @@ private List<BibEntry> performSearchByIds(Collection<String> identifiers) throws
try {
String postData = buildPostData(ids);
URLDownload download = new URLDownload(urLforExport);
download.addHeader("Authorization", "Bearer " + importerPreferences.getApiKey(getName()).orElse(API_KEY));
importerPreferences.getApiKey(getName()).ifPresent(key -> download.addHeader("Authorization", "Bearer " + key));
download.addHeader("ContentType", "application/json");
download.setPostData(postData);
String content = download.asString();
Expand Down Expand Up @@ -308,7 +307,7 @@ public Page<BibEntry> performSearchPaged(QueryNode luceneQuery, int pageNumber)
@Override
public URLDownload getUrlDownload(URL url) {
URLDownload urlDownload = new URLDownload(url);
urlDownload.addHeader("Authorization", "Bearer " + importerPreferences.getApiKey(getName()).orElse(API_KEY));
importerPreferences.getApiKey(getName()).ifPresent(key -> urlDownload.addHeader("Authorization", "Bearer " + key));
return urlDownload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.jabref.logic.importer.fetcher.transformers.BiodiversityLibraryTransformer;
import org.jabref.logic.importer.util.JsonReader;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
Expand All @@ -37,14 +36,12 @@
* @see <a href="https://www.biodiversitylibrary.org/docs/api3.html">API documentation</a>
*/
public class BiodiversityLibrary implements SearchBasedParserFetcher, CustomizableKeyFetcher {
public static final String FETCHER_NAME = "Biodiversity Heritage";

private static final String API_KEY = new BuildInfo().biodiversityHeritageApiKey;
private static final String BASE_URL = "https://www.biodiversitylibrary.org/api3";
private static final String RESPONSE_FORMAT = "json";
private static final String TEST_URL_WITHOUT_API_KEY = "https://www.biodiversitylibrary.org/api3?apikey=";

private static final String FETCHER_NAME = "Biodiversity Heritage";

private final ImporterPreferences importerPreferences;

public BiodiversityLibrary(ImporterPreferences importerPreferences) {
Expand All @@ -63,7 +60,7 @@ public String getTestUrl() {

public URL getBaseURL() throws URISyntaxException, MalformedURLException {
URIBuilder baseURI = new URIBuilder(BASE_URL);
baseURI.addParameter("apikey", importerPreferences.getApiKey(getName()).orElse(API_KEY));
importerPreferences.getApiKey(getName()).ifPresent(key -> baseURI.addParameter("apikey", key));
baseURI.addParameter("format", RESPONSE_FORMAT);

return baseURI.build().toURL();
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/org/jabref/logic/importer/fetcher/IEEE.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.jabref.logic.importer.fetcher.transformers.IEEEQueryTransformer;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.os.OS;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
Expand Down Expand Up @@ -59,7 +58,6 @@ public class IEEE implements FulltextFetcher, PagedSearchBasedParserFetcher, Cus
private static final Pattern PDF_PATTERN = Pattern.compile("\"(https://ieeexplore.ieee.org/ielx[0-9/]+\\.pdf[^\"]+)\"");
private static final String IEEE_DOI = "10.1109";
private static final String BASE_URL = "https://ieeexplore.ieee.org";
private static final String API_KEY = new BuildInfo().ieeeAPIKey;
private static final String TEST_URL_WITHOUT_API_KEY = "https://ieeexploreapi.ieee.org/api/v1/search/articles?max_records=0&apikey=";

private final ImportFormatPreferences importFormatPreferences;
Expand Down Expand Up @@ -264,10 +262,6 @@ public Optional<HelpFile> getHelpPage() {
return Optional.of(HelpFile.FETCHER_IEEEXPLORE);
}

private String getApiKey() {
return importerPreferences.getApiKey(getName()).orElse(API_KEY);
}

@Override
public String getTestUrl() {
return TEST_URL_WITHOUT_API_KEY;
Expand All @@ -280,7 +274,7 @@ public URL getURLForQuery(QueryNode luceneQuery, int pageNumber) throws URISynta
transformer = new IEEEQueryTransformer();
String transformedQuery = transformer.transformLuceneQuery(luceneQuery).orElse("");
URIBuilder uriBuilder = new URIBuilder("https://ieeexploreapi.ieee.org/api/v1/search/articles");
uriBuilder.addParameter("apikey", getApiKey());
importerPreferences.getApiKey(FETCHER_NAME).ifPresent(apiKey -> uriBuilder.addParameter("apikey", apiKey));
if (!transformedQuery.isBlank()) {
uriBuilder.addParameter("querytext", transformedQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.jabref.logic.importer.FulltextFetcher;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;
Expand All @@ -35,11 +34,11 @@
* See <a href="https://dev.elsevier.com/">https://dev.elsevier.com/</a>.
*/
public class ScienceDirect implements FulltextFetcher, CustomizableKeyFetcher {
public static final String FETCHER_NAME = "ScienceDirect";

private static final Logger LOGGER = LoggerFactory.getLogger(ScienceDirect.class);

private static final String API_URL = "https://api.elsevier.com/content/article/doi/";
private static final String API_KEY = new BuildInfo().scienceDirectApiKey;
private static final String FETCHER_NAME = "ScienceDirect";

private final ImporterPreferences importerPreferences;

Expand Down Expand Up @@ -140,7 +139,7 @@ private String getUrlByDoi(String doi) throws UnirestException {
try {
String request = API_URL + doi;
HttpResponse<JsonNode> jsonResponse = Unirest.get(request)
.header("X-ELS-APIKey", importerPreferences.getApiKey(getName()).orElse(API_KEY))
.header("X-ELS-APIKey", importerPreferences.getApiKey(getName()).orElse(""))
.queryString("httpAccept", "application/json")
.asJson();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.fetcher.transformers.SpringerQueryTransformer;
import org.jabref.logic.os.OS;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.Month;
Expand All @@ -39,13 +38,11 @@
* @see <a href="https://dev.springernature.com/">API documentation</a> for more details
*/
public class SpringerFetcher implements PagedSearchBasedParserFetcher, CustomizableKeyFetcher {

public static final String FETCHER_NAME = "Springer";

private static final Logger LOGGER = LoggerFactory.getLogger(SpringerFetcher.class);

private static final String API_URL = "https://api.springernature.com/meta/v1/json";
private static final String API_KEY = new BuildInfo().springerNatureAPIKey;
// Springer query using the parameter 'q=doi:10.1007/s11276-008-0131-4s=1' will respond faster
private static final String TEST_URL_WITHOUT_API_KEY = "https://api.springernature.com/meta/v1/json?q=doi:10.1007/s11276-008-0131-4s=1&p=1&api_key=";

Expand Down Expand Up @@ -188,7 +185,7 @@ public String getTestUrl() {
public URL getURLForQuery(QueryNode luceneQuery, int pageNumber) throws URISyntaxException, MalformedURLException {
URIBuilder uriBuilder = new URIBuilder(API_URL);
uriBuilder.addParameter("q", new SpringerQueryTransformer().transformLuceneQuery(luceneQuery).orElse("")); // Search query
uriBuilder.addParameter("api_key", importerPreferences.getApiKey(getName()).orElse(API_KEY)); // API key
importerPreferences.getApiKey(getName()).ifPresent(key -> uriBuilder.addParameter("api_key", key)); // API key
uriBuilder.addParameter("s", String.valueOf(getPageSize() * pageNumber + 1)); // Start entry, starts indexing at 1
uriBuilder.addParameter("p", String.valueOf(getPageSize())); // Page size
return uriBuilder.build().toURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.jabref.logic.importer.FulltextFetcher;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;
Expand All @@ -33,7 +32,6 @@ public class SpringerLink implements FulltextFetcher, CustomizableKeyFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringerLink.class);

private static final String API_URL = "https://api.springer.com/meta/v1/json";
private static final String API_KEY = new BuildInfo().springerNatureAPIKey;
private static final String CONTENT_HOST = "link.springer.com";

private final ImporterPreferences importerPreferences;
Expand All @@ -55,7 +53,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
// Available in catalog?
try {
HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL)
.queryString("api_key", importerPreferences.getApiKey(getName()).orElse(API_KEY))
.queryString("api_key", importerPreferences.getApiKey(getName()).orElse(""))
.queryString("q", "doi:%s".formatted(doi.get().getDOI()))
.asJson();
if (jsonResponse.getBody() != null) {
Expand Down
Loading

0 comments on commit b60d898

Please sign in to comment.