001package org.hl7.fhir.utilities.npm;
002
003import java.io.ByteArrayInputStream;
004import java.io.FileNotFoundException;
005import java.io.IOException;
006import java.io.InputStream;
007import java.text.ParseException;
008import java.text.SimpleDateFormat;
009import java.time.Instant;
010import java.time.format.DateTimeParseException;
011import java.util.*;
012
013import javax.annotation.Nullable;
014
015import lombok.extern.slf4j.Slf4j;
016import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
017import org.hl7.fhir.utilities.FileUtilities;
018import org.hl7.fhir.utilities.Utilities;
019import org.hl7.fhir.utilities.VersionUtilities;
020import org.hl7.fhir.utilities.http.HTTPAuthenticationMode;
021import org.hl7.fhir.utilities.http.HTTPResult;
022import org.hl7.fhir.utilities.http.ManagedWebAccess;
023import org.hl7.fhir.utilities.http.ManagedWebAccessor;
024import org.hl7.fhir.utilities.json.model.JsonArray;
025import org.hl7.fhir.utilities.json.model.JsonObject;
026import org.hl7.fhir.utilities.json.model.JsonProperty;
027import org.hl7.fhir.utilities.json.parser.JsonParser;
028
029import static org.hl7.fhir.utilities.VersionUtilities.checkVersionNotNullAndValid;
030import static org.hl7.fhir.utilities.VersionUtilities.fixForSpecialValue;
031
032@Slf4j
033public class PackageClient {
034
035  private PackageServer server;
036  private String address;
037
038
039  public PackageClient(PackageServer server) {
040    super();
041    this.server = server;
042    address = this.server.getUrl();
043  }
044
045  public boolean exists(String id, String ver) throws IOException {
046    List<PackageInfo> vl = getVersions(id);
047    for (PackageInfo pi : vl) {
048      if (ver == null || ver.equals(pi.getVersion())) {
049        return true;
050      }
051    }
052    return false;
053  }
054
055  public InputStream fetch(String id, String ver) throws IOException {
056    return fetchCached(getPackageTarballUrl(id, ver));
057  }
058
059  private String getPackageTarballUrl(String id, String ver) throws IOException {
060    if (server.getServerType() == PackageServer.PackageServerType.NPM) {
061      return getNpmServerTarballUrl(id, ver);
062    }
063    return Utilities.pathURL(address, id, ver);
064  }
065
066  private String getNpmServerTarballUrl(String id, String ver) throws IOException {
067    String packageDescriptorUrl = Utilities.pathURL(address, id, ver);
068    JsonObject json;
069
070      json = fetchJson(packageDescriptorUrl);
071      JsonObject dist = json.getJsonObject("dist");
072      return dist.getJsonString("tarball").asString();
073
074  }
075
076  public InputStream fetch(PackageInfo info) throws IOException {
077    return fetchCached(getPackageTarballUrl(info.getId(), info.getVersion()));
078  }
079
080  public InputStream fetchCached(String url) throws IOException, FileNotFoundException {
081    return fetchUrl(url, null);
082  }
083
084  public List<PackageInfo> getVersions(String id) throws IOException {
085    String url = Utilities.pathURL(address, id);
086    List<PackageInfo> res = new ArrayList<>();
087    JsonObject json;
088    try {
089      json = fetchJson(url);
090      JsonObject versions = json.getJsonObject("versions");
091      boolean hasDates = true;
092      if (versions != null) {
093        for (JsonProperty v : versions.getProperties()) {
094          JsonObject obj = versions.getJsonObject(v.getName());
095          Instant d = getInstantFromPackageDate(obj);
096          if (d == null) {
097            hasDates = false;
098          }
099          res.add(new PackageInfo(obj.asString("Name", "name"),
100            obj.asString("Version", "version"),
101            obj.asString("FhirVersion", "fhirVersion"),
102            obj.asString("Description", "description"),
103            obj.asString("url"),
104            obj.asString("canonical"),
105            address, d));
106        }
107      }
108      if (hasDates) {
109        Collections.sort(res, new PackageInfo.PackageInfoSorter(true));
110      } else {
111        Collections.sort(res, new PackageInfo.PackageInfoSorter(false));
112      }
113    } catch (Exception e) {
114      log.warn("Error fetching "+url+": "+e.getMessage());
115    }
116    return res;    
117  }
118
119  @Nullable
120  private static Instant getInstantFromPackageDate(JsonObject obj) {
121    try {
122      return obj.hasString("date") ? obj.asDate("date") : null;
123    } catch (DateTimeParseException e) {
124      try {
125        return new SimpleDateFormat("yyyyMMddhhmmss").parse(obj.getJsonString("date").asString()).toInstant();
126      } catch (ParseException ex) {
127        // we tried to parse the date, but failed twice. We're just going to pretend we didn't get a date 
128        return null;    
129      }
130    }
131  }
132
133  public List<PackageInfo> search(String name, String pkgCanonical, String fhirVersion, boolean preRelease, String canonical) throws IOException {
134    CommaSeparatedStringBuilder params = new CommaSeparatedStringBuilder("&");
135    if (!Utilities.noString(name)) {
136      params.append("name="+name);
137    }
138    if (!Utilities.noString(pkgCanonical)) {
139      params.append("pkgcanonical="+pkgCanonical);
140    }
141    if (!Utilities.noString(fhirVersion)) {
142      params.append("fhirversion="+fhirVersion);
143    }
144    if (!Utilities.noString(canonical)) {
145      params.append("canonical="+canonical);
146    }
147    if (preRelease) {
148      params.append("prerelease="+preRelease);
149    }
150    List<PackageInfo> res = new ArrayList<>();
151    try {
152      JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+ params);
153      boolean hasDates = true;
154      for (JsonObject obj : json.asJsonObjects()) {
155        Instant d = obj.has("date") ? obj.asDate("date") : null;
156        if (d == null) {
157          hasDates = false;
158        }
159        res.add(new PackageInfo(obj.asString("Name", "name"),
160          obj.asString("Version", "version"),
161          obj.asString("FhirVersion", "fhirVersion"),
162          obj.asString("Description", "description"),
163          obj.asString("url"),
164          obj.asString("canonical"),
165          address, d));
166      }
167      if (hasDates) {
168        Collections.sort(res, new PackageInfo.PackageInfoSorter(true));
169      } else {
170        Collections.sort(res, new PackageInfo.PackageInfoSorter(false));
171      }
172    } catch (IOException e1) {
173    }
174    return res;    
175  }  
176
177  public Date getNewPackages(Date lastCalled, List<PackageInfo> updates) {
178    return null;
179  }
180 
181  private InputStream fetchUrl(String source, String accept) throws IOException {
182    ManagedWebAccessor webAccessor = ManagedWebAccess.accessor(Arrays.asList("web"));
183    if (server.getAuthenticationMode() == HTTPAuthenticationMode.TOKEN) {
184      webAccessor.withToken(server.getToken());
185    } else if (server.getAuthenticationMode() == HTTPAuthenticationMode.BASIC) {
186      webAccessor.withBasicAuth(server.getUsername(), server.getPassword());
187    } else if (server.getAuthenticationMode() == HTTPAuthenticationMode.APIKEY) {
188      webAccessor.withApiKey(server.getApiKey());
189    }
190    HTTPResult res = webAccessor.get(source, accept);
191    res.checkThrowException();
192    return new ByteArrayInputStream(res.getContent());
193  }
194
195  private JsonObject fetchJson(String source) throws IOException {
196    String src = FileUtilities.streamToString(fetchUrl(source, "application/json"));
197    return JsonParser.parseObject(src);
198  }
199  
200  private JsonArray fetchJsonArray(String source) throws IOException {
201    String src = FileUtilities.streamToString(fetchUrl(source, "application/json"));
202    return (JsonArray) JsonParser.parse(src);
203  }
204
205  public String url(String id, String v) {
206    return Utilities.pathURL(address, id, v);
207  }
208
209  public String getLatestVersion(String id, boolean mileStonesOnly) throws IOException {
210    List<PackageInfo> list = getVersions(id);
211    if (mileStonesOnly) {
212      list.removeIf(i -> i.getVersion().contains("-"));
213    }
214    if (list.isEmpty()) {
215      throw new IOException("Package not found: "+id);
216    } else {
217      String v = list.get(0).getVersion();
218      for (PackageInfo p : list) {
219        if (VersionUtilities.isThisOrLater(v, p.getVersion(), VersionUtilities.VersionPrecision.MINOR)) {
220          v = p.getVersion();
221        }
222      }
223      return v;
224    }
225  }
226  
227  public String getLatestVersion(String id, String specVersion) throws IOException {
228    List<PackageInfo> list = getVersions(id);
229    if (list.isEmpty()) {
230      throw new IOException("Package not found: "+id);
231    } else {
232      String v = null;
233      for (PackageInfo p : list) {
234        String version = p.getVersion();
235        if (VersionUtilities.isSemVer(version) && VersionUtilities.versionMatches(specVersion, version)) {
236          v = version;
237        }
238      }
239      if (v == null) {
240        for (PackageInfo p : list) {
241          String version = p.getVersion();
242          while (version.contains(".00")) {
243            version = version.replace(".00", ".0");
244          }
245          if (VersionUtilities.isSemVer(version) && VersionUtilities.versionMatches(specVersion, version)) {
246            v = version;
247          }
248        }
249      }
250      return v;
251    }
252  }
253
254  protected PackageInfo getPackageInfoFromJSON(JsonObject o, String name, String canonical, String fhirVersion) {
255      String packageId = o.asString("npm-name");
256      String packageName = o.asString("name");
257      String packageCanonical = o.asString("canonical");
258      String packageDescription = o.asString("description");
259      Instant packageDate = o.has("date") ? o.asDate("date") : null;
260      boolean ok = true;
261      if (ok && !Utilities.noString(name)) {
262        ok = (packageName != null && packageName.contains(name)) || (packageDescription != null && packageDescription.contains(name)) || (packageId != null && packageId.contains(name));
263      }
264      if (ok && !Utilities.noString(canonical)) {
265        ok = packageCanonical.contains(canonical);
266      }
267      String currentVersion = null;
268      String currentFhirVersion = null;
269      String currentUrl = null;
270
271      if (ok) {
272        // Get the latest version out of the available valid editions of this IG.
273        for (JsonObject edition : o.getJsonObjects("editions")) {
274          if (fhirVersion == null || fhirVersion.equals(edition.asString("fhir-version"))) {
275            String igVersion = edition.asString("ig-version");
276              if (!VersionUtilities.isSemVer(igVersion))
277              {
278                log.error("Non-semver version \"{}\" encountered while getting package info for {}", igVersion, edition.asString("package"));
279                continue;
280              }
281              // If this version is valid, check if it is later than the current version
282              if (currentVersion == null || VersionUtilities.isThisOrLater(currentVersion, igVersion, VersionUtilities.VersionPrecision.MINOR)) {
283                currentVersion = igVersion;
284                currentFhirVersion = edition.getJsonArray("fhir-version").get(0).asString();
285                currentUrl = edition.asString("url");
286
287                String npmPackage = edition.asString("package");
288                if (npmPackage != null && packageId == null) {
289
290                  packageId = npmPackage.substring(0, npmPackage.indexOf("#"));
291                }
292              }
293          }
294        }
295      }
296      return new PackageInfo(packageId, currentVersion, currentFhirVersion, packageDescription, currentUrl, packageCanonical, address, packageDate);
297  }
298  
299  public List<PackageInfo> listFromRegistry(String name, String canonical, String fhirVersion) throws IOException {
300    List<PackageInfo> result = new ArrayList<>();
301    JsonObject packages = JsonParser.parseObjectFromUrl("https://fhir.github.io/ig-registry/fhir-ig-list.json?nocache=" + System.currentTimeMillis());
302    for (JsonObject o : packages.getJsonObjects("guides")) {
303      if (o.has("canonical")) {
304      final PackageInfo packageInfo = getPackageInfoFromJSON(o, name, canonical, fhirVersion);
305        if (packageInfo.getVersion() != null) {
306          result.add(packageInfo);
307        }
308      }
309    }
310    return result;
311  }
312
313  public void findDependents(Set<String> list, String id) {
314    CommaSeparatedStringBuilder params = new CommaSeparatedStringBuilder("&");
315    params.append("dependency="+id.replace("#", "|"));
316    try {
317      JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+params.toString());
318      for (JsonObject obj : json.asJsonObjects()) {
319        list.add(obj.asString("Name", "name"));
320      }
321    } catch (IOException e1) {
322    }
323  }
324  
325}