Skip to content
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

Replace new instantiation of BuildInfo with calls to Injector #11954

Merged
merged 11 commits into from
Oct 14, 2024
2 changes: 1 addition & 1 deletion docs/code-howtos/fetchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ In `build.gradle`, these variables are filled:
The `BuildInfo` class reads from that file.

```java
new BuildInfo().springerNatureAPIKey
Injector.instantiateModelOrService(BuildInfo.class).springerNatureAPIKey
calixtus marked this conversation as resolved.
Show resolved Hide resolved
```

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 @@ -12,17 +12,20 @@
import org.jabref.logic.util.BuildInfo;
import org.jabref.model.entry.BibEntry;

import com.airhacks.afterburner.injection.Injector;
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 String apiKey;
private final ImporterPreferences importerPreferences;

public SemanticScholarFetcher(ImporterPreferences importerPreferences) {
this.importerPreferences = importerPreferences;
this.apiKey = Injector.instantiateModelOrService(BuildInfo.class).semanticScholarApiKey;
}

public String getAPIUrl(String entry_point, BibEntry entry) {
Expand Down Expand Up @@ -86,10 +89,10 @@ public List<BibEntry> searchCiting(BibEntry entry) throws FetcherException {

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

private String getApiKey() {
return importerPreferences.getApiKey(getName()).orElse(API_KEY);
return importerPreferences.getApiKey(getName()).orElse(apiKey);
calixtus marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
6 changes: 4 additions & 2 deletions src/main/java/org/jabref/logic/importer/fetcher/IEEE.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.StringUtil;

import com.airhacks.afterburner.injection.Injector;
import kong.unirest.core.json.JSONArray;
import kong.unirest.core.json.JSONObject;
import org.apache.hc.core5.net.URIBuilder;
Expand Down Expand Up @@ -59,15 +60,16 @@ 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 String apiKey;
private final ImportFormatPreferences importFormatPreferences;
private final ImporterPreferences importerPreferences;

private IEEEQueryTransformer transformer;

public IEEE(ImportFormatPreferences importFormatPreferences, ImporterPreferences importerPreferences) {
this.apiKey = Injector.instantiateModelOrService(BuildInfo.class).ieeeAPIKey;
this.importFormatPreferences = Objects.requireNonNull(importFormatPreferences);
this.importerPreferences = Objects.requireNonNull(importerPreferences);
}
Expand Down Expand Up @@ -265,7 +267,7 @@ public Optional<HelpFile> getHelpPage() {
}

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

@Override
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 @@ -14,6 +14,7 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;

import com.airhacks.afterburner.injection.Injector;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
Expand All @@ -33,12 +34,13 @@ 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 String apiKey;
private final ImporterPreferences importerPreferences;

public SpringerLink(ImporterPreferences importerPreferences) {
this.apiKey = Injector.instantiateModelOrService(BuildInfo.class).springerNatureAPIKey;
this.importerPreferences = importerPreferences;
}

Expand All @@ -55,7 +57,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(apiKey))
.queryString("q", "doi:%s".formatted(doi.get().getDOI()))
.asJson();
if (jsonResponse.getBody() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javafx.collections.ListChangeListener;
import javafx.collections.SetChangeListener;

import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.SemanticScholarFetcher;
import org.jabref.logic.FilePreferences;
import org.jabref.logic.InternalPreferences;
import org.jabref.logic.JabRefException;
Expand All @@ -52,9 +53,12 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.importer.fetcher.ACMPortalFetcher;
import org.jabref.logic.importer.fetcher.AstrophysicsDataSystem;
import org.jabref.logic.importer.fetcher.BiodiversityLibrary;
import org.jabref.logic.importer.fetcher.DBLPFetcher;
import org.jabref.logic.importer.fetcher.IEEE;
import org.jabref.logic.importer.fetcher.MrDlibPreferences;
import org.jabref.logic.importer.fetcher.ScienceDirect;
import org.jabref.logic.importer.fetcher.SpringerFetcher;
import org.jabref.logic.importer.fileformat.CustomImporter;
import org.jabref.logic.importer.plaincitation.PlainCitationParserChoice;
Expand All @@ -81,6 +85,7 @@
import org.jabref.logic.search.SearchPreferences;
import org.jabref.logic.shared.prefs.SharedDatabasePreferences;
import org.jabref.logic.shared.security.Password;
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.Directories;
import org.jabref.logic.util.Version;
import org.jabref.logic.util.io.AutoLinkPreferences;
Expand Down Expand Up @@ -1998,6 +2003,7 @@ public ImporterPreferences getImporterPreferences() {
getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION),
getCustomImportFormats(),
getFetcherKeys(),
getDefaultFetcherKeys(),
getBoolean(FETCHER_CUSTOM_KEY_PERSIST),
getStringList(SEARCH_CATALOGS),
PlainCitationParserChoice.valueOf(get(DEFAULT_PLAIN_CITATION_PARSER))
Expand Down Expand Up @@ -2085,6 +2091,22 @@ private List<String> getFetcherKeysFromKeyring(List<String> names) {
return keys;
}

private Map<String, String> getDefaultFetcherKeys() {
BuildInfo buildInfo = Injector.instantiateModelOrService(BuildInfo.class);
if (buildInfo == null) {
return Collections.emptyMap();
calixtus marked this conversation as resolved.
Show resolved Hide resolved
}

Map<String, String> keys = new HashMap<>();
keys.put(SemanticScholarFetcher.FETCHER_NAME, buildInfo.semanticScholarApiKey);
keys.put(AstrophysicsDataSystem.FETCHER_NAME, buildInfo.astrophysicsDataSystemAPIKey);
keys.put(BiodiversityLibrary.FETCHER_NAME, buildInfo.biodiversityHeritageApiKey);
keys.put(ScienceDirect.FETCHER_NAME, buildInfo.scienceDirectApiKey);
keys.put(SpringerFetcher.FETCHER_NAME, buildInfo.springerNatureAPIKey);

return keys;
}

private void storeFetcherKeys(Set<FetcherApiKey> fetcherApiKeys) {
List<String> names = new ArrayList<>();
List<String> uses = new ArrayList<>();
Expand Down
Loading
Loading