Search in sources :

Example 76 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method fetchTheOldWay.

// ----- the old way, from before package server, while everything gets onto the package server
private InputStreamWithSrc fetchTheOldWay(String id, String v) {
    String url = getUrlForPackage(id);
    if (url == null) {
        try {
            url = getPackageUrlFromBuildList(id);
        } catch (Exception e) {
            url = null;
        }
    }
    if (url == null) {
        throw new FHIRException("Unable to resolve package id " + id + "#" + v);
    }
    if (url.contains("/ImplementationGuide/")) {
        url = url.substring(0, url.indexOf("/ImplementationGuide/"));
    }
    String pu = Utilities.pathURL(url, "package-list.json");
    String aurl = pu;
    JsonObject json;
    try {
        json = JsonTrackingParser.fetchJson(pu);
    } catch (Exception e) {
        String pv = Utilities.pathURL(url, v, "package.tgz");
        try {
            aurl = pv;
            InputStreamWithSrc src = new InputStreamWithSrc(fetchFromUrlSpecific(pv, false), pv, v);
            return src;
        } catch (Exception e1) {
            throw new FHIRException("Error fetching package directly (" + pv + "), or fetching package list for " + id + " from " + pu + ": " + e1.getMessage(), e1);
        }
    }
    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")) {
        JsonObject vo = (JsonObject) e;
        if (v.equals(JSONUtil.str(vo, "version"))) {
            aurl = Utilities.pathURL(JSONUtil.str(vo, "path"), "package.tgz");
            String u = Utilities.pathURL(JSONUtil.str(vo, "path"), "package.tgz");
            return new InputStreamWithSrc(fetchFromUrlSpecific(u, true), u, v);
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 77 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method loadPackageFromCacheOnly.

/**
 * Load the identified package from the cache - if it exists
 * <p>
 * This is for special purpose only (testing, control over speed of loading).
 * Generally, use the loadPackage method
 *
 * @param id
 * @param version
 * @return
 * @throws IOException
 */
@Override
public NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException {
    if (!Utilities.noString(version) && version.startsWith("file:")) {
        return loadPackageFromFile(id, version.substring(5));
    }
    for (NpmPackage p : temporaryPackages) {
        if (p.name().equals(id) && ("current".equals(version) || "dev".equals(version) || p.version().equals(version))) {
            return p;
        }
        if (p.name().equals(id) && Utilities.noString(version)) {
            return p;
        }
    }
    String foundPackage = null;
    String foundVersion = null;
    for (String f : reverseSorted(new File(cacheFolder).list())) {
        File cf = new File(Utilities.path(cacheFolder, f));
        if (cf.isDirectory()) {
            if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
                return loadPackageInfo(Utilities.path(cacheFolder, f));
            }
            if (version != null && version.endsWith(".x") && f.contains("#")) {
                String[] parts = f.split("#");
                if (parts[0].equals(id) && VersionUtilities.isMajMinOrLaterPatch((foundVersion != null ? foundVersion : version), parts[1])) {
                    foundVersion = parts[1];
                    foundPackage = f;
                }
            }
        }
    }
    if (foundPackage != null) {
        return loadPackageInfo(Utilities.path(cacheFolder, foundPackage));
    }
    if ("dev".equals(version))
        return loadPackageFromCacheOnly(id, "current");
    else
        return null;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 78 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class NpmPackage method fromFolder.

/**
 * Factory method that parses a package from an extracted folder
 */
public static NpmPackage fromFolder(String path) throws IOException {
    NpmPackage res = new NpmPackage();
    res.loadFiles(path, new File(path));
    res.checkIndexed(path);
    return res;
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 79 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class NpmPackage method loadSubFolders.

private void loadSubFolders(String rootPath, File dir) throws IOException {
    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            String d = f.getAbsolutePath().substring(rootPath.length() + 1);
            if (!d.startsWith("package")) {
                d = Utilities.path("package", d);
            }
            NpmPackageFolder folder = this.new NpmPackageFolder(d);
            folder.folder = f;
            this.folders.put(d, folder);
            File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
            if (ij.exists()) {
                try {
                    if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) {
                        indexFolder(folder.getName(), folder);
                    }
                } catch (Exception e) {
                    throw new IOException("Error parsing " + ij.getAbsolutePath() + ": " + e.getMessage(), e);
                }
            }
            loadSubFolders(rootPath, f);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 80 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class NpmPackage method indexFolder.

public void indexFolder(String desc, NpmPackageFolder folder) throws FileNotFoundException, IOException {
    List<String> remove = new ArrayList<>();
    NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
    indexer.start();
    for (String n : folder.listFiles()) {
        if (!indexer.seeFile(n, folder.fetchFile(n))) {
            remove.add(n);
        }
    }
    for (String n : remove) {
        folder.removeFile(n);
    }
    String json = indexer.build();
    try {
        folder.readIndex(JsonTrackingParser.parseJson(json));
        if (folder.folder != null) {
            TextFile.stringToFile(json, Utilities.path(folder.folder.getAbsolutePath(), ".index.json"));
        }
    } catch (Exception e) {
        TextFile.stringToFile(json, Utilities.path("[tmp]", ".index.json"));
        throw new IOException("Error parsing " + (desc == null ? "" : desc + "#") + "package/" + folder.name + "/.index.json: " + e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

File (java.io.File)25 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 TextFile (org.hl7.fhir.utilities.TextFile)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)15 JsonObject (com.google.gson.JsonObject)12 FileNotFoundException (java.io.FileNotFoundException)11 FileInputStream (java.io.FileInputStream)9 IniFile (org.hl7.fhir.utilities.IniFile)9 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)9 ParseException (java.text.ParseException)8 HashMap (java.util.HashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 JsonElement (com.google.gson.JsonElement)5 FileOutputStream (java.io.FileOutputStream)5 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)5 CSFile (org.hl7.fhir.utilities.CSFile)5