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

feat: Make http and file document loaders more configurable. #6

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.apicatalog.jsonld.document.Document;
import com.apicatalog.jsonld.document.JsonDocument;
import com.apicatalog.jsonld.http.DefaultHttpClient;
import com.apicatalog.jsonld.http.HttpClient;
import com.apicatalog.jsonld.http.media.MediaType;
import com.apicatalog.jsonld.loader.DocumentLoader;
import com.apicatalog.jsonld.loader.DocumentLoaderOptions;
import com.apicatalog.jsonld.loader.FileLoader;
Expand All @@ -21,9 +19,11 @@

public class ConfigurableDocumentLoader implements DocumentLoader {

private HttpClient httpClient = DefaultHttpClient.defaultInstance();
private DocumentLoader httpLoader = new HttpLoader(this.httpClient);
private DocumentLoader fileLoader = new FileLoader();
private static DocumentLoader DEFAULT_HTTP_LOADER;
private static DocumentLoader DEFAULT_FILE_LOADER;

private DocumentLoader httpLoader;
private DocumentLoader fileLoader;

private boolean enableLocalCache = true;
private boolean enableHttp = false;
Expand All @@ -43,6 +43,24 @@ public class ConfigurableDocumentLoader implements DocumentLoader {
DOCUMENT_LOADER = new ConfigurableDocumentLoader();
}

public static DocumentLoader getDefaultHttpLoader() {
if (DEFAULT_HTTP_LOADER == null) DEFAULT_HTTP_LOADER = new HttpLoader(DefaultHttpClient.defaultInstance());
return DEFAULT_HTTP_LOADER;
}

public static DocumentLoader getDefaultFileLoader() {
if (DEFAULT_FILE_LOADER == null) DEFAULT_FILE_LOADER = new FileLoader();
return DEFAULT_FILE_LOADER;
}

public static void setDefaultHttpLoader(DocumentLoader defaultHttpLoader) {
DEFAULT_HTTP_LOADER = defaultHttpLoader;
}

public static void setDefaultFileLoader(DocumentLoader defaultFileLoader) {
DEFAULT_FILE_LOADER = defaultFileLoader;
}

public ConfigurableDocumentLoader() {

}
Expand All @@ -60,27 +78,33 @@ public Document loadDocument(URI url, DocumentLoaderOptions options) throws Json
}
if (this.isEnableHttp() && "http".equalsIgnoreCase(url.getScheme())) {
if (!this.getHttpContexts().isEmpty() && !this.getHttpContexts().contains(url)) return null;
DocumentLoader httpLoader = this.getHttpLoader();
if (httpLoader == null) httpLoader = getDefaultHttpLoader();
Document document = this.getRemoteCache() == null ? null : this.getRemoteCache().getIfPresent(url);
if (document == null) {
document = this.getHttpLoader().loadDocument(url, options);
document = httpLoader.loadDocument(url, options);
if (this.getRemoteCache() != null) this.getRemoteCache().put(url, document);
}
return document;
}
if (this.isEnableHttps() && "https".equalsIgnoreCase(url.getScheme())) {
if (!this.getHttpsContexts().isEmpty() && !this.getHttpsContexts().contains(url)) return null;
DocumentLoader httpLoader = this.getHttpLoader();
if (httpLoader == null) httpLoader = getDefaultHttpLoader();
Document document = this.getRemoteCache() == null ? null : this.getRemoteCache().getIfPresent(url);
if (document == null) {
document = this.getHttpLoader().loadDocument(url, options);
document = httpLoader.loadDocument(url, options);
if (this.getRemoteCache() != null) this.getRemoteCache().put(url, document);
}
return document;
}
if (this.isEnableFile() && "file".equalsIgnoreCase(url.getScheme())) {
if (!this.getFileContexts().isEmpty() && !this.getFileContexts().contains(url)) return null;
DocumentLoader fileLoader = this.getFileLoader();
if (fileLoader == null) fileLoader = getDefaultFileLoader();
Document document = this.getRemoteCache() == null ? null : this.getRemoteCache().getIfPresent(url);
if (document == null) {
document = this.getFileLoader().loadDocument(url, options);
document = fileLoader.loadDocument(url, options);
if (this.getRemoteCache() != null) this.getRemoteCache().put(url, document);
}
return document;
Expand All @@ -94,14 +118,6 @@ public Document loadDocument(URI url, DocumentLoaderOptions options) throws Json
* Getters and setters
*/

public HttpClient getHttpClient() {
return this.httpClient;
}

public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}

public DocumentLoader getHttpLoader() {
return this.httpLoader;
}
Expand Down