001package org.hl7.fhir.utilities.cache;
002
003import org.apache.commons.lang3.Validate;
004import org.hl7.fhir.exceptions.FHIRException;
005import org.hl7.fhir.utilities.Utilities;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009import javax.annotation.Nonnull;
010import javax.annotation.Nullable;
011import java.io.IOException;
012import java.io.InputStream;
013import java.util.ArrayList;
014import java.util.List;
015import java.util.function.Function;
016
017public abstract class BasePackageCacheManager implements IPackageCacheManager {
018  private static final Logger ourLog = LoggerFactory.getLogger(BasePackageCacheManager.class);
019  private List<String> myPackageServers = new ArrayList<>();
020  private Function<String, PackageClient> myClientFactory = address -> new CachingPackageClient(address);
021
022  /**
023   * Constructor
024   */
025  public BasePackageCacheManager() {
026    super();
027  }
028
029  /**
030   * Provide a new client factory implementation
031   */
032  public void setClientFactory(Function<String, PackageClient> theClientFactory) {
033    Validate.notNull(theClientFactory, "theClientFactory must not be null");
034    myClientFactory = theClientFactory;
035  }
036
037  public List<String> getPackageServers() {
038    return myPackageServers;
039  }
040
041  /**
042   * Add a package server that can be used to fetch remote packages
043   */
044  public void addPackageServer(@Nonnull String thePackageServer) {
045    Validate.notBlank(thePackageServer, "thePackageServer must not be null or empty");
046    if (!myPackageServers.contains(thePackageServer)) {
047      myPackageServers.add(thePackageServer);
048    }
049  }
050
051
052  /**
053   * Load the latest version of the identified package from the cache - it it exists
054   */
055  public NpmPackage loadPackageFromCacheOnly(String id) throws IOException {
056    return loadPackageFromCacheOnly(id, null);
057  }
058
059  /**
060   * Try to load a package from all registered package servers, and return <code>null</code>
061   * if it can not be found at any of them.
062   */
063  @Nullable
064  protected InputStreamWithSrc loadFromPackageServer(String id, String version) {
065
066    for (String nextPackageServer : getPackageServers()) {
067      PackageClient packageClient = myClientFactory.apply(nextPackageServer);
068      try {
069        if (Utilities.noString(version)) {
070          version = packageClient.getLatestVersion(id);
071        }
072        InputStream stream = packageClient.fetch(id, version);
073        String url = packageClient.url(id, version);
074        return new InputStreamWithSrc(stream, url, version);
075      } catch (IOException e) {
076        ourLog.info("Failed to resolve package {}#{} from server: {}", id, version, nextPackageServer);
077      }
078
079    }
080
081    return null;
082  }
083
084  public abstract NpmPackage loadPackageFromCacheOnly(String id, @Nullable String version) throws IOException;
085
086  @Override
087  public String getPackageUrl(String packageId) throws IOException {
088    String result = null;
089    NpmPackage npm = loadPackageFromCacheOnly(packageId);
090    if (npm != null) {
091      return npm.canonical();
092    }
093
094    for (String nextPackageServer : getPackageServers()) {
095      result = getPackageUrl(packageId, nextPackageServer);
096      if (result != null) {
097        return result;
098      }
099    }
100
101    return result;
102  }
103
104
105  private String getPackageUrl(String packageId, String server) throws IOException {
106    PackageClient pc = myClientFactory.apply(server);
107    List<PackageClient.PackageInfo> res = pc.search(packageId, null, null, false);
108    if (res.size() == 0) {
109      return null;
110    } else {
111      return res.get(0).getUrl();
112    }
113  }
114
115
116  @Override
117  public String getPackageId(String canonicalUrl) throws IOException {
118    String result = null;
119
120    for (String nextPackageServer : getPackageServers()) {
121      result = getPackageId(canonicalUrl, nextPackageServer);
122      if (result != null) {
123        break;
124      }
125    }
126
127    return result;
128  }
129
130  private String getPackageId(String canonical, String server) throws IOException {
131    if (canonical == null) {
132      return null;
133    }
134    PackageClient pc = myClientFactory.apply(server);
135    List<PackageClient.PackageInfo> res = pc.search(null, canonical, null, false);
136    if (res.size() == 0) {
137      return null;
138    } else {
139      // this is driven by HL7 Australia (http://hl7.org.au/fhir/ is the canonical url for the base package, and the root for all the others)
140      for (PackageClient.PackageInfo pi : res) {
141        if (canonical.equals(pi.getCanonical())) {
142          return pi.getId();
143        }
144      }
145      return res.get(0).getId();
146    }
147  }
148
149  public class InputStreamWithSrc {
150
151    public InputStream stream;
152    public String url;
153    public String version;
154
155    public InputStreamWithSrc(InputStream stream, String url, String version) {
156      this.stream = stream;
157      this.url = url;
158      this.version = version;
159    }
160  }
161
162  public NpmPackage loadPackage(String idAndVer) throws FHIRException, IOException {
163    return loadPackage(idAndVer, null);
164  }
165
166}