Files
hapi-fhir-jpaserver-starter/src/main/java/ca/uhn/fhir/jpa/starter/HapiProperties.java
Sean McIlvenna 269dd77351 Setting properties for ModelConfig in addition to DaoConfig
Setting properties for handling Email via the emailSender() bean
2019-02-12 14:08:07 -08:00

274 lines
9.6 KiB
Java

package ca.uhn.fhir.jpa.starter;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.rest.server.ETagSupportEnum;
import com.google.common.annotations.VisibleForTesting;
import java.io.InputStream;
import java.util.Properties;
public class HapiProperties {
static final String ALLOW_EXTERNAL_REFERENCES = "allow_external_references";
static final String ALLOW_MULTIPLE_DELETE = "allow_multiple_delete";
static final String ALLOW_PLACEHOLDER_REFERENCES = "allow_placeholder_references";
static final String DATASOURCE_DRIVER = "datasource.driver";
static final String DATASOURCE_PASSWORD = "datasource.password";
static final String DATASOURCE_URL = "datasource.url";
static final String DATASOURCE_USERNAME = "datasource.username";
static final String DEFAULT_ENCODING = "default_encoding";
static final String DEFAULT_PAGE_SIZE = "default_page_size";
static final String DEFAULT_PRETTY_PRINT = "default_pretty_print";
static final String ETAG_SUPPORT = "etag_support";
static final String FHIR_VERSION = "fhir_version";
static final String HAPI_PROPERTIES = "hapi.properties";
static final String LOGGER_ERROR_FORMAT = "logger.error_format";
static final String LOGGER_FORMAT = "logger.format";
static final String LOGGER_LOG_EXCEPTIONS = "logger.log_exceptions";
static final String LOGGER_NAME = "logger.name";
static final String MAX_PAGE_SIZE = "max_page_size";
static final String PERSISTENCE_UNIT_NAME = "persistence_unit_name";
static final String SERVER_ADDRESS = "server_address";
static final String SERVER_BASE = "server.base";
static final String SERVER_ID = "server.id";
static final String SERVER_NAME = "server.name";
static final String SUBSCRIPTION_EMAIL_ENABLED = "subscription.email.enabled";
static final String SUBSCRIPTION_RESTHOOK_ENABLED = "subscription.resthook.enabled";
static final String TEST_PORT = "test.port";
static final String ALLOW_CONTAINS_SEARCHES = "allow_contains_searches";
static final String ALLOW_OVERRIDE_DEFAULT_SEARCH_PARAMS = "allow_override_default_search_params";
static final String EMAIL_FROM = "email.from";
private static Properties properties;
/*
* Force the configuration to be reloaded
*/
public static void forceReload() {
properties = null;
getProperties();
}
/**
* This is mostly here for unit tests. Use the actual properties file
* to set values
*/
@VisibleForTesting
public static void setProperty(String theKey, String theValue) {
getProperties().setProperty(theKey, theValue);
}
public static Properties getProperties() {
if (properties == null) {
// Load the configurable properties file
try (InputStream in = HapiProperties.class.getClassLoader().getResourceAsStream(HAPI_PROPERTIES)){
HapiProperties.properties = new Properties();
HapiProperties.properties.load(in);
} catch (Exception e) {
throw new ConfigurationException("Could not load HAPI properties", e);
}
}
return properties;
}
private static String getProperty(String propertyName) {
Properties properties = HapiProperties.getProperties();
if (properties != null) {
return properties.getProperty(propertyName);
}
return null;
}
private static String getProperty(String propertyName, String defaultValue) {
Properties properties = HapiProperties.getProperties();
if (properties != null) {
String value = properties.getProperty(propertyName);
if (value != null && value.length() > 0) {
return value;
}
}
return defaultValue;
}
private static Boolean getBooleanProperty(String propertyName, Boolean defaultValue) {
String value = HapiProperties.getProperty(propertyName);
if (value == null || value.length() == 0) {
return defaultValue;
}
return Boolean.parseBoolean(value);
}
private static Integer getIntegerProperty(String propertyName, Integer defaultValue) {
String value = HapiProperties.getProperty(propertyName);
if (value == null || value.length() == 0) {
return defaultValue;
}
return Integer.parseInt(value);
}
public static FhirVersionEnum getFhirVersion() {
String fhirVersionString = HapiProperties.getProperty(FHIR_VERSION);
if (fhirVersionString != null && fhirVersionString.length() > 0) {
return FhirVersionEnum.valueOf(fhirVersionString);
}
return FhirVersionEnum.DSTU3;
}
public static ETagSupportEnum getEtagSupport() {
String etagSupportString = HapiProperties.getProperty(ETAG_SUPPORT);
if (etagSupportString != null && etagSupportString.length() > 0) {
return ETagSupportEnum.valueOf(etagSupportString);
}
return ETagSupportEnum.ENABLED;
}
public static EncodingEnum getDefaultEncoding() {
String defaultEncodingString = HapiProperties.getProperty(DEFAULT_ENCODING);
if (defaultEncodingString != null && defaultEncodingString.length() > 0) {
return EncodingEnum.valueOf(defaultEncodingString);
}
return EncodingEnum.JSON;
}
public static Boolean getDefaultPrettyPrint() {
return HapiProperties.getBooleanProperty(DEFAULT_PRETTY_PRINT, true);
}
public static String getServerAddress() {
return HapiProperties.getProperty(SERVER_ADDRESS);
}
public static Integer getDefaultPageSize() {
return HapiProperties.getIntegerProperty(DEFAULT_PAGE_SIZE, 20);
}
public static Integer getMaximumPageSize() {
return HapiProperties.getIntegerProperty(MAX_PAGE_SIZE, 200);
}
public static String getPersistenceUnitName() {
return HapiProperties.getProperty(PERSISTENCE_UNIT_NAME, "HAPI_PU");
}
public static String getLoggerName() {
return HapiProperties.getProperty(LOGGER_NAME, "fhirtest.access");
}
public static String getLoggerFormat() {
return HapiProperties.getProperty(LOGGER_FORMAT, "Path[${servletPath}] Source[${requestHeader.x-forwarded-for}] Operation[${operationType} ${operationName} ${idOrResourceName}] UA[${requestHeader.user-agent}] Params[${requestParameters}] ResponseEncoding[${responseEncodingNoDefault}]");
}
public static String getLoggerErrorFormat() {
return HapiProperties.getProperty(LOGGER_ERROR_FORMAT, "ERROR - ${requestVerb} ${requestUrl}");
}
public static Boolean getLoggerLogExceptions() {
return HapiProperties.getBooleanProperty(LOGGER_LOG_EXCEPTIONS, true);
}
public static String getDataSourceDriver() {
return HapiProperties.getProperty(DATASOURCE_DRIVER, "org.apache.derby.jdbc.EmbeddedDriver");
}
public static String getDataSourceUrl() {
return HapiProperties.getProperty(DATASOURCE_URL, "jdbc:derby:directory:target/jpaserver_derby_files;create=true");
}
public static String getDataSourceUsername() {
return HapiProperties.getProperty(DATASOURCE_USERNAME);
}
public static String getDataSourcePassword() {
return HapiProperties.getProperty(DATASOURCE_PASSWORD);
}
public static Boolean getAllowMultipleDelete() {
return HapiProperties.getBooleanProperty(ALLOW_MULTIPLE_DELETE, true);
}
public static Boolean getAllowExternalReferences() {
return HapiProperties.getBooleanProperty(ALLOW_EXTERNAL_REFERENCES, true);
}
public static Boolean getExpungeEnabled() {
return HapiProperties.getBooleanProperty("expunge_enabled", true);
}
public static Integer getTestPort() {
return HapiProperties.getIntegerProperty(TEST_PORT, 0);
}
public static String getServerBase() {
return HapiProperties.getProperty(SERVER_BASE, "/fhir");
}
public static String getServerName() {
return HapiProperties.getProperty(SERVER_NAME, "Local Tester");
}
public static String getServerId() {
return HapiProperties.getProperty(SERVER_ID, "home");
}
public static Boolean getAllowPlaceholderReferences() {
return HapiProperties.getBooleanProperty(ALLOW_PLACEHOLDER_REFERENCES, true);
}
public static Boolean getSubscriptionEmailEnabled() {
return HapiProperties.getBooleanProperty(SUBSCRIPTION_EMAIL_ENABLED, true);
}
public static Boolean getSubscriptionRestHookEnabled() {
return HapiProperties.getBooleanProperty(SUBSCRIPTION_RESTHOOK_ENABLED, true);
}
public static Boolean getAllowContainsSearches() {
return HapiProperties.getBooleanProperty(ALLOW_CONTAINS_SEARCHES, true);
}
public static Boolean getAllowOverrideDefaultSearchParams() {
return HapiProperties.getBooleanProperty(ALLOW_OVERRIDE_DEFAULT_SEARCH_PARAMS, true);
}
public static String getEmailFrom() {
return HapiProperties.getProperty(EMAIL_FROM, "some@test.com");
}
public static Boolean getEmailEnabled() {
return HapiProperties.getBooleanProperty("email.enabled", false);
}
public static String getEmailHost() {
return HapiProperties.getProperty("email.host");
}
public static Integer getEmailPort() {
return HapiProperties.getIntegerProperty("email.port", 0);
}
public static String getEmailUsername() {
return HapiProperties.getProperty("email.username");
}
public static String getEmailPassword() {
return HapiProperties.getProperty("email.password");
}
}