feat: added support for package.tgz uri in the "file:/" format (#3347)

* feat: added support for package.zgz uri in the "file:/" format

* added changelog entry

* add error code to thrown message

Co-authored-by: Tadgh <garygrantgraham@gmail.com>
This commit is contained in:
Patrick Werner 2022-02-05 19:25:50 +01:00 committed by GitHub
parent f6181fb6ff
commit 2531e6e57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 6 deletions

View File

@ -25,7 +25,7 @@ public final class Msg {
/**
* IMPORTANT: Please update the following comment after you add a new code
* Last code value: 2023
* Last code value: 2024
*/
private Msg() {}

View File

@ -0,0 +1,4 @@
---
type: add
issue: 3347
title: "Added ability to load Implementation Guide packages from filesystem by supporting the file:/ syntax of url."

View File

@ -88,7 +88,11 @@ import javax.transaction.Transactional;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -474,6 +478,13 @@ public class JpaPackageCache extends BasePackageCacheManager implements IHapiPac
protected byte[] loadPackageUrlContents(String thePackageUrl) {
if (thePackageUrl.startsWith("classpath:")) {
return ClasspathUtil.loadResourceAsByteArray(thePackageUrl.substring("classpath:" .length()));
} else if (thePackageUrl.startsWith("file:")) {
try {
byte[] bytes = Files.readAllBytes(Paths.get(new URI(thePackageUrl)));
return bytes;
} catch (IOException | URISyntaxException e) {
throw new InternalErrorException(Msg.code(2024) + "Error loading \"" + thePackageUrl + "\": " + e.getMessage());
}
} else {
HttpClientConnectionManager connManager = new BasicHttpClientConnectionManager();
try (CloseableHttpResponse request = HttpClientBuilder

View File

@ -59,6 +59,8 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -938,6 +940,25 @@ public class NpmR4Test extends BaseJpaR4Test {
});
}
@Test
public void testInstallR4PackageFromFile() {
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current absolute path is: " + s);
String fileUrl = "file:" + s + "/src/test/resources/packages/de.basisprofil.r4-1.2.0.tgz";
myPackageInstallerSvc.install(new PackageInstallationSpec()
.setName("de.basisprofil.r4")
.setVersion("1.2.0")
.setPackageUrl(fileUrl)
);
runInTransaction(() -> {
assertTrue(myPackageVersionDao.findByPackageIdAndVersion("de.basisprofil.r4", "1.2.0").isPresent());
});
}
static class FakeNpmServlet extends HttpServlet {
private final Map<String, byte[]> myResponses = new HashMap<>();