001package org.hl7.fhir.utilities.settings;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.IOException;
006import java.io.InputStream;
007import java.util.Collections;
008import java.util.List;
009
010import org.hl7.fhir.utilities.Utilities;
011import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
012
013import com.fasterxml.jackson.databind.DeserializationFeature;
014import com.fasterxml.jackson.databind.ObjectMapper;
015
016
017public class FhirSettings {
018
019  
020  public static final String FHIR_SETTINGS_PATH = "fhir.settings.path";
021  private static String explicitFilePath = null;
022  private static Boolean prohibitNetworkAccess;
023
024  public static void setExplicitFilePath(String explicitFilePath) {
025    if (instance != null) {
026      throw new IllegalStateException("Attempted to set explicitFilePath after FhirSettings has already been initialized with path: " + instance.filePath);
027    }
028    if (FhirSettings.explicitFilePath != null) {
029      throw new IllegalStateException("Attempted to set explicitFilePath to " + explicitFilePath + " when already set to " + FhirSettings.explicitFilePath);
030    }
031    FhirSettings.explicitFilePath = explicitFilePath;
032  }
033  private final FhirSettingsPOJO fhirSettings;
034
035  public static String getFilePath() {
036    getInstance();
037    return instance.filePath;
038  }
039
040  final String filePath;
041  private FhirSettings(FhirSettingsPOJO fhirSettings, String filePath) {
042
043    this.fhirSettings = fhirSettings;
044    this.filePath = filePath;
045  }
046
047  public static String getApiKey(String key) {
048    getInstance();
049    return instance.fhirSettings.getApiKeys() != null
050      ? instance.fhirSettings.getApiKeys().get(key)
051      : null;
052  }
053
054  public static boolean hasApiKey(String key) {
055    getInstance();
056    return instance.fhirSettings.getApiKeys() != null
057      ? instance.fhirSettings.getApiKeys().get(key) != null
058      : false;
059  }
060
061  public static String getNpmPath() {
062    getInstance();
063    return instance.fhirSettings.getNpmPath();
064  }
065
066  public static boolean hasNpmPath() {
067    getInstance();
068    return instance.fhirSettings.getNpmPath() != null;
069  }
070  public static boolean hasRubyPath() {
071    getInstance();
072    return instance.fhirSettings.getRubyPath() != null;
073  }
074
075  public static String getRubyPath() {
076    getInstance();
077    return instance.fhirSettings.getRubyPath();
078  }
079
080  public static boolean hasFhirTestCasesPath() {
081    getInstance();
082    return instance.fhirSettings.getFhirTestCasesPath() != null;
083  }
084
085  public static String getFhirTestCasesPath() {
086    getInstance();
087    return instance.fhirSettings.getFhirTestCasesPath();
088  }
089
090  public static boolean hasDiffToolPath() {
091    getInstance();
092    return instance.fhirSettings.getDiffToolPath() != null;
093  }
094
095  public static String getDiffToolPath() {
096    getInstance();
097    return instance.fhirSettings.getDiffToolPath();
098  }
099
100  public static boolean hasTempPath() {
101    getInstance();
102    return instance.fhirSettings.getTempPath() != null;
103  }
104
105  public static String getTempPath() {
106    getInstance();
107    return instance.fhirSettings.getTempPath();
108  }
109
110  public static boolean hasTestIGsPath() {
111    getInstance();
112    return instance.fhirSettings.getTestIgsPath() != null;
113  }
114
115  public static String getTestIgsPath() {
116    getInstance();
117    return instance.fhirSettings.getTestIgsPath();
118  }
119
120  public static boolean hasProhibitNetworkAccess() {
121    if (prohibitNetworkAccess != null) {
122      return true;
123    }
124    getInstance();
125    return instance.fhirSettings.getProhibitNetworkAccess() != null; 
126  }
127
128  public static boolean isProhibitNetworkAccess() {
129    if (prohibitNetworkAccess != null) {
130      return prohibitNetworkAccess;
131    }
132    getInstance();
133    return instance.fhirSettings.getProhibitNetworkAccess() == null
134      ? false
135      : instance.fhirSettings.getProhibitNetworkAccess(); 
136  }
137  
138  /**
139   * See ManagedWebAccess and use that to control network access
140   * 
141   * @param value
142   */
143  @Deprecated
144  public static void setProhibitNetworkAccess(boolean value) {
145    prohibitNetworkAccess = value;
146  }
147
148
149  public static String getTxFhirProduction() {
150    getInstance();
151    return instance.fhirSettings.getTxFhirProduction() == null
152      ? FhirSettingsPOJO.TX_SERVER_PROD
153      : instance.fhirSettings.getTxFhirProduction(); 
154  }
155
156  public static String getTxFhirDevelopment() {
157    getInstance();
158    return instance.fhirSettings.getTxFhirDevelopment() == null
159      ? FhirSettingsPOJO.TX_SERVER_DEV
160      : instance.fhirSettings.getTxFhirDevelopment();
161  }
162
163  public static String getTxFhirLocal() {
164    getInstance();
165    return instance.fhirSettings.getTxFhirLocal() == null
166      ? FhirSettingsPOJO.TX_SERVER_LOCAL
167      : instance.fhirSettings.getTxFhirLocal();
168  }
169  
170  private static FhirSettings instance = null;
171
172  private static FhirSettings getInstance()  {
173    if (instance == null) {
174      try {
175        instance = getInstanceFromPath(explicitFilePath);
176      } catch (IOException e) {
177        e.printStackTrace();
178        instance = new FhirSettings(new FhirSettingsPOJO(), null);
179      }
180    }
181    return instance;
182  }
183
184  protected static FhirSettings getInstanceFromPath(String explicitFilePath) throws IOException {
185    final String filePath;
186    filePath = getSettingsFilePath(explicitFilePath);
187    return new FhirSettings(getFhirSettingsPOJO(filePath), filePath);
188  }
189
190  protected static String getSettingsFilePath(String explicitFilePath) {
191    final String filePath;
192    String pathFromSystemProperties;
193    try {
194      pathFromSystemProperties = getDefaultSettingsPath();
195    } catch (IOException e) {
196      pathFromSystemProperties = null;
197    }
198    filePath = explicitFilePath != null ? explicitFilePath :
199      System.getProperty(FHIR_SETTINGS_PATH, pathFromSystemProperties);
200    return filePath;
201  }
202
203  static FhirSettingsPOJO getFhirSettingsPOJO(String filePath) throws IOException {
204    final File file = ManagedFileAccess.file(filePath);
205
206    if (!file.exists()) {
207      return new FhirSettingsPOJO();
208    }
209
210    final ObjectMapper objectMapper = new ObjectMapper();
211    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212    final InputStream inputStream = ManagedFileAccess.inStream(file);
213    final FhirSettingsPOJO output = objectMapper.readValue(inputStream, FhirSettingsPOJO.class);
214
215    return output;
216  }
217
218  protected static String getDefaultSettingsPath() throws IOException {
219    return Utilities.path(System.getProperty("user.home"), ".fhir", "fhir-settings.json");
220  }
221
222  public static boolean isIgnoreDefaultPackageServers() {
223    getInstance();
224    if (instance.fhirSettings.getPackageManagement() == null || instance.fhirSettings.getPackageManagement().getIgnoreDefaultServers() == null) {
225      return false;
226    }
227    return instance.fhirSettings.getPackageManagement().getIgnoreDefaultServers();
228  }
229
230  public static List<ServerDetailsPOJO> getPackageServers() {
231    getInstance();
232    if (instance.fhirSettings.getPackageManagement() == null) {
233      return Collections.emptyList();
234    }
235    return List.of(instance.fhirSettings.getPackageManagement().getServers().toArray(new ServerDetailsPOJO[]{}));
236  }
237}