From a510a6d1aaae9c0e0c89e7ec96c93438010973f6 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Thu, 16 May 2024 13:34:50 +1000 Subject: [PATCH] introduce ManagedWebAccess --- .../convertors/analytics/PackageVisitor.java | 12 +- .../hl7/fhir/convertors/misc/CKMImporter.java | 8 +- .../fhir/convertors/misc/ICD11Generator.java | 4 +- .../fhir/r4/conformance/ProfileComparer.java | 8 +- .../TerminologyCacheManager.java | 8 +- .../hl7/fhir/r5/elementmodel/SHLParser.java | 13 +- .../TerminologyCacheManager.java | 8 +- .../hl7/fhir/utilities/http/HTTPResult.java | 63 +++++++ .../fhir/utilities/http/ManagedWebAccess.java | 164 ++++++++++++++++++ .../{ => http}/SimpleHTTPClient.java | 62 +------ .../utilities/json/JsonTrackingParser.java | 11 +- .../utilities/json/parser/JsonParser.java | 9 +- .../npm/FilesystemPackageCacheManager.java | 11 +- .../hl7/fhir/utilities/npm/NpmPackage.java | 8 +- .../hl7/fhir/utilities/npm/PackageClient.java | 4 +- .../hl7/fhir/utilities/npm/PackageServer.java | 2 +- .../fhir/utilities/SimpleHTTPClientTest.java | 4 +- .../npm/BasePackageCacheManagerTests.java | 2 +- .../fhir/utilities/npm/PackageServerTest.java | 2 +- .../org/hl7/fhir/validation/IgLoader.java | 13 +- .../java/org/hl7/fhir/validation/Scanner.java | 8 +- .../hl7/fhir/validation/ValidationEngine.java | 11 +- .../validation/cli/utils/ProfileLoader.java | 8 +- .../hl7/fhir/validation/ipa/IPAValidator.java | 8 +- .../validation/tests/ValidationTests.java | 8 +- 25 files changed, 316 insertions(+), 143 deletions(-) create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/HTTPResult.java create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccess.java rename org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/{ => http}/SimpleHTTPClient.java (76%) diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java index 2116caa6d..64a1f0055 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/analytics/PackageVisitor.java @@ -14,11 +14,11 @@ import javax.xml.parsers.ParserConfigurationException; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r5.utils.EOperationOutcome; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.utilities.json.parser.JsonParser; @@ -233,8 +233,8 @@ public class PackageVisitor { JsonObject manifest = JsonParser.parseObjectFromUrl(repo+"/package.manifest.json"); File co = ManagedFileAccess.file(Utilities.path(cache, pid+"."+manifest.asString("date")+".tgz")); if (!co.exists()) { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - HTTPResult res = fetcher.get(repo+"/package.tgz?nocache=" + System.currentTimeMillis()); + + HTTPResult res = ManagedWebAccess.get(repo+"/package.tgz?nocache=" + System.currentTimeMillis()); res.checkThrowException(); TextFile.bytesToFile(res.getContent(), co); } @@ -330,8 +330,8 @@ public class PackageVisitor { private void processFeed(Set list, String str) throws IOException, ParserConfigurationException, SAXException { System.out.println("Feed "+str); try { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - HTTPResult res = fetcher.get(str+"?nocache=" + System.currentTimeMillis()); + + HTTPResult res = ManagedWebAccess.get(str+"?nocache=" + System.currentTimeMillis()); res.checkThrowException(); Document xml = XMLUtil.parseToDom(res.getContent()); for (Element channel : XMLUtil.getNamedChildren(xml.getDocumentElement(), "channel")) { diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CKMImporter.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CKMImporter.java index 03b39aab9..881bad4a9 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CKMImporter.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/CKMImporter.java @@ -40,11 +40,11 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.hl7.fhir.convertors.misc.adl.ADLImporter; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; import org.hl7.fhir.utilities.xml.XMLUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -125,8 +125,8 @@ public class CKMImporter { } private Document loadXml(String address) throws Exception { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(address, "application/xml"); + + HTTPResult res = ManagedWebAccess.get(address, "application/xml"); res.checkThrowException(); InputStream xml = new ByteArrayInputStream(res.getContent()); diff --git a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java index e2e402bce..e986b3570 100644 --- a/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java +++ b/org.hl7.fhir.convertors/src/main/java/org/hl7/fhir/convertors/misc/ICD11Generator.java @@ -25,10 +25,10 @@ import org.hl7.fhir.r4.model.ValueSet.ConceptSetComponent; import org.hl7.fhir.r4.model.ValueSet.FilterOperator; import org.hl7.fhir.r4.terminologies.CodeSystemUtilities; import org.hl7.fhir.r4.utils.ToolingExtensions; -import org.hl7.fhir.utilities.SimpleHTTPClient; -import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.model.JsonElement; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.utilities.json.parser.JsonParser; diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/conformance/ProfileComparer.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/conformance/ProfileComparer.java index df0179d52..f9d34478d 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/conformance/ProfileComparer.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/conformance/ProfileComparer.java @@ -66,11 +66,11 @@ 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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; import org.hl7.fhir.utilities.validation.ValidationMessage; import org.hl7.fhir.utilities.validation.ValidationMessage.Source; @@ -1383,8 +1383,8 @@ public class ProfileComparer { File f = ManagedFileAccess.file(local); if (f.exists()) return TextFile.fileToString(f); - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source); + + HTTPResult res = ManagedWebAccess.get(source); res.checkThrowException(); String result = TextFile.bytesToString(res.getContent()); TextFile.stringToFile(result, f); diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/terminologies/TerminologyCacheManager.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/terminologies/TerminologyCacheManager.java index b481fe422..3d6b21878 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/terminologies/TerminologyCacheManager.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/terminologies/TerminologyCacheManager.java @@ -22,13 +22,14 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; -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; import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; public class TerminologyCacheManager { @@ -87,8 +88,7 @@ public class TerminologyCacheManager { try { System.out.println("Initialise terminology cache from " + source); - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source + "?nocache=" + System.currentTimeMillis()); res.checkThrowException(); unzip(new ByteArrayInputStream(res.getContent()), cacheFolder); } catch (Exception e) { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/SHLParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/SHLParser.java index e47867691..8c5b1c017 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/SHLParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/SHLParser.java @@ -29,11 +29,12 @@ import org.hl7.fhir.r5.context.IWorkerContext; import org.hl7.fhir.r5.formats.IParser.OutputStyle; import org.hl7.fhir.r5.test.utils.TestingUtilities; import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; -import org.hl7.fhir.utilities.SimpleHTTPClient; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; -import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonElement; import org.hl7.fhir.utilities.json.model.JsonElementType; @@ -287,9 +288,7 @@ public class SHLParser extends ParserBase { private HTTPResult fetchFile(String url, String ct) throws IOException { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - fetcher.addHeader("Accept", ct); - HTTPResult res = fetcher.get(url); + HTTPResult res = ManagedWebAccess.get(url, ct); res.checkThrowException(); return res; } @@ -298,10 +297,10 @@ public class SHLParser extends ParserBase { if (testMode) { return new HTTPResult(url, 200, "OK", "application/json", TextFile.streamToBytes(TestingUtilities.loadTestResourceStream("validator", "shlink.manifest.json"))); } - SimpleHTTPClient fetcher = new SimpleHTTPClient(); + JsonObject j = new JsonObject(); j.add("recipient", "FHIR Validator"); - HTTPResult res = fetcher.post(url, "application/json", org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json"); + HTTPResult res = ManagedWebAccess.post(url, org.hl7.fhir.utilities.json.parser.JsonParser.composeBytes(j), "application/json", "application/json"); res.checkThrowException(); return res; } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/TerminologyCacheManager.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/TerminologyCacheManager.java index fc3ea158b..e483a7c79 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/TerminologyCacheManager.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/terminologies/TerminologyCacheManager.java @@ -22,13 +22,14 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; -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; import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; public class TerminologyCacheManager { @@ -86,8 +87,7 @@ public class TerminologyCacheManager { try { System.out.println("Initialise terminology cache from "+source); - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source+"?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis()); res.checkThrowException(); unzip(new ByteArrayInputStream(res.getContent()), cacheFolder); } catch (Exception e) { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/HTTPResult.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/HTTPResult.java new file mode 100644 index 000000000..5957979ca --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/HTTPResult.java @@ -0,0 +1,63 @@ +package org.hl7.fhir.utilities.http; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.hl7.fhir.utilities.TextFile; +import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.http.SimpleHTTPClient.HTTPResultException; + +public class HTTPResult { + private int code; + private String contentType; + private byte[] content; + private String source; + private String message; + + + public HTTPResult(String source, int code, String message, String contentType, byte[] content) { + super(); + this.source = source; + this.code = code; + this.contentType = contentType; + this.content = content; + this.message = message; + } + + 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) { + String filename = Utilities.path("[tmp]", "http-log", "fhir-http-"+(SimpleHTTPClient.nextCounter())+".log"); + if (content == null || content.length == 0) { + HTTPResultException exception = new HTTPResultException(code, message, source, null); + throw new IOException(exception.message, exception); + } else { + Utilities.createDirectory(Utilities.path("[tmp]", "http-log")); + TextFile.bytesToFile(content, filename); + HTTPResultException exception = new HTTPResultException(code, message, source, filename); + throw new IOException(exception.message, exception); + } + } + } + + public String getMessage() { + return message; + } + + public String getContentAsString() { + return new String(content, StandardCharsets.UTF_8); + } +} \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccess.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccess.java new file mode 100644 index 000000000..93863d1b0 --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/ManagedWebAccess.java @@ -0,0 +1,164 @@ +package org.hl7.fhir.utilities.http; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ + + + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import org.hl7.fhir.utilities.Utilities; + +/** + * see security.md - manages access to the local file system by the FHIR HAPI Core library + * + * 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), + * or whether the host application provides controlled access, or whether no access is allowed at all + * (in which case other information providers need to be provided) + * + * @author Grahame + * + */ +public class ManagedWebAccess { + + public interface IWebAccessor { + HTTPResult get(String url, String accept, String userAgent, String authenticationHeader) throws IOException; + HTTPResult post(String url, byte[] bytes, String contentType, String accept, String userAgent, String authenticationHeader) throws IOException; + HTTPResult put(String url, byte[] bytes, String contentType, String accept, String userAgent, String authenticationHeader) throws IOException; + } + + public enum WebAccessPolicy { + DIRECT, // open access to the local file system, though access can be restricted only to files under the paths in AllowedPaths + MANAGED, // no access except by the FileSystemProxyProvider + PROHIBITED, // no access at all to File() services + } + + private static WebAccessPolicy accessPolicy = WebAccessPolicy.DIRECT; // for legacy reasons + private static List allowedDomains = new ArrayList<>(); + private static IWebAccessor accessor; + private static String userAgent; + + + public static WebAccessPolicy getAccessPolicy() { + return accessPolicy; + } + + public static void setAccessPolicy(WebAccessPolicy accessPolicy) { + ManagedWebAccess.accessPolicy = accessPolicy; + } + + private static boolean inAllowedPaths(String pathname) { + if (allowedDomains.isEmpty()) { + return true; + } + for (String s : allowedDomains) { + if (pathname.startsWith(s)) { + return true; + } + } + return false; + } + + public static String getUserAgent() { + return userAgent; + } + + public static void setUserAgent(String userAgent) { + ManagedWebAccess.userAgent = userAgent; + } + + public static HTTPResult get(String url) throws IOException { + return get(url, null, null, null); + } + + public static HTTPResult get(String url, String accept) throws IOException { + return get(url, accept, null, null); + } + + public static HTTPResult get(String url, String accept, String userAgent, String authenticationHeader) throws IOException { + switch (accessPolicy) { + case DIRECT: + if (!inAllowedPaths(url)) { + throw new IOException("The pathname '"+url+"' cannot be accessed by policy"); + } + return new SimpleHTTPClient().get(url, accept); + case MANAGED: + return accessor.get(url, accept, userAgent, authenticationHeader); + case PROHIBITED: + throw new IOException("Access the internet is not allowed by local security policy"); + default: + throw new IOException("Internal Error"); + } + } + + public static HTTPResult post(String url, byte[] content, String contentType, String accept) throws IOException { + return post(url, content, contentType, accept, null, null); + } + + public static HTTPResult post(String url, byte[] content, String contentType, String accept, String userAgent, String authenticationHeader) throws IOException { + switch (accessPolicy) { + case DIRECT: + if (!inAllowedPaths(url)) { + throw new IOException("The pathname '"+url+"' cannot be accessed by policy"); + } + return new SimpleHTTPClient().post(url, contentType, content, accept); + case MANAGED: + return accessor.get(url, accept, userAgent, authenticationHeader); + case PROHIBITED: + throw new IOException("Access the internet is not allowed by local security policy"); + default: + throw new IOException("Internal Error"); + } + } + + public static HTTPResult put(String url, byte[] content, String contentType, String accept, String userAgent, String authenticationHeader) throws IOException { + switch (accessPolicy) { + case DIRECT: + if (!inAllowedPaths(url)) { + throw new IOException("The pathname '"+url+"' cannot be accessed by policy"); + } + return new SimpleHTTPClient().put(url, contentType, content, accept); + case MANAGED: + return accessor.get(url, accept, userAgent, authenticationHeader); + case PROHIBITED: + throw new IOException("Access the internet is not allowed by local security policy"); + default: + throw new IOException("Internal Error"); + } + } +} \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/SimpleHTTPClient.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/SimpleHTTPClient.java similarity index 76% rename from org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/SimpleHTTPClient.java rename to org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/SimpleHTTPClient.java index 56461c690..0e9f1de49 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/SimpleHTTPClient.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/http/SimpleHTTPClient.java @@ -1,4 +1,4 @@ -package org.hl7.fhir.utilities; +package org.hl7.fhir.utilities.http; import java.io.IOException; import java.net.HttpURLConnection; @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; import org.hl7.fhir.exceptions.FHIRException; +import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.settings.FhirSettings; import lombok.Getter; @@ -67,61 +68,6 @@ public class SimpleHTTPClient { } - public static class HTTPResult { - private int code; - private String contentType; - private byte[] content; - private String source; - private String message; - - - public HTTPResult(String source, int code, String message, String contentType, byte[] content) { - super(); - this.source = source; - this.code = code; - this.contentType = contentType; - this.content = content; - this.message = message; - } - - 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) { - String filename = Utilities.path("[tmp]", "http-log", "fhir-http-"+(++counter)+".log"); - if (content == null || content.length == 0) { - HTTPResultException exception = new HTTPResultException(code, message, source, null); - throw new IOException(exception.message, exception); - } else { - Utilities.createDirectory(Utilities.path("[tmp]", "http-log")); - TextFile.bytesToFile(content, filename); - HTTPResultException exception = new HTTPResultException(code, message, source, filename); - throw new IOException(exception.message, exception); - } - } - } - - public String getMessage() { - return message; - } - - public String getContentAsString() { - return new String(content, StandardCharsets.UTF_8); - } - } - private List
headers = new ArrayList<>(); @Getter @Setter @@ -254,5 +200,9 @@ public class SimpleHTTPClient { return new HTTPResult(url, c.getResponseCode(), c.getResponseMessage(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getResponseCode() >= 400 ? c.getErrorStream() : c.getInputStream())); } + public static int nextCounter() { + return ++counter; + } + } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/JsonTrackingParser.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/JsonTrackingParser.java index 325c02617..f73ce6433 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/JsonTrackingParser.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/JsonTrackingParser.java @@ -40,10 +40,11 @@ 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; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -724,15 +725,13 @@ public class JsonTrackingParser { } public static JsonObject fetchJson(String source) throws IOException { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json"); res.checkThrowException(); return parseJson(res.getContent()); } public static JsonArray fetchJsonArray(String source) throws IOException { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json"); res.checkThrowException(); return parseJsonArray(res.getContent()); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java index 3bf7e9f37..cdc02db94 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/json/parser/JsonParser.java @@ -8,11 +8,12 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.List; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.JsonException; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonBoolean; @@ -671,10 +672,8 @@ public class JsonParser { } private static byte[] fetch(String source) throws IOException { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - fetcher.addHeader("Accept", "application/json, application/fhir+json"); String murl = source.contains("?") ? source+"&nocache=" + System.currentTimeMillis() : source+"?nocache=" + System.currentTimeMillis(); - HTTPResult res = fetcher.get(murl); + HTTPResult res = ManagedWebAccess.get(murl, "application/json, application/fhir+json"); res.checkThrowException(); return res.getContent(); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java index b326fcc40..f65c1aa48 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/FilesystemPackageCacheManager.java @@ -27,12 +27,13 @@ import lombok.With; import org.apache.commons.io.FileUtils; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.IniFile; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonElement; import org.hl7.fhir.utilities.json.model.JsonObject; @@ -658,8 +659,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException { try { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source); + HTTPResult res = ManagedWebAccess.get(source); res.checkThrowException(); return new ByteArrayInputStream(res.getContent()); } catch (Exception e) { @@ -803,9 +803,8 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } private void loadFromBuildServer() throws IOException { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis()); res.checkThrowException(); buildInfo = (JsonArray) JsonParser.parse(TextFile.bytesToString(res.getContent())); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java index ee64726f5..cf278228f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/NpmPackage.java @@ -68,11 +68,12 @@ import org.apache.commons.compress.compressors.gzip.GzipParameters; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.utilities.ByteProvider; 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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.JsonException; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonElement; @@ -1443,8 +1444,7 @@ public class NpmPackage { } public static NpmPackage fromUrl(String source) throws IOException { - SimpleHTTPClient fetcher = new SimpleHTTPClient(); - HTTPResult res = fetcher.get(source+"?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source+"?nocache=" + System.currentTimeMillis()); res.checkThrowException(); return fromPackage(new ByteArrayInputStream(res.getContent())); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java index 0728243bc..7374f8edc 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageClient.java @@ -18,11 +18,11 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; 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.http.HTTPResult; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.model.JsonArray; import org.hl7.fhir.utilities.json.model.JsonObject; import org.hl7.fhir.utilities.json.model.JsonProperty; diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java index 22b1143c4..341d9ce11 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java @@ -6,7 +6,7 @@ import java.util.stream.Collectors; import javax.annotation.Nullable; -import org.hl7.fhir.utilities.SimpleHTTPClient; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.settings.FhirSettings; import org.hl7.fhir.utilities.settings.PackageServerPOJO; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java index d2fe2e6c9..866894056 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/SimpleHTTPClientTest.java @@ -4,6 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.junit.jupiter.api.Test; public class SimpleHTTPClientTest { @@ -13,7 +15,7 @@ public class SimpleHTTPClientTest { SimpleHTTPClient http = new SimpleHTTPClient(); String url = "https://hl7.org/fhir/us/core/package-list.json?nocache=" + System.currentTimeMillis(); - SimpleHTTPClient.HTTPResult res = http.get(url, "application/json"); + HTTPResult res = http.get(url, "application/json"); // System.out.println(res.getCode()); // System.out.println(new String(res.getContent(), StandardCharsets.UTF_8)); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/BasePackageCacheManagerTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/BasePackageCacheManagerTests.java index f8db6c792..7d963a819 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/BasePackageCacheManagerTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/BasePackageCacheManagerTests.java @@ -9,7 +9,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.hl7.fhir.exceptions.FHIRException; -import org.hl7.fhir.utilities.SimpleHTTPClient; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageServerTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageServerTest.java index 824900fd8..1de2b1521 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageServerTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageServerTest.java @@ -10,7 +10,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Base64; -import org.hl7.fhir.utilities.SimpleHTTPClient; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java index 36fc1e218..60c802a90 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/IgLoader.java @@ -32,12 +32,13 @@ import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities; import org.hl7.fhir.utilities.ByteProvider; import org.hl7.fhir.utilities.IniFile; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.turtle.Turtle; @@ -416,8 +417,7 @@ public class IgLoader implements IValidationEngineLoader { private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException, IOException { try { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(source + "?nocache=" + System.currentTimeMillis()); res.checkThrowException(); return new ByteArrayInputStream(res.getContent()); } catch (IOException e) { @@ -561,14 +561,13 @@ public class IgLoader implements IValidationEngineLoader { private byte[] fetchFromUrlSpecific(String source, String contentType, boolean optional, List 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 - HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis(), contentType); + HTTPResult res = ManagedWebAccess.get(source + "?nocache=" + System.currentTimeMillis(), contentType); res.checkThrowException(); return res.getContent(); } catch (Exception e) { - HTTPResult res = http.get(source, contentType); + HTTPResult res = ManagedWebAccess.get(source, contentType); res.checkThrowException(); return res.getContent(); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java index eec3e31bc..bd78e36e5 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Scanner.java @@ -31,11 +31,12 @@ import org.hl7.fhir.r5.renderers.RendererFactory; import org.hl7.fhir.r5.renderers.utils.RenderingContext; import org.hl7.fhir.r5.renderers.utils.RenderingContext.GenerationRules; import org.hl7.fhir.r5.utils.EOperationOutcome; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.validation.ValidationMessage; import org.hl7.fhir.utilities.xhtml.XhtmlComposer; import org.hl7.fhir.validation.ValidatorUtils.SourceFile; @@ -318,8 +319,7 @@ public class Scanner { } protected void download(String address, String filename) throws IOException { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(address); + HTTPResult res = ManagedWebAccess.get(address); res.checkThrowException(); TextFile.bytesToFile(res.getContent(), filename); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java index 29df53882..6660e8f23 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ValidationEngine.java @@ -82,13 +82,14 @@ import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy; import org.hl7.fhir.utilities.FhirPublication; import org.hl7.fhir.utilities.IniFile; import org.hl7.fhir.utilities.SIDUtilities; -import org.hl7.fhir.utilities.SimpleHTTPClient; -import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.npm.CommonPackages; import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.npm.NpmPackage; @@ -915,8 +916,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP if (output.startsWith("http://")) { ByteArrayOutputStream bs = new ByteArrayOutputStream(); handleOutputToStream(r, output, bs, version); - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.post(output, "application/fhir+xml", bs.toByteArray(), "application/fhir+xml"); + HTTPResult res = ManagedWebAccess.post(output, bs.toByteArray(), "application/fhir+xml", "application/fhir+xml"); res.checkThrowException(); } else { FileOutputStream s = ManagedFileAccess.outStream(output); @@ -1073,8 +1073,7 @@ public class ValidationEngine implements IValidatorResourceFetcher, IValidationP @Override public byte[] fetchRaw(IResourceValidator validator, String source) throws IOException { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source); + HTTPResult res = ManagedWebAccess.get(source); res.checkThrowException(); return res.getContent(); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/ProfileLoader.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/ProfileLoader.java index 36ce2b154..539598ad8 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/ProfileLoader.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/ProfileLoader.java @@ -4,11 +4,12 @@ import java.io.File; import java.io.IOException; 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 org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; public class ProfileLoader { public static byte[] loadProfileSource(String src) throws FHIRException, IOException { @@ -25,8 +26,7 @@ public class ProfileLoader { private static byte[] loadProfileFromUrl(String src) throws FHIRException { try { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(src + "?nocache=" + System.currentTimeMillis()); + HTTPResult res = ManagedWebAccess.get(src + "?nocache=" + System.currentTimeMillis()); res.checkThrowException(); return res.getContent(); } catch (Exception e) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ipa/IPAValidator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ipa/IPAValidator.java index a4f366331..828b03d47 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ipa/IPAValidator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/ipa/IPAValidator.java @@ -7,8 +7,9 @@ import java.util.Map; import org.hl7.fhir.r5.elementmodel.Element; import org.hl7.fhir.r5.elementmodel.JsonParser; -import org.hl7.fhir.utilities.SimpleHTTPClient; -import org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.validation.ValidationMessage; import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity; import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType; @@ -160,8 +161,7 @@ public class IPAValidator { private Element makeRequest(ValidationNode vn, String url) { try { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult result = http.get(url, "application/fhir+json"); + HTTPResult result = ManagedWebAccess.get(url, "application/fhir+json"); if (result.getCode() >= 300) { vn.getIssues().add(new ValidationMessage(Source.IPAValidator, IssueType.EXCEPTION, "http.request", "HTTP Return code is "+result.getCode()+" "+result.getMessage(), diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTests.java index 825ea6243..808baf51e 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTests.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/tests/ValidationTests.java @@ -70,12 +70,13 @@ import org.hl7.fhir.r5.utils.validation.constants.CodedContentValidationPolicy; import org.hl7.fhir.r5.utils.validation.constants.ContainedReferenceValidationPolicy; import org.hl7.fhir.r5.utils.validation.constants.ReferenceValidationPolicy; import org.hl7.fhir.utilities.FhirPublication; -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.filesystem.ManagedFileAccess; +import org.hl7.fhir.utilities.http.HTTPResult; +import org.hl7.fhir.utilities.http.ManagedWebAccess; +import org.hl7.fhir.utilities.http.SimpleHTTPClient; import org.hl7.fhir.utilities.json.JsonException; import org.hl7.fhir.utilities.json.JsonTrackingParser; import org.hl7.fhir.utilities.json.JsonUtilities; @@ -828,8 +829,7 @@ public class ValidationTests implements IEvaluationContext, IValidatorResourceFe @Override public byte[] fetchRaw(IResourceValidator validator, String source) throws MalformedURLException, IOException { - SimpleHTTPClient http = new SimpleHTTPClient(); - HTTPResult res = http.get(source); + HTTPResult res = ManagedWebAccess.get(source); res.checkThrowException(); return res.getContent(); }