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