Switch to static fhirBuilder for all FhirRequestBuilders
+ Fix null user agent issue + Load ManagedWebAccess from FHIR settings + Add terminology servers to FhirSettings
This commit is contained in:
parent
e45c0b2bf0
commit
b6ef81c9f3
|
@ -119,7 +119,7 @@ public class ClientUtils {
|
||||||
private final TimeUnit timeoutUnit = TimeUnit.MILLISECONDS;
|
private final TimeUnit timeoutUnit = TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
return ManagedWebAccess.fhirBuilder().withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri, String resourceFormat,
|
public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri, String resourceFormat,
|
||||||
|
|
|
@ -115,7 +115,7 @@ public class FhirRequestBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
return ManagedWebAccess.fhirBuilder().withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||||
|
|
|
@ -125,7 +125,7 @@ public class FhirRequestBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
return ManagedWebAccess.fhirBuilder().withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||||
|
|
|
@ -127,7 +127,7 @@ public class FhirRequestBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
return ManagedWebAccess.fhirBuilder().withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||||
|
|
|
@ -113,59 +113,10 @@ public class FhirRequestBuilder {
|
||||||
|| issue.getSeverity() == OperationOutcome.IssueSeverity.FATAL));
|
|| issue.getSeverity() == OperationOutcome.IssueSeverity.FATAL));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
protected ManagedFhirWebAccessBuilder getManagedWebAccessBuilder() {
|
||||||
return new ManagedFhirWebAccessBuilder("hapi-fhir-tooling-client", null).withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
return ManagedWebAccess.fhirBuilder().withRetries(retryCount).withTimeout(timeout, timeoutUnit).withLogger(logger);
|
||||||
}
|
|
||||||
/**
|
|
||||||
* We only ever want to have one copy of the HttpClient kicking around at any given time. If we need to make changes
|
|
||||||
* to any configuration, such as proxy settings, timeout, caches, etc, we can do a per-call configuration through
|
|
||||||
* the {@link OkHttpClient#newBuilder()} method. That will return a builder that shares the same connection pool,
|
|
||||||
* dispatcher, and configuration with the original client.
|
|
||||||
* </p>
|
|
||||||
* The {@link OkHttpClient} uses the proxy auth properties set in the current system properties. The reason we don't
|
|
||||||
* set the proxy address and authentication explicitly, is due to the fact that this class is often used in conjunction
|
|
||||||
* with other http client tools which rely on the system.properties settings to determine proxy settings. It's easier
|
|
||||||
* to keep the method consistent across the board. ...for now.
|
|
||||||
*
|
|
||||||
* @return {@link OkHttpClient} instance
|
|
||||||
*/
|
|
||||||
/*FIXME delete after refactor
|
|
||||||
protected OkHttpClient getHttpClient() {
|
|
||||||
|
|
||||||
if (okHttpClient == null) {
|
|
||||||
okHttpClient = new OkHttpClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Authenticator proxyAuthenticator = getAuthenticator();
|
|
||||||
|
|
||||||
OkHttpClient.Builder builder = okHttpClient.newBuilder();
|
|
||||||
if (logger != null) builder.addInterceptor(logger);
|
|
||||||
builder.addInterceptor(new RetryInterceptor(retryCount));
|
|
||||||
return builder.connectTimeout(timeout, timeoutUnit)
|
|
||||||
.writeTimeout(timeout, timeoutUnit)
|
|
||||||
.readTimeout(timeout, timeoutUnit)
|
|
||||||
.proxyAuthenticator(proxyAuthenticator)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
/*FIXME delete after refactor
|
|
||||||
@Nonnull
|
|
||||||
private static Authenticator getAuthenticator() {
|
|
||||||
return (route, response) -> {
|
|
||||||
final String httpProxyUser = System.getProperty(HTTP_PROXY_USER);
|
|
||||||
final String httpProxyPass = System.getProperty(HTTP_PROXY_PASS);
|
|
||||||
if (httpProxyUser != null && httpProxyPass != null) {
|
|
||||||
String credential = Credentials.basic(httpProxyUser, httpProxyPass);
|
|
||||||
return response.request().newBuilder()
|
|
||||||
.header(HEADER_PROXY_AUTH, credential)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
return response.request().newBuilder().build();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
public FhirRequestBuilder withResourceFormat(String resourceFormat) {
|
||||||
this.resourceFormat = resourceFormat;
|
this.resourceFormat = resourceFormat;
|
||||||
return this;
|
return this;
|
||||||
|
@ -197,8 +148,6 @@ public class FhirRequestBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
|
public <T extends Resource> ResourceRequest<T> execute() throws IOException {
|
||||||
HTTPRequest requestWithHeaders = formatHeaders(httpRequest, resourceFormat, headers);
|
HTTPRequest requestWithHeaders = formatHeaders(httpRequest, resourceFormat, headers);
|
||||||
HTTPResult response = getManagedWebAccessBuilder().httpCall(requestWithHeaders);//getHttpClient().newCall(httpRequest.build()).execute();
|
HTTPResult response = getManagedWebAccessBuilder().httpCall(requestWithHeaders);//getHttpClient().newCall(httpRequest.build()).execute();
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package org.hl7.fhir.r5.utils.client;
|
||||||
|
|
||||||
|
import org.hl7.fhir.utilities.http.ManagedWebAccess;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
public class ManagedWebAccessAuthTests {
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
ManagedWebAccess.setUserAgent("hapi-fhir-testing-client");
|
||||||
|
}
|
||||||
|
}
|
|
@ -50,7 +50,8 @@ public class ManagedFhirWebAccessBuilder extends ManagedWebAccessBuilderBase<Man
|
||||||
|
|
||||||
protected HTTPRequest httpRequestWithDefaultHeaders(HTTPRequest request) {
|
protected HTTPRequest httpRequestWithDefaultHeaders(HTTPRequest request) {
|
||||||
List<HTTPHeader> headers = new ArrayList<>();
|
List<HTTPHeader> headers = new ArrayList<>();
|
||||||
if (HTTPHeaderUtil.getSingleHeader(request.getHeaders(), HTTPHeaderUtil.USER_AGENT) == null) {
|
if (HTTPHeaderUtil.getSingleHeader(request.getHeaders(), HTTPHeaderUtil.USER_AGENT) == null
|
||||||
|
&& getUserAgent() != null) {
|
||||||
headers.add(new HTTPHeader(HTTPHeaderUtil.USER_AGENT, getUserAgent()));
|
headers.add(new HTTPHeader(HTTPHeaderUtil.USER_AGENT, getUserAgent()));
|
||||||
}
|
}
|
||||||
request.getHeaders().forEach(headers::add);
|
request.getHeaders().forEach(headers::add);
|
||||||
|
|
|
@ -32,16 +32,15 @@ package org.hl7.fhir.utilities.http;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import org.hl7.fhir.utilities.settings.FhirSettings;
|
||||||
import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
|
import org.hl7.fhir.utilities.settings.ServerDetailsPOJO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* see security.md - manages access to the local file system by the FHIR HAPI Core library
|
* see security.md - manages web access by the FHIR HAPI Core library
|
||||||
* <p/>
|
* <p/>
|
||||||
* By using accessPolicy, allowedDomains and accessor, a host java application can control
|
* By using accessPolicy, allowedDomains and accessor, a host java application can control
|
||||||
* whether this library has direct access to the web (and which domains it is allowed to access),
|
* whether this library has direct access to the web (and which domains it is allowed to access),
|
||||||
|
@ -77,6 +76,8 @@ public class ManagedWebAccess {
|
||||||
@Getter
|
@Getter
|
||||||
private static IFhirWebAccessor fhirWebAccessor;
|
private static IFhirWebAccessor fhirWebAccessor;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private static String userAgent;
|
private static String userAgent;
|
||||||
private static List<ServerDetailsPOJO> serverAuthDetails;
|
private static List<ServerDetailsPOJO> serverAuthDetails;
|
||||||
|
@ -133,4 +134,19 @@ public class ManagedWebAccess {
|
||||||
return fhirBuilder().httpCall(httpRequest);
|
return fhirBuilder().httpCall(httpRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void loadFromFHIRSettings() {
|
||||||
|
setAccessPolicy(FhirSettings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT);
|
||||||
|
setUserAgent("hapi-fhir-tooling-client");
|
||||||
|
serverAuthDetails = new ArrayList<>();
|
||||||
|
serverAuthDetails.addAll(FhirSettings.getPackageServers());
|
||||||
|
serverAuthDetails.addAll(FhirSettings.getTerminologyServers());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadFromFHIRSettings(FhirSettings settings) {
|
||||||
|
setAccessPolicy(settings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT);
|
||||||
|
setUserAgent("hapi-fhir-tooling-client");
|
||||||
|
serverAuthDetails = new ArrayList<>();
|
||||||
|
serverAuthDetails.addAll(settings.getPackageServers());
|
||||||
|
serverAuthDetails.addAll(settings.getTerminologyServers());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -4,6 +4,7 @@ import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -232,6 +233,14 @@ public class FhirSettings {
|
||||||
if (instance.fhirSettings.getPackageManagement() == null) {
|
if (instance.fhirSettings.getPackageManagement() == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
return List.of(instance.fhirSettings.getPackageManagement().getServers().toArray(new ServerDetailsPOJO[]{}));
|
return Arrays.asList(instance.fhirSettings.getPackageManagement().getServers().toArray(new ServerDetailsPOJO[]{}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<ServerDetailsPOJO> getTerminologyServers() {
|
||||||
|
getInstance();
|
||||||
|
if (instance.fhirSettings.getTerminologyServers() == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return Arrays.asList(instance.fhirSettings.getTerminologyServers().getServers().toArray(new ServerDetailsPOJO[]{}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,6 +71,7 @@ import org.hl7.fhir.utilities.SystemExitManager;
|
||||||
import org.hl7.fhir.utilities.TimeTracker;
|
import org.hl7.fhir.utilities.TimeTracker;
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
import org.hl7.fhir.utilities.VersionUtilities;
|
import org.hl7.fhir.utilities.VersionUtilities;
|
||||||
|
import org.hl7.fhir.utilities.http.ManagedWebAccess;
|
||||||
import org.hl7.fhir.utilities.settings.FhirSettings;
|
import org.hl7.fhir.utilities.settings.FhirSettings;
|
||||||
import org.hl7.fhir.validation.cli.model.CliContext;
|
import org.hl7.fhir.validation.cli.model.CliContext;
|
||||||
import org.hl7.fhir.validation.cli.services.ValidationService;
|
import org.hl7.fhir.validation.cli.services.ValidationService;
|
||||||
|
@ -151,6 +152,7 @@ public class ValidatorCli {
|
||||||
if (cliContext.getFhirSettingsFile() != null) {
|
if (cliContext.getFhirSettingsFile() != null) {
|
||||||
FhirSettings.setExplicitFilePath(cliContext.getFhirSettingsFile());
|
FhirSettings.setExplicitFilePath(cliContext.getFhirSettingsFile());
|
||||||
}
|
}
|
||||||
|
ManagedWebAccess.loadFromFHIRSettings();
|
||||||
|
|
||||||
FileFormat.checkCharsetAndWarnIfNotUTF8(System.out);
|
FileFormat.checkCharsetAndWarnIfNotUTF8(System.out);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import org.hl7.fhir.utilities.http.ManagedWebAccess;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
public class ManagedWebAccessAuthTests {
|
||||||
|
@BeforeClass
|
||||||
|
public static void setUp() {
|
||||||
|
ManagedWebAccess.setUserAgent("hapi-fhir-testing-client");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue