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.ArrayList;
012import java.util.Collections;
013import java.util.Date;
014import java.util.List;
015import java.util.Set;
016
017import javax.annotation.Nonnull;
018import javax.annotation.Nullable;
019
020import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
021import org.hl7.fhir.utilities.TextFile;
022import org.hl7.fhir.utilities.Utilities;
023import org.hl7.fhir.utilities.VersionUtilities;
024import org.hl7.fhir.utilities.http.HTTPAuthenticationMode;
025import org.hl7.fhir.utilities.http.HTTPResult;
026import org.hl7.fhir.utilities.http.ManagedWebAccess;
027import org.hl7.fhir.utilities.http.ManagedWebAccessBuilder;
028import org.hl7.fhir.utilities.json.model.JsonArray;
029import org.hl7.fhir.utilities.json.model.JsonObject;
030import org.hl7.fhir.utilities.json.model.JsonProperty;
031import org.hl7.fhir.utilities.json.parser.JsonParser;
032
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      System.out.println("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 canonical, String fhirVersion, boolean preRelease) throws IOException {
134    CommaSeparatedStringBuilder params = new CommaSeparatedStringBuilder("&");
135    if (!Utilities.noString(name)) {
136      params.append("name="+name);
137    }
138    if (!Utilities.noString(canonical)) {
139      params.append("pkgcanonical="+canonical);
140    }
141    if (!Utilities.noString(fhirVersion)) {
142      params.append("fhirversion="+fhirVersion);
143    }
144    if (preRelease) {
145      params.append("prerelease="+preRelease);
146    }
147    List<PackageInfo> res = new ArrayList<>();
148    try {
149      JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+params.toString());
150      boolean hasDates = true;
151      for (JsonObject obj : json.asJsonObjects()) {
152        Instant d = obj.has("date") ? obj.asDate("date") : null;
153        if (d == null) {
154          hasDates = false;
155        }
156        res.add(new PackageInfo(obj.asString("Name", "name"),
157          obj.asString("Version", "version"),
158          obj.asString("FhirVersion", "fhirVersion"),
159          obj.asString("Description", "description"),
160          obj.asString("url"),
161          obj.asString("canonical"),
162          address, d));
163      }
164      if (hasDates) {
165        Collections.sort(res, new PackageInfo.PackageInfoSorter(true));
166      } else {
167        Collections.sort(res, new PackageInfo.PackageInfoSorter(false));
168      }
169    } catch (IOException e1) {
170    }
171    return res;    
172  }  
173
174  public Date getNewPackages(Date lastCalled, List<PackageInfo> updates) {
175    return null;
176  }
177 
178  private InputStream fetchUrl(String source, String accept) throws IOException {
179    ManagedWebAccessBuilder client = ManagedWebAccess.builder().withAccept(accept);
180    if (server.getAuthenticationMode() == HTTPAuthenticationMode.TOKEN) {
181      client.withToken(server.getToken());
182    } else if (server.getAuthenticationMode() == HTTPAuthenticationMode.BASIC) {
183      client.withBasicAuth(server.getUsername(), server.getPassword());
184    }
185    HTTPResult res = client.get(source);
186    res.checkThrowException();
187    return new ByteArrayInputStream(res.getContent());
188  }
189
190  private JsonObject fetchJson(String source) throws IOException {
191    String src = TextFile.streamToString(fetchUrl(source, "application/json"));
192    //System.out.println(src);
193    return JsonParser.parseObject(src);
194  }
195  
196  private JsonArray fetchJsonArray(String source) throws IOException {
197    String src = TextFile.streamToString(fetchUrl(source, "application/json"));
198    //System.out.println(src);
199    return (JsonArray) JsonParser.parse(src);
200  }
201
202  public String url(String id, String v) {
203    return Utilities.pathURL(address, id, v);
204  }
205
206  public String getLatestVersion(String id) throws IOException {
207    List<PackageInfo> list = getVersions(id);
208    if (list.isEmpty()) {
209      throw new IOException("Package not found: "+id);
210    } else {
211      String v = list.get(0).getVersion();
212      for (PackageInfo p : list) {
213        if (VersionUtilities.isThisOrLater(v, p.getVersion())) {
214          v = p.getVersion();
215        }
216      }
217      return v;
218    }
219  }
220  
221  public String getLatestVersion(String id, String specVersion) throws IOException {
222    List<PackageInfo> list = getVersions(id);
223    if (list.isEmpty()) {
224      throw new IOException("Package not found: "+id);
225    } else {
226      String v = null;
227      for (PackageInfo p : list) {
228        if (VersionUtilities.isMajMinOrLaterPatch(specVersion, p.getVersion())) {
229          v = p.getVersion();
230        }
231      }
232      return v;
233    }
234  }
235
236  protected PackageInfo getPackageInfoFromJSON(JsonObject o, String name, String canonical, String fhirVersion) {
237      String id = o.asString("npm-name");
238      String pname = o.asString("name");
239      String pcanonical = o.asString("canonical");
240      String description = o.asString("description");
241      Instant d = o.has("date") ? o.asDate("date") : null;
242      boolean ok = true;
243      if (ok && !Utilities.noString(name)) {
244        ok = (pname != null && pname.contains(name)) || (description != null && description.contains(name)) || (id != null && id.contains(name));
245      }
246      if (ok && !Utilities.noString(canonical)) {
247        ok = pcanonical.contains(canonical);
248      }
249      String version = null;
250      String fVersion = null;
251      String url = null;
252
253      if (ok) {
254        // if we can find something...
255        for (JsonObject e : o.getJsonObjects("editions")) {
256          if (fhirVersion == null || fhirVersion.equals(e.asString("fhir-version"))) {
257            String v = e.asString("ig-version");
258            if (version == null || VersionUtilities.isThisOrLater(version, v)) {
259              version = v;
260              fVersion = e.getJsonArray("fhir-version").get(0).asString();
261              url = e.asString("url");
262
263              String npmPackage = e.asString("package");
264              if (npmPackage != null && id == null) {
265                id = npmPackage.substring(0, npmPackage.indexOf("#"));
266              }
267            }
268          }
269        }
270      }
271      return new PackageInfo(id, version, fVersion, description, url, pcanonical, address, d);
272  }
273  
274  public List<PackageInfo> listFromRegistry(String name, String canonical, String fhirVersion) throws IOException {
275    List<PackageInfo> result = new ArrayList<>();
276    JsonObject packages = JsonParser.parseObjectFromUrl("https://fhir.github.io/ig-registry/fhir-ig-list.json?nocache=" + System.currentTimeMillis());
277    for (JsonObject o : packages.getJsonObjects("guides")) {
278      if (o.has("canonical")) {
279      final PackageInfo packageInfo = getPackageInfoFromJSON(o, name, canonical, fhirVersion);
280        if (packageInfo.getVersion() != null) {
281          result.add(packageInfo);
282        }
283      }
284    }
285    return result;
286  }
287
288  public void findDependents(Set<String> list, String id) {
289    CommaSeparatedStringBuilder params = new CommaSeparatedStringBuilder("&");
290    params.append("dependency="+id.replace("#", "|"));
291    try {
292      JsonArray json = fetchJsonArray(Utilities.pathURL(address, "catalog?")+params.toString());
293      for (JsonObject obj : json.asJsonObjects()) {
294        list.add(obj.asString("Name", "name"));
295      }
296    } catch (IOException e1) {
297    }
298  }
299  
300}