001package org.hl7.fhir.utilities.npm;
002
003import lombok.Getter;
004import lombok.extern.slf4j.Slf4j;
005import org.hl7.fhir.exceptions.FHIRException;
006import org.hl7.fhir.utilities.FileUtilities;
007import org.hl7.fhir.utilities.Utilities;
008import org.hl7.fhir.utilities.http.HTTPResult;
009import org.hl7.fhir.utilities.http.ManagedWebAccess;
010import org.hl7.fhir.utilities.json.model.JsonArray;
011import org.hl7.fhir.utilities.json.model.JsonElement;
012import org.hl7.fhir.utilities.json.model.JsonObject;
013import org.hl7.fhir.utilities.json.parser.JsonParser;
014
015import java.io.ByteArrayInputStream;
016import java.io.IOException;
017import java.io.InputStream;
018import java.text.ParseException;
019import java.text.SimpleDateFormat;
020import java.util.*;
021
022@Slf4j
023public class CIBuildClient {
024
025  private static final String DEFAULT_ROOT_URL = "https://build.fhir.org";
026  private static final long DEFAULT_CI_QUERY_INTERVAL = 1000 * 60 * 60;
027  private final long ciQueryInterval;
028
029  @Getter
030  private long ciLastQueriedTimeStamp = 0;
031
032  @Getter
033  private JsonArray ciBuildInfo;
034
035  @Getter
036  private final String rootUrl;
037
038  /**
039   * key = packageId
040   * value = url of built package on https://build.fhir.org/ig/
041   **/
042  private final Map<String, String> ciPackageUrls = new HashMap<>();
043
044  public CIBuildClient() {
045   this(DEFAULT_ROOT_URL, DEFAULT_CI_QUERY_INTERVAL, false);
046  }
047
048  public CIBuildClient(String rootUrl, long ciQueryInterval, boolean silent) {
049    this.rootUrl = rootUrl;
050    this.ciQueryInterval = ciQueryInterval;
051  }
052
053  String getPackageId(String canonical) {
054    if (canonical == null) {
055      return null;
056    }
057    checkCIServerQueried();
058    if (ciBuildInfo != null) {
059      for (JsonElement n : ciBuildInfo) {
060        JsonObject o = (JsonObject) n;
061        if (canonical.equals(o.asString("url"))) {
062          return o.asString("package-id");
063        }
064      }
065      for (JsonElement n : ciBuildInfo) {
066        JsonObject o = (JsonObject) n;
067        if (o.asString("url").startsWith(canonical + "/ImplementationGuide/")) {
068          return o.asString("package-id");
069        }
070      }
071    }
072    return null;
073  }
074
075  String getPackageUrl(String packageId) {
076    checkCIServerQueried();
077    for (JsonObject o : ciBuildInfo.asJsonObjects()) {
078      if (packageId.equals(o.asString("package-id"))) {
079        return o.asString("url");
080      }
081    }
082    return null;
083  }
084
085  public boolean isCurrent(String id, NpmPackage npmPackage) throws IOException {
086    checkCIServerQueried();
087    String packageManifestUrl = ciPackageUrls.get(id);
088    JsonObject packageManifestJson = JsonParser.parseObjectFromUrl(Utilities.pathURL(packageManifestUrl, "package.manifest.json"));
089    String currentDate = packageManifestJson.asString("date");
090    String packageDate = npmPackage.date();
091    return currentDate.equals(packageDate); // nup, we need a new copy
092  }
093
094  BasePackageCacheManager.InputStreamWithSrc loadFromCIBuild(String id, String branch) {
095    checkCIServerQueried();
096
097    if (ciPackageUrls.containsKey(id)) {
098      String packageBaseUrl = ciPackageUrls.get(id);
099      if (branch == null) {
100        InputStream stream;
101        try {
102          stream = fetchFromUrlSpecific(Utilities.pathURL(packageBaseUrl, "package.tgz"));
103        } catch (Exception e) {
104          stream = fetchFromUrlSpecific(Utilities.pathURL(packageBaseUrl, "branches", "main", "package.tgz"));
105        }
106        return new BasePackageCacheManager.InputStreamWithSrc(stream, Utilities.pathURL(packageBaseUrl, "package.tgz"), "current");
107      } else {
108        InputStream stream = fetchFromUrlSpecific(Utilities.pathURL(packageBaseUrl, "branches", branch, "package.tgz"));
109        return new BasePackageCacheManager.InputStreamWithSrc(stream, Utilities.pathURL(packageBaseUrl, "branches", branch, "package.tgz"), "current$" + branch);
110      }
111    } else if (id.startsWith("hl7.fhir.r6")) {
112      InputStream stream = fetchFromUrlSpecific(Utilities.pathURL(rootUrl, id + ".tgz"));
113      return new BasePackageCacheManager.InputStreamWithSrc(stream, Utilities.pathURL(rootUrl, id + ".tgz"), "current");
114    } else if (Utilities.endsWithInList(id, ".r3", ".r4", ".r4b", ".r5", ".r6")) {
115      String npid = id.substring(0, id.lastIndexOf("."));
116      String url = ciPackageUrls.get(npid);
117      if (url == null) {
118        throw new FHIRException("The package '" + id + "' has no entry on the current build server (" + ciPackageUrls + ")");        
119      } else {
120        url = Utilities.pathURL(url, id+".tgz");
121        InputStream stream = fetchFromUrlSpecific(url);
122        return new BasePackageCacheManager.InputStreamWithSrc(stream, url, "current");
123      }
124    } else {
125      throw new FHIRException("The package '" + id + "' has no entry on the current build server (" + ciPackageUrls + ")");
126    }
127  }
128
129  private InputStream fetchFromUrlSpecific(String source) throws FHIRException {
130    try {
131      HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source);
132      res.checkThrowException();
133      return new ByteArrayInputStream(res.getContent());
134    } catch (Exception e) {
135        throw new FHIRException("Unable to fetch: " + e.getMessage(), e);
136    }
137  }
138
139  private void checkCIServerQueried() {
140    if (System.currentTimeMillis() - ciLastQueriedTimeStamp > ciQueryInterval) {
141      try {
142        updateFromCIServer();
143      } catch (Exception e) {
144        try {
145          // we always pause a second and try again - the most common reason to be here is that the file was being changed on the server
146          Thread.sleep(1000);
147          updateFromCIServer();
148        } catch (Exception e2) {
149          log.debug("Error connecting to build server - running without build (" + e2.getMessage() + ")");
150        }
151      }
152    }
153  }
154
155  private void updateFromCIServer() throws IOException {
156    HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), rootUrl + "/ig/qas.json?nocache=" + System.currentTimeMillis());
157    res.checkThrowException();
158
159    ciBuildInfo = (JsonArray) JsonParser.parse(FileUtilities.bytesToString(res.getContent()));
160
161    List<BuildRecord> builds = new ArrayList<>();
162
163    for (JsonElement n : ciBuildInfo) {
164      JsonObject j = (JsonObject) n;
165      if (j.has("url") && j.has("package-id") && j.asString("package-id").contains(".")) {
166        String packageUrl = j.asString("url");
167        if (packageUrl.contains("/ImplementationGuide/"))
168          packageUrl = packageUrl.substring(0, packageUrl.indexOf("/ImplementationGuide/"));
169        builds.add(new BuildRecord(packageUrl, j.asString("package-id"), getRepo(j.asString("repo")), readDate(j.asString("date"))));
170      }
171    }
172    Collections.sort(builds, new BuildRecordSorter());
173    for (BuildRecord build : builds) {
174      if (!ciPackageUrls.containsKey(build.getPackageId())) {
175        ciPackageUrls.put(build.getPackageId(), rootUrl + "/ig/" + build.getRepo());
176      }
177    }
178    ciLastQueriedTimeStamp = System.currentTimeMillis();
179  }
180
181  private String getRepo(String path) {
182    String[] p = path.split("/");
183    return p[0] + "/" + p[1];
184  }
185
186  private Date readDate(String s) {
187    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM, yyyy HH:mm:ss Z", new Locale("en", "US"));
188    try {
189      return sdf.parse(s);
190    } catch (ParseException e) {
191      e.printStackTrace();
192      return new Date();
193    }
194  }
195
196  public static class BuildRecord {
197
198    private final String url;
199    private final String packageId;
200    private final String repo;
201    private final Date date;
202
203    public BuildRecord(String url, String packageId, String repo, Date date) {
204      super();
205      this.url = url;
206      this.packageId = packageId;
207      this.repo = repo;
208      this.date = date;
209    }
210
211    public String getUrl() {
212      return url;
213    }
214
215    public String getPackageId() {
216      return packageId;
217    }
218
219    public String getRepo() {
220      return repo;
221    }
222
223    public Date getDate() {
224      return date;
225    }
226
227  }
228
229  public static class BuildRecordSorter implements Comparator<BuildRecord> {
230
231    @Override
232    public int compare(BuildRecord arg0, BuildRecord arg1) {
233      return arg1.date.compareTo(arg0.date);
234    }
235  }
236}