rework HTTP client - all client calls route through SimpleHTTPClient + define CommonPackages infrastructure

This commit is contained in:
Grahame Grieve 2021-11-09 11:38:25 +11:00
parent d91b26e69b
commit 1aae6c8c76
17 changed files with 381 additions and 91 deletions

View File

@ -1,5 +1,8 @@
package org.hl7.fhir.convertors.misc;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
@ -38,10 +41,10 @@ import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -121,13 +124,10 @@ public class CKMImporter {
}
private Document loadXml(String address) throws Exception {
URL url = new URL(address);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(address, "application/xml");
res.checkThrowException();
InputStream xml = new ByteArrayInputStream(res.getContent());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

View File

@ -17,11 +17,13 @@ import org.hl7.fhir.r4.terminologies.CodeSystemUtilities;
import org.hl7.fhir.r4.utils.ToolingExtensions;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@ -388,12 +390,12 @@ public class ICD11Generator {
private JsonObject fetchJson(String source) throws IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
c.addRequestProperty("Accept", "application/json");
c.addRequestProperty("API-Version", "v2");
c.addRequestProperty("Accept-Language", "en");
return (JsonObject) new com.google.gson.JsonParser().parse(TextFile.streamToString(c.getInputStream()));
SimpleHTTPClient http = new SimpleHTTPClient();
http.addHeader("API-Version", "v2");
http.addHeader("Accept-Language", "en");
HTTPResult res = http.get(source, "application/json");
res.checkThrowException();
return JsonTrackingParser.parseJson(res.getContent());
}

View File

@ -34,7 +34,6 @@ package org.hl7.fhir.r4.conformance;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -70,6 +69,8 @@ import org.hl7.fhir.r4.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
import org.hl7.fhir.r4.utils.DefinitionNavigator;
import org.hl7.fhir.r4.utils.ToolingExtensions;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.validation.ValidationMessage;
@ -1283,9 +1284,10 @@ public class ProfileComparer {
File f = new File(local);
if (f.exists())
return TextFile.fileToString(f);
URL url = new URL(source);
URLConnection c = url.openConnection();
String result = TextFile.streamToString(c.getInputStream());
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source);
res.checkThrowException();
String result = TextFile.bytesToString(res.getContent());
TextFile.stringToFile(result, f);
return result;
}

View File

@ -0,0 +1,201 @@
package org.hl7.fhir.utilities;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.SimpleHTTPClient.Header;
import org.hl7.fhir.utilities.npm.SSLCertTruster;
public class SimpleHTTPClient {
public class Header {
private String name;
private String value;
public Header(String name, String value) {
super();
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}
private static final int MAX_REDIRECTS = 5;
public class HTTPResult {
private int code;
private String contentType;
private byte[] content;
private String source;
public HTTPResult(String source, int code, String contentType, byte[] content) {
super();
this.source = source;
this.code = code;
this.contentType = contentType;
this.content = content;
}
public int getCode() {
return code;
}
public String getContentType() {
return contentType;
}
public byte[] getContent() {
return content;
}
public String getSource() {
return source;
}
public void checkThrowException() throws IOException {
if (code >= 300) {
throw new IOException("Invalid HTTP response "+code+" from "+source);
}
}
}
private List<Header> headers = new ArrayList<>();
private String username;
private String password;
public void addHeader(String name, String value) {
headers.add(new Header(name, value));
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private boolean trustAll = false;
public void trustAllhosts() {
trustAll = true;
SSLCertTruster.trustAllHosts();
}
public HTTPResult get(String url) throws IOException {
return get(url, null);
}
public HTTPResult get(String url, String accept) throws IOException {
URL u = new URL(url);
boolean isSSL = url.startsWith("https://");
// handling redirects - setInstanceFollowRedirects(true) doesn't handle crossing http to https
Map<String, Integer> visited = new HashMap<>();
HttpURLConnection c = null;
boolean done = false;
while (!done) {
int times = visited.compute(url, (key, count) -> count == null ? 1 : count + 1);
if (times > MAX_REDIRECTS)
throw new IOException("Stuck in redirect loop");
u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Accept", accept);
setHeaders(c);
c.setInstanceFollowRedirects(false);
if (trustAll && url.startsWith("https://")) {
((javax.net.ssl.HttpsURLConnection) c).setHostnameVerifier(SSLCertTruster.DO_NOT_VERIFY);
}
switch (c.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
String location = c.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
URL base = new URL(url);
URL next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
continue;
default:
done = true;
}
}
return new HTTPResult(url, c.getResponseCode(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getInputStream()));
}
private void setHeaders(HttpURLConnection c) {
for (Header h : headers) {
c.setRequestProperty(h.getName(), h.getValue());
}
c.setConnectTimeout(15000);
c.setReadTimeout(15000);
if (username != null) {
String auth = username+":"+password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
c.setRequestProperty("Authorization", authHeaderValue);
}
}
public HTTPResult post(String url, String contentType, byte[] content, String accept) throws IOException {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setRequestMethod("POST");
c.setRequestProperty("Content-type", contentType);
if (accept != null) {
c.setRequestProperty("Accept", accept);
}
setHeaders(c);
c.getOutputStream().write(content);
c.getOutputStream().close();
return new HTTPResult(url, c.getResponseCode(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getInputStream()));
}
public HTTPResult put(String url, String contentType, byte[] content, String accept) throws IOException {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setRequestMethod("PUT");
c.setRequestProperty("Content-type", contentType);
if (accept != null) {
c.setRequestProperty("Accept", accept);
}
setHeaders(c);
c.getOutputStream().write(content);
c.getOutputStream().close();
return new HTTPResult(url, c.getResponseCode(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getInputStream()));
}
}

View File

@ -1,9 +1,7 @@
package org.hl7.fhir.utilities.json;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
@ -134,12 +132,6 @@ public class JSONUtil {
}
}
public static JsonObject fetchJson(String source) throws IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
return (JsonObject) new com.google.gson.JsonParser().parse(TextFile.streamToString(c.getInputStream()));
}
public static String type(JsonElement e) {
if (e == null) {
return "(null)";

View File

@ -36,12 +36,12 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Stack;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -663,7 +663,7 @@ public class JsonTrackingParser {
String jcnt = gson.toJson(json);
TextFile.stringToFile(jcnt, file);
}
public static void write(JsonObject json, String fileName) throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jcnt = gson.toJson(json);
@ -675,6 +675,11 @@ public class JsonTrackingParser {
return gson.toJson(json);
}
public static String writeDense(JsonObject json) {
Gson gson = new GsonBuilder().create();
return gson.toJson(json);
}
public static byte[] writeBytes(JsonObject json, boolean pretty) {
if (pretty) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
@ -686,10 +691,10 @@ public class JsonTrackingParser {
}
public static JsonObject fetchJson(String source) throws IOException {
URL url = new URL(source+"?nocache=" + System.currentTimeMillis());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setInstanceFollowRedirects(true);
return parseJson(c.getInputStream());
SimpleHTTPClient fetcher = new SimpleHTTPClient();
HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return parseJson(res.getContent());
}

View File

@ -0,0 +1,11 @@
package org.hl7.fhir.utilities.npm;
public class CommonPackages {
public static final String ID_XVER = "hl7.fhir.xver-extensions";
public static final String VER_XVER = "0.0.7";
public static final String ID_PUBPACK = "hl7.fhir.pubpack";
public static final String VER_PUBPACK = "0.0.9";
}

View File

@ -36,6 +36,8 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.IniFile;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -47,6 +49,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -54,7 +58,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.sql.Timestamp;
@ -146,12 +149,6 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
return pi;
}
private JsonObject fetchJson(String source) throws IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
return (JsonObject) new com.google.gson.JsonParser().parse(TextFile.streamToString(c.getInputStream()));
}
private void clearCache() throws IOException {
for (File f : new File(cacheFolder).listFiles()) {
if (f.isDirectory()) {
@ -229,7 +226,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
public String getLatestVersion(String id) throws IOException {
for (String nextPackageServer : getPackageServers()) {
// special case:
if (!("hl7.fhir.pubpack".equals(id) && PRIMARY_SERVER.equals(nextPackageServer))) {
if (!(CommonPackages.ID_PUBPACK.equals(id) && PRIMARY_SERVER.equals(nextPackageServer))) {
CachingPackageClient pc = new CachingPackageClient(nextPackageServer);
try {
return pc.getLatestVersion(id);
@ -517,9 +514,10 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException {
try {
URL url = new URL(source);
URLConnection c = url.openConnection();
return c.getInputStream();
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source);
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
} catch (Exception e) {
if (optional)
return null;
@ -627,7 +625,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
// special case: current versions roll over, and we have to check their currency
try {
String url = ciList.get(id);
JsonObject json = fetchJson(Utilities.pathURL(url, "package.manifest.json"));
JsonObject json = JsonTrackingParser.fetchJson(Utilities.pathURL(url, "package.manifest.json"));
String currDate = JSONUtil.str(json, "date");
String packDate = p.date();
if (!currDate.equals(packDate))
@ -651,13 +649,11 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
private void loadFromBuildServer() throws IOException {
URL url = new URL("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
SSLCertTruster.trustAllHosts();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setHostnameVerifier(SSLCertTruster.DO_NOT_VERIFY);
connection.setRequestMethod("GET");
InputStream json = connection.getInputStream();
buildInfo = (JsonArray) new com.google.gson.JsonParser().parse(TextFile.streamToString(json));
SimpleHTTPClient http = new SimpleHTTPClient();
http.trustAllhosts();
HTTPResult res = http.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
res.checkThrowException();
buildInfo = (JsonArray) new com.google.gson.JsonParser().parse(TextFile.bytesToString(res.getContent()));
List<BuildRecord> builds = new ArrayList<>();
@ -714,7 +710,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
String aurl = pu;
JsonObject json;
try {
json = fetchJson(pu);
json = JsonTrackingParser.fetchJson(pu);
} catch (Exception e) {
String pv = Utilities.pathURL(url, v, "package.tgz");
try {
@ -755,7 +751,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
throw new FHIRException("Unable to resolve package id " + id);
}
String pu = Utilities.pathURL(url, "package-list.json");
JsonObject json = fetchJson(pu);
JsonObject json = JsonTrackingParser.fetchJson(pu);
if (!id.equals(JSONUtil.str(json, "package-id")))
throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + JSONUtil.str(json, "package-id"));
for (JsonElement e : json.getAsJsonArray("list")) {
@ -769,7 +765,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
private String getUrlForPackage(String id) {
if ("hl7.fhir.xver-extensions".equals(id)) {
if (CommonPackages.ID_XVER.equals(id)) {
return "http://fhir.org/packages/hl7.fhir.xver-extensions";
}
return null;

View File

@ -4,17 +4,19 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.JSONUtil;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@ -135,12 +137,10 @@ public class PackageClient {
}
private InputStream fetchUrl(String source, String accept) throws IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
if (accept != null) {
c.setRequestProperty("accept", accept);
}
return c.getInputStream();
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source, accept);
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
}
private JsonObject fetchJson(String source) throws IOException {

View File

@ -0,0 +1,75 @@
package org.hl7.fhir.utilities.npm;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.json.JSONUtil;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import com.google.gson.JsonObject;
public class PackageScanner {
public static void main(String[] args) throws IOException {
List<String> output = new ArrayList<>();
Set<String> packages = new HashSet<>();
processServer("http://packages.fhir.org", output, packages);
processServer("http://packages2.fhir.org/packages", output, packages);
StringBuilder b = new StringBuilder();
for (String s : output) {
b.append(s);
b.append("\r\n");
System.out.println(s);
}
TextFile.stringToFile(b.toString(), "c:\\temp\\packages.csv");
}
public static void processServer(String server, List<String> output, Set<String> packages) throws IOException {
System.out.println("Server: "+server);
PackageClient client = new PackageClient(server);
List<PackageInfo> list = client.search(null, null, null, false);
output.add("id\tversion\tcanonica\tfhir version\tfhir-versions\tkind\ttype\tsource");
for (PackageInfo pi : list) {
System.out.print(" fetch: "+pi.getId());
List<PackageInfo> versions = null;
while (versions == null) {
System.out.print("-");
try {
versions = client.getVersions(pi.getId());
} catch (Exception e) {
// nothing
}
}
for (PackageInfo piv : versions) {
if (!packages.contains(pi.getId()+"#"+piv.getVersion())) {
packages.add(pi.getId()+"#"+piv.getVersion());
try {
System.out.print(".");
InputStream cnt = client.fetch(pi.getId(), piv.getVersion());
NpmPackage pck = NpmPackage.fromPackage(cnt);
JsonObject json = pck.getNpm();
String fv;
try {
fv = pck.fhirVersion();
} catch (Exception e) {
fv = "--";
}
output.add(pck.name()+"\t"+pck.version()+"\t"+pck.canonical()+"\t"+fv+'\t'+pck.fhirVersionList()+'\t'+JSONUtil.str(json, "kind")+'\t'+JSONUtil.str(json, "type")+'\t'+JsonTrackingParser.writeDense(json)); } catch (Exception e) {
System.out.println("Error acessing "+pi.getId()+"#"+piv.getVersion()+": "+e.getMessage());
e.printStackTrace();
}
}
}
System.out.println("!");
}
}
}

View File

@ -13,7 +13,7 @@ import java.security.cert.X509Certificate;
public class SSLCertTruster {
// always verify the host - dont check for certificate
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}

View File

@ -18,6 +18,8 @@ import org.hl7.fhir.r5.model.Constants;
import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.IniFile;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -33,9 +35,7 @@ import org.hl7.fhir.validation.cli.utils.VersionSourceInformation;
import org.w3c.dom.Document;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -273,9 +273,10 @@ public class IgLoader {
private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException, IOException {
try {
URL url = new URL(source + "?nocache=" + System.currentTimeMillis());
URLConnection c = url.openConnection();
return c.getInputStream();
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return new ByteArrayInputStream(res.getContent());
} catch (IOException e) {
if (optional)
return null;
@ -412,17 +413,16 @@ public class IgLoader {
private byte[] fetchFromUrlSpecific(String source, String contentType, boolean optional, List<String> errors) throws FHIRException, IOException {
try {
SimpleHTTPClient http = new SimpleHTTPClient();
try {
// try with cache-busting option and then try withhout in case the server doesn't support that
URL url = new URL(source + "?nocache=" + System.currentTimeMillis());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", contentType);
return TextFile.streamToBytes(conn.getInputStream());
HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis(), contentType);
res.checkThrowException();
return res.getContent();
} catch (Exception e) {
URL url = new URL(source);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", contentType);
return TextFile.streamToBytes(conn.getInputStream());
HTTPResult res = http.get(source, contentType);
res.checkThrowException();
return res.getContent();
}
} catch (IOException e) {
if (errors != null) {

View File

@ -63,6 +63,7 @@ import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ComparisonService;
import org.hl7.fhir.validation.cli.services.ValidationService;
@ -222,7 +223,7 @@ public class ValidatorCli {
String v = VersionUtilities.getCurrentVersion(cliContext.getSv());
String definitions = VersionUtilities.packageForVersion(v) + "#" + v;
ValidationEngine validator = validationService.initializeValidator(cliContext, definitions, tt);
validator.loadPackage("hl7.fhir.pubpack", null);
validator.loadPackage(CommonPackages.ID_PUBPACK, null);
ComparisonService.doLeftRightComparison(args, Params.getParam(args, Params.DESTINATION), validator);
}

View File

@ -2,13 +2,14 @@ package org.hl7.fhir.validation.cli.utils;
import org.apache.commons.io.IOUtils;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class ProfileLoader {
public static byte[] loadProfileSource(String src) throws FHIRException, IOException {
@ -25,10 +26,10 @@ public class ProfileLoader {
private static byte[] loadProfileFromUrl(String src) throws FHIRException {
try {
URL url = new URL(src + "?nocache=" + System.currentTimeMillis());
URLConnection c = url.openConnection();
return IOUtils.toByteArray(c.getInputStream());
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(src + "?nocache=" + System.currentTimeMillis());
res.checkThrowException();
return res.getContent();
} catch (Exception e) {
throw new FHIRException("Unable to find definitions at URL '" + src + "': " + e.getMessage(), e);
}

View File

@ -52,6 +52,7 @@ import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.npm.ToolsVersion;
@ -124,7 +125,7 @@ public class ComparisonTests {
System.out.println("---- Set up Output ----------------------------------------------------------");
Utilities.createDirectory(Utilities.path("[tmp]", "comparison"));
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
NpmPackage npm = pcm.loadPackage("hl7.fhir.pubpack", "0.0.9");
NpmPackage npm = pcm.loadPackage(CommonPackages.ID_PUBPACK, CommonPackages.VER_PUBPACK);
for (String f : npm.list("other")) {
TextFile.streamToFile(npm.load("other", f), Utilities.path("[tmp]", "comparison", f));
}

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.validation.tests;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.junit.jupiter.api.Test;
public class ProfileComparisonTests {
@ -13,7 +14,7 @@ public class ProfileComparisonTests {
// ValidationEngine ve = new ValidationEngine("hl7.fhir.r3.core#3.0.2", DEF_TX, null, FhirPublication.STU3, "3.0.2");
// ve.loadIg("hl7.fhir.us.core#1.0.1", false);
// ve.loadIg("hl7.fhir.au.base#current", false);
// ve.getContext().loadFromPackage(new PackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage("hl7.fhir.pubpack", "0.0.9"), new R5ToR5Loader(new String[] {"Binary"}), "Binary");
// ve.getContext().loadFromPackage(new PackageCacheManager(true, ToolsVersion.TOOLS_VERSION).loadPackage(CommonPackages.ID_PUBPACK, CommonPackages.VER_PUBPACK), new R5ToR5Loader(new String[] {"Binary"}), "Binary");
//
//
// String left = "http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient";

View File

@ -6,7 +6,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
@ -54,6 +53,8 @@ import org.hl7.fhir.r5.utils.IResourceValidator.BestPracticeWarningLevel;
import org.hl7.fhir.r5.utils.IResourceValidator.BundleValidationRule;
import org.hl7.fhir.r5.utils.IResourceValidator.IValidatorResourceFetcher;
import org.hl7.fhir.r5.utils.IResourceValidator.ReferenceValidationPolicy;
import org.hl7.fhir.utilities.SimpleHTTPClient;
import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
@ -554,9 +555,10 @@ public class ValidationTests implements IEvaluationContext, IValidatorResourceFe
@Override
public byte[] fetchRaw(IResourceValidator validator, String source) throws MalformedURLException, IOException {
URL url = new URL(source);
URLConnection c = url.openConnection();
return TextFile.streamToBytes(c.getInputStream());
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source);
res.checkThrowException();
return res.getContent();
}
@Override