work on no-network-access mode

This commit is contained in:
Grahame Grieve 2023-04-18 09:39:59 +08:00
parent 6644d9bd6d
commit 81d26c5160
8 changed files with 128 additions and 12 deletions

View File

@ -85,6 +85,8 @@ import org.hl7.fhir.dstu2.model.OperationOutcome.OperationOutcomeIssueComponent;
import org.hl7.fhir.dstu2.model.Resource;
import org.hl7.fhir.dstu2.model.ResourceType;
import org.hl7.fhir.dstu2.utils.ResourceUtilities;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.ToolGlobalSettings;
import org.hl7.fhir.utilities.ToolingClientLogger;
import org.hl7.fhir.utilities.Utilities;
@ -140,26 +142,42 @@ public class ClientUtils {
}
public <T extends Resource> ResourceRequest<T> issueOptionsRequest(URI optionsUri, String resourceFormat, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpOptions options = new HttpOptions(optionsUri);
return issueResourceRequest(resourceFormat, options, timeoutLoading);
}
public <T extends Resource> ResourceRequest<T> issueGetResourceRequest(URI resourceUri, String resourceFormat, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpGet httpget = new HttpGet(resourceUri);
return issueResourceRequest(resourceFormat, httpget, timeoutLoading);
}
public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat, List<Header> headers, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpPut httpPut = new HttpPut(resourceUri);
return issueResourceRequest(resourceFormat, httpPut, payload, headers, timeoutLoading);
}
public <T extends Resource> ResourceRequest<T> issuePutRequest(URI resourceUri, byte[] payload, String resourceFormat, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpPut httpPut = new HttpPut(resourceUri);
return issueResourceRequest(resourceFormat, httpPut, payload, null, timeoutLoading);
}
public <T extends Resource> ResourceRequest<T> issuePostRequest(URI resourceUri, byte[] payload, String resourceFormat, List<Header> headers, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpPost httpPost = new HttpPost(resourceUri);
return issueResourceRequest(resourceFormat, httpPost, payload, headers, timeoutLoading);
}
@ -170,6 +188,9 @@ public class ClientUtils {
}
public Bundle issueGetFeedRequest(URI resourceUri, String resourceFormat) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpGet httpget = new HttpGet(resourceUri);
configureFhirRequest(httpget, resourceFormat);
HttpResponse response = sendRequest(httpget);
@ -188,6 +209,9 @@ public class ClientUtils {
}
public Bundle postBatchRequest(URI resourceUri, byte[] payload, String resourceFormat, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpPost httpPost = new HttpPost(resourceUri);
configureFhirRequest(httpPost, resourceFormat);
HttpResponse response = sendPayload(httpPost, payload, proxy, timeoutLoading);
@ -195,6 +219,9 @@ public class ClientUtils {
}
public boolean issueDeleteRequest(URI resourceUri) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpDelete deleteRequest = new HttpDelete(resourceUri);
HttpResponse response = sendRequest(deleteRequest);
int responseStatusCode = response.getStatusLine().getStatusCode();
@ -228,6 +255,9 @@ public class ClientUtils {
* @return
*/
protected <T extends Resource> ResourceRequest<T> issueResourceRequest(String resourceFormat, HttpUriRequest request, byte[] payload, List<Header> headers, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
configureFhirRequest(request, resourceFormat, headers);
HttpResponse response = null;
if(request instanceof HttpEntityEnclosingRequest && payload != null) {
@ -285,6 +315,9 @@ public class ClientUtils {
*/
@SuppressWarnings({ "resource", "deprecation" })
protected HttpResponse sendPayload(HttpEntityEnclosingRequestBase request, byte[] payload, HttpHost proxy, int timeoutLoading) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpResponse response = null;
boolean ok = false;
long t = System.currentTimeMillis();
@ -309,7 +342,7 @@ public class ClientUtils {
if (tryCount <= retryCount || (tryCount < 3 && ioe instanceof org.apache.http.conn.ConnectTimeoutException)) {
ok = false;
} else {
throw new EFhirClientException("Error sending HTTP Post/Put Payload: "+ioe.getMessage(), ioe);
throw new EFhirClientException("Error sending HTTP Post/Put Payload to "+"??"+": "+ioe.getMessage(), ioe);
}
}
}
@ -323,6 +356,9 @@ public class ClientUtils {
* @return
*/
protected HttpResponse sendRequest(HttpUriRequest request) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
HttpResponse response = null;
try {
HttpClient httpclient = new DefaultHttpClient();
@ -430,6 +466,10 @@ public class ClientUtils {
* ***************************************************************/
public HttpURLConnection buildConnection(URI baseServiceUri, String tail) {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
try {
HttpURLConnection client = (HttpURLConnection) baseServiceUri.resolve(tail).toURL().openConnection();
return client;

View File

@ -11,6 +11,8 @@ import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.utils.ResourceUtilities;
import org.hl7.fhir.dstu3.utils.client.EFhirClientException;
import org.hl7.fhir.dstu3.utils.client.ResourceFormat;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.ToolGlobalSettings;
import org.hl7.fhir.utilities.ToolingClientLogger;
import javax.annotation.Nonnull;
@ -150,6 +152,10 @@ public class FhirRequestBuilder {
* @return {@link OkHttpClient} instance
*/
protected OkHttpClient getHttpClient() {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
}

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.r4.utils.client.network;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.formats.IParser;
import org.hl7.fhir.r4.formats.JsonParser;
import org.hl7.fhir.r4.formats.XmlParser;
@ -11,6 +12,7 @@ import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.utils.ResourceUtilities;
import org.hl7.fhir.r4.utils.client.EFhirClientException;
import org.hl7.fhir.r4.utils.client.ResourceFormat;
import org.hl7.fhir.utilities.ToolGlobalSettings;
import javax.annotation.Nonnull;
import java.io.IOException;
@ -148,6 +150,10 @@ public class FhirRequestBuilder {
* @return {@link OkHttpClient} instance
*/
protected OkHttpClient getHttpClient() {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
}

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.r5.utils.client.network;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.formats.IParser;
import org.hl7.fhir.r5.formats.JsonParser;
import org.hl7.fhir.r5.formats.XmlParser;
@ -11,6 +12,7 @@ import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.utils.ResourceUtilities;
import org.hl7.fhir.r5.utils.client.EFhirClientException;
import org.hl7.fhir.r5.utils.client.ResourceFormat;
import org.hl7.fhir.utilities.ToolGlobalSettings;
import javax.annotation.Nonnull;
import java.io.IOException;
@ -148,6 +150,10 @@ public class FhirRequestBuilder {
* @return {@link OkHttpClient} instance
*/
protected OkHttpClient getHttpClient() {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
}

View File

@ -100,6 +100,10 @@ public class FTPClient {
* Connect to the server, throw an exception if it fails
*/
public void connect() throws IOException {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
if (port != -1) {
logger.debug("Connecting to : " + server + ":" + port);
clientImpl.connect(server, port);

View File

@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.SimpleHTTPClient.Header;
import org.hl7.fhir.utilities.npm.SSLCertTruster;
@ -122,6 +123,10 @@ public class SimpleHTTPClient {
}
public HTTPResult get(String url, String accept) throws IOException {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
URL u = new URL(url);
// boolean isSSL = url.startsWith("https://");
@ -180,6 +185,9 @@ public class SimpleHTTPClient {
}
public HTTPResult post(String url, String contentType, byte[] content, String accept) throws IOException {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);
@ -197,6 +205,9 @@ public class SimpleHTTPClient {
public HTTPResult put(String url, String contentType, byte[] content, String accept) throws IOException {
if (ToolGlobalSettings.isNoNetwork()) {
throw new FHIRException("Network Access is prohibited in this context");
}
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);

View File

@ -4,6 +4,7 @@ import java.io.IOException;
public class ToolGlobalSettings {
private static Boolean noNetwork = null;
private static boolean inited = false;
private static String npmPath;
@ -67,6 +68,17 @@ public class ToolGlobalSettings {
return testIgsPath != null;
}
public static boolean isNoNetwork() {
init();
return noNetwork;
}
public static void setNoNetwork(boolean value) {
init();
noNetwork = value;
}
private static void init() {
if (!inited) {
inited = true;
@ -80,6 +92,7 @@ public class ToolGlobalSettings {
comparePath = ini.getStringProperty("paths", "compare");
tempPath = ini.getStringProperty("paths", "temp");
testIgsPath = ini.getStringProperty("paths", "test-igs");
noNetwork = ini.getBooleanProperty("network", "no-access");
}
} catch (IOException e) {
}

View File

@ -269,8 +269,32 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
}
}
try {
return fetchVersionTheOldWay(id);
} catch (Exception e) {
ourLog.info("Failed to determine latest version of package {} from server: {}", id, "build.fhir.org");
}
// still here? use the latest version we previously found or at least, is in the cache
return fetchVersionTheOldWay(id);
String version = getLatestVersionFromCache(id);
if (version != null) {
return version;
}
throw new FHIRException("Unable to find the last version for package "+id+": no local copy, and no network access");
}
public String getLatestVersionFromCache(String id) throws IOException {
for (String f : reverseSorted(new File(cacheFolder).list())) {
File cf = new File(Utilities.path(cacheFolder, f));
if (cf.isDirectory()) {
if (f.startsWith(id + "#")) {
String ver = f.substring(f.indexOf("#")+1);
ourLog.info("Latest version of package {} found locally is {} - using that", id, ver);
return ver;
}
}
}
return null;
}
private NpmPackage loadPackageFromFile(String id, String folder) throws IOException {
@ -685,20 +709,26 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
return null; // nup, we need a new copy
}
} catch (Exception e) {
log("Unable to check package currency: "+id+": "+id);
}
return p;
}
private boolean checkBuildLoaded() {
if (buildLoaded)
return true;
try {
loadFromBuildServer();
} catch (Exception e) {
log("Error connecting to build server - running without build (" + e.getMessage() + ")");
e.printStackTrace();
private void checkBuildLoaded() {
if (!buildLoaded) {
try {
loadFromBuildServer();
} catch (Exception e) {
try {
// we always pause a second and try again - the most common reason to be here is that the file was being changed on the server
Thread.sleep(1000);
loadFromBuildServer();
} catch (Exception e2) {
log("Error connecting to build server - running without build (" + e2.getMessage() + ")");
// e.printStackTrace();
}
}
}
return false;
}
private void loadFromBuildServer() throws IOException {
@ -726,7 +756,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
ciList.put(bld.getPackageId(), "https://build.fhir.org/ig/" + bld.getRepo());
}
}
buildLoaded = true; // whether it succeeds or not
buildLoaded = true;
}
private String getRepo(String path) {