From 08ed54fd35c20e94d0b6b074dbc3d52f9621d144 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Tue, 31 Jan 2023 10:32:21 +1100 Subject: [PATCH] Rework Package API to allow for authentication on package servers --- .../convertors/analytics/PackageVisitor.java | 3 +- .../npm/BasePackageCacheManager.java | 24 ++++----- .../npm/FilesystemPackageCacheManager.java | 16 +++--- .../hl7/fhir/utilities/npm/PackageClient.java | 11 ++-- .../fhir/utilities/npm/PackageScanner.java | 6 +-- .../hl7/fhir/utilities/npm/PackageServer.java | 53 +++++++++++++++++++ .../fhir/utilities/npm/PackageClientTest.java | 2 +- .../validation/packages/PackageValidator.java | 3 +- 8 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java 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 67c6c0071..fb7150f59 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 @@ -24,6 +24,7 @@ import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.npm.PackageClient; import org.hl7.fhir.utilities.npm.PackageInfo; +import org.hl7.fhir.utilities.npm.PackageServer; import org.hl7.fhir.utilities.npm.ToolsVersion; import org.hl7.fhir.utilities.xml.XMLUtil; import org.w3c.dom.Document; @@ -108,7 +109,7 @@ public class PackageVisitor { public void visitPackages() throws IOException, ParserConfigurationException, SAXException { System.out.println("Finding packages"); - pc = new PackageClient(PackageClient.PRIMARY_SERVER); + pc = new PackageClient(PackageServer.primaryServer()); pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION); Map cpidMap = getAllCIPackages(); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java index 179b342b3..e54121b92 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/BasePackageCacheManager.java @@ -18,8 +18,8 @@ import org.slf4j.LoggerFactory; public abstract class BasePackageCacheManager implements IPackageCacheManager { private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class); - private List myPackageServers = new ArrayList<>(); - private Function myClientFactory = address -> new PackageClient(address); + protected List myPackageServers = new ArrayList<>(); + private Function myClientFactory = server -> new PackageClient(server); protected boolean silent; /** @@ -32,20 +32,20 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { /** * Provide a new client factory implementation */ - public void setClientFactory(Function theClientFactory) { + public void setClientFactory(Function theClientFactory) { Validate.notNull(theClientFactory, "theClientFactory must not be null"); myClientFactory = theClientFactory; } - public List getPackageServers() { + public List getPackageServers() { return myPackageServers; } /** * Add a package server that can be used to fetch remote packages */ - public void addPackageServer(@Nonnull String thePackageServer) { - Validate.notBlank(thePackageServer, "thePackageServer must not be null or empty"); + public void addPackageServer(@Nonnull PackageServer thePackageServer) { + Validate.notNull(thePackageServer, "thePackageServer must not be null or empty"); if (!myPackageServers.contains(thePackageServer)) { myPackageServers.add(thePackageServer); } @@ -66,8 +66,8 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { @Nullable protected InputStreamWithSrc loadFromPackageServer(String id, String version) { - for (String nextPackageServer : getPackageServers()) { - if (okToUsePackageServer(nextPackageServer, id)) { + for (PackageServer nextPackageServer : getPackageServers()) { + if (okToUsePackageServer(nextPackageServer.getUrl(), id)) { PackageClient packageClient = myClientFactory.apply(nextPackageServer); try { if (Utilities.noString(version)) { @@ -110,7 +110,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { return npm.canonical(); } - for (String nextPackageServer : getPackageServers()) { + for (PackageServer nextPackageServer : getPackageServers()) { result = getPackageUrl(packageId, nextPackageServer); if (result != null) { return result; @@ -121,7 +121,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { } - private String getPackageUrl(String packageId, String server) throws IOException { + private String getPackageUrl(String packageId, PackageServer server) throws IOException { PackageClient pc = myClientFactory.apply(server); List res = pc.search(packageId, null, null, false); if (res.size() == 0) { @@ -136,7 +136,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { public String getPackageId(String canonicalUrl) throws IOException { String result = null; - for (String nextPackageServer : getPackageServers()) { + for (PackageServer nextPackageServer : getPackageServers()) { result = getPackageId(canonicalUrl, nextPackageServer); if (result != null) { break; @@ -146,7 +146,7 @@ public abstract class BasePackageCacheManager implements IPackageCacheManager { return result; } - private String getPackageId(String canonical, String server) throws IOException { + private String getPackageId(String canonical, PackageServer server) throws IOException { if (canonical == null) { return null; } 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 e7794bc2b..6cdc886d6 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 @@ -107,8 +107,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple */ @Deprecated public FilesystemPackageCacheManager(boolean userMode, int toolsVersion) throws IOException { - addPackageServer(PackageClient.PRIMARY_SERVER); - addPackageServer(PackageClient.SECONDARY_SERVER); + myPackageServers.addAll(PackageServer.publicServers()); if (userMode) cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages"); @@ -122,8 +121,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } public FilesystemPackageCacheManager(boolean userMode) throws IOException { - addPackageServer(PackageClient.PRIMARY_SERVER); - addPackageServer(PackageClient.SECONDARY_SERVER); + myPackageServers.addAll(PackageServer.publicServers()); if (userMode) cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "packages"); @@ -218,7 +216,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } } - private void listSpecs(Map specList, String server) throws IOException { + private void listSpecs(Map specList, PackageServer server) throws IOException { PackageClient pc = new PackageClient(server); List matches = pc.search(null, null, null, false); for (PackageInfo m : matches) { @@ -244,9 +242,9 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple } public String getLatestVersion(String id) throws IOException { - for (String nextPackageServer : getPackageServers()) { + for (PackageServer nextPackageServer : getPackageServers()) { // special case: - if (!(Utilities.existsInList(id,CommonPackages.ID_PUBPACK, "hl7.terminology.r5") && PackageClient.PRIMARY_SERVER.equals(nextPackageServer))) { + if (!(Utilities.existsInList(id,CommonPackages.ID_PUBPACK, "hl7.terminology.r5") && PackageServer.PRIMARY_SERVER.equals(nextPackageServer.getUrl()))) { PackageClient pc = new PackageClient(nextPackageServer); try { return pc.getLatestVersion(id); @@ -488,7 +486,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple for (NpmPackage p : temporaryPackages) { specList.put(p.name(), p.canonical()); } - for (String next : getPackageServers()) { + for (PackageServer next : getPackageServers()) { listSpecs(specList, next); } addCIBuildSpecs(specList); @@ -945,7 +943,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple if (packageInstalled(id, ver)) { return true; } - for (String s : getPackageServers()) { + for (PackageServer s : getPackageServers()) { if (new PackageClient(s).exists(id, ver)) { return true; } 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 9b48f3d1e..32aa8d76c 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 @@ -26,16 +26,15 @@ public class PackageClient { - public static final String PRIMARY_SERVER = "http://packages.fhir.org"; - public static final String SECONDARY_SERVER = "https://packages2.fhir.org/packages"; - - private String address; + private PackageServer server; private String cacheFolder; + private String address; - public PackageClient(String address) { + public PackageClient(PackageServer server) { super(); - this.address = address; + this.server = server; + address = server.getUrl(); try { cacheFolder = Utilities.path(System.getProperty("user.home"), ".fhir", "package-client"); Utilities.createDirectory(cacheFolder); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java index cb2b24391..1278e8482 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageScanner.java @@ -20,8 +20,8 @@ public class PackageScanner { List output = new ArrayList<>(); Set packages = new HashSet<>(); - processServer("http://packages.fhir.org", output, packages); - processServer("http://packages2.fhir.org/packages", output, packages); + processServer(PackageServer.primaryServer(), output, packages); + processServer(PackageServer.secondaryServer(), output, packages); StringBuilder b = new StringBuilder(); for (String s : output) { @@ -32,7 +32,7 @@ public class PackageScanner { TextFile.stringToFile(b.toString(), Utilities.path("[tmp]", "packages.csv")); } - public static void processServer(String server, List output, Set packages) throws IOException { + public static void processServer(PackageServer server, List output, Set packages) throws IOException { System.out.println("Server: "+server); PackageClient client = new PackageClient(server); List list = client.search(null, null, null, false); 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 new file mode 100644 index 000000000..584ba18ff --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/npm/PackageServer.java @@ -0,0 +1,53 @@ +package org.hl7.fhir.utilities.npm; + +import java.util.ArrayList; +import java.util.List; + +public class PackageServer { + + public enum PackageServerAuthenticationMode { + NONE + } + + public PackageServer(String url) { + this.url = url; + mode = PackageServerAuthenticationMode.NONE; + } + private String url; + private PackageServerAuthenticationMode mode; + private String username; + private String password; + public String getUrl() { + return url; + } + public PackageServerAuthenticationMode getMode() { + return mode; + } + public String getUsername() { + return username; + } + public String getPassword() { + return password; + } + + + + public static final String PRIMARY_SERVER = "http://packages.fhir.org"; + public static final String SECONDARY_SERVER = "https://packages2.fhir.org/packages"; + + public static PackageServer primaryServer() { + return new PackageServer(PRIMARY_SERVER); + } + + public static PackageServer secondaryServer() { + return new PackageServer(SECONDARY_SERVER); + } + + public static List publicServers() { + List servers = new ArrayList<>(); + servers.add(primaryServer()); + servers.add(secondaryServer()); + return servers; + } + +} \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java index 3d91409c8..6409e0380 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/PackageClientTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; public class PackageClientTest implements ResourceLoaderTests { - PackageClient packageClient = new PackageClient(PackageClient.PRIMARY_SERVER); + PackageClient packageClient = new PackageClient(PackageServer.primaryServer()); private void assertExpectedFields(final PackageInfo packageInfo) { assertEquals("dummy.package", packageInfo.getId()); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/packages/PackageValidator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/packages/PackageValidator.java index be105a8fa..3887a6850 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/packages/PackageValidator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/packages/PackageValidator.java @@ -11,6 +11,7 @@ import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; import org.hl7.fhir.utilities.npm.NpmPackage; import org.hl7.fhir.utilities.npm.PackageClient; import org.hl7.fhir.utilities.npm.PackageInfo; +import org.hl7.fhir.utilities.npm.PackageServer; import org.hl7.fhir.utilities.npm.ToolsVersion; public class PackageValidator { @@ -22,7 +23,7 @@ public class PackageValidator { private void execute() throws IOException { FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION); - PackageClient pc = new PackageClient(PackageClient.PRIMARY_SERVER); + PackageClient pc = new PackageClient(PackageServer.primaryServer()); for (PackageInfo t : pc.search(null, null, null, false)) { System.out.println("Check Package "+t.getId()); List vl = pc.getVersions(t.getId());