
Class FhirContext
- java.lang.Object
-
- ca.uhn.fhir.context.FhirContext
-
public class FhirContext extends Object
The FHIR context is the central starting point for the use of the HAPI FHIR API. It should be created once, and then used as a factory for various other types of objects (parsers, clients, etc.).Important usage notes:
-
Thread safety: This class is thread safe and may be shared between multiple processing
threads, except for the
registerCustomType(java.lang.Class<? extends org.hl7.fhir.instance.model.api.IBase>)
andregisterCustomTypes(java.util.Collection<java.lang.Class<? extends org.hl7.fhir.instance.model.api.IBase>>)
methods. - Performance: This class is expensive to create, as it scans every resource class it needs to parse or encode to build up an internal model of those classes. For that reason, you should try to create one FhirContext instance which remains for the life of your application and reuse that instance. Note that it will not cause problems to create multiple instances (ie. resources originating from one FhirContext may be passed to parsers originating from another) but you will incur a performance penalty if a new FhirContext is created for every message you parse/encode.
-
Thread safety: This class is thread safe and may be shared between multiple processing
threads, except for the
-
-
Constructor Summary
Constructors Constructor Description FhirContext()
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.FhirContext(FhirVersionEnum theVersion)
In most cases it is recommended that you use one of the static initializer methods instead of this method, e.g.FhirContext(Class<?>... theResourceTypes)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.FhirContext(Class<? extends IBaseResource> theResourceType)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.FhirContext(Collection<Class<? extends IBaseResource>> theResourceTypes)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static FhirContext
forCached(FhirVersionEnum theFhirVersionEnum)
Returns a statically cached FhirContext instance for the given version, creating one if none exists in the cache.static FhirContext
forDstu2()
Creates and returns a new FhirContext with versionDSTU2
static FhirContext
forDstu2_1()
Creates and returns a new FhirContext with versionDSTU2
(2016 May DSTU3 Snapshot)static FhirContext
forDstu2Hl7Org()
Creates and returns a new FhirContext with versionDSTU2
(using the Reference Implementation Structures)static FhirContext
forDstu3()
Creates and returns a new FhirContext with versionDSTU3
static FhirContext
forR4()
Creates and returns a new FhirContext with versionR4
static FhirContext
forR5()
Creates and returns a new FhirContext with versionR5
AddProfileTagEnum
getAddProfileTagWhenEncoding()
When encoding resources, this setting configures the parser to include an entry in the resource's metadata section which indicates which profile(s) the resource claims to conform to.Class<? extends IBaseResource>
getDefaultTypeForProfile(String theProfile)
Returns the default resource type for the given profileBaseRuntimeElementDefinition<?>
getElementDefinition(Class<? extends IBase> theElementType)
Returns the scanned runtime model for the given type.BaseRuntimeElementDefinition<?>
getElementDefinition(String theElementName)
Returns the scanned runtime model for the given type.Collection<BaseRuntimeElementDefinition<?>>
getElementDefinitions()
Returns all element definitions (resources, datatypes, etc.)HapiLocalizer
getLocalizer()
This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with cautionINarrativeGenerator
getNarrativeGenerator()
ParserOptions
getParserOptions()
Returns the parser options object which will be used to supply default options to newly created parsersSet<PerformanceOptionsEnum>
getPerformanceOptions()
Get the configured performance optionsRuntimeResourceDefinition
getResourceDefinition(FhirVersionEnum theVersion, String theResourceName)
RuntimeResourceDefinition
getResourceDefinition(Class<? extends IBaseResource> theResourceType)
Returns the scanned runtime model for the given type.RuntimeResourceDefinition
getResourceDefinition(String theResourceName)
RuntimeResourceDefinition
getResourceDefinition(IBaseResource theResource)
Returns the scanned runtime model for the given type.RuntimeResourceDefinition
getResourceDefinitionById(String theId)
Returns the scanned runtime model for the given type.Collection<RuntimeResourceDefinition>
getResourceDefinitionsWithExplicitId()
Returns the scanned runtime models.String
getResourceType(Class<? extends IBaseResource> theResourceType)
Returns the name of a given resource class.String
getResourceType(String theResourceName)
String
getResourceType(IBaseResource theResource)
Returns the name of the scanned runtime model for the given type.Set<String>
getResourceTypes()
Returns an unmodifiable set containing all resource names known to this contextIRestfulClientFactory
getRestfulClientFactory()
Get the restful client factory.RuntimeChildUndeclaredExtensionDefinition
getRuntimeChildUndeclaredExtensionDefinition()
IValidationSupport
getValidationSupport()
Returns the validation support module configured for this context, creating a default implementation if no module has been passed in via thesetValidationSupport(IValidationSupport)
methodIFhirVersion
getVersion()
boolean
hasDefaultTypeForProfile()
Returnstrue
if any default types for specific profiles have been defined within this context.IVersionSpecificBundleFactory
newBundleFactory()
IFhirPath
newFhirPath()
Creates a new FhirPath engine which can be used to evaluate path expressions over FHIR resources.IFhirPath
newFluentPath()
Deprecated.Deprecated in HAPI FHIR 5.0.0.IParser
newJsonParser()
Create and return a new JSON parser.IParser
newRDFParser()
Create and return a new RDF parser.<T extends IRestfulClient>
TnewRestfulClient(Class<T> theClientType, String theServerBase)
Instantiates a new client instance.IGenericClient
newRestfulGenericClient(String theServerBase)
Instantiates a new generic client.FhirTerser
newTerser()
FhirValidator
newValidator()
Create a new validator instance.ViewGenerator
newViewGenerator()
IParser
newXmlParser()
Create and return a new XML parser.void
registerCustomType(Class<? extends IBase> theType)
This method may be used to register a custom resource or datatype.void
registerCustomTypes(Collection<Class<? extends IBase>> theTypes)
This method may be used to register a custom resource or datatype.void
setAddProfileTagWhenEncoding(AddProfileTagEnum theAddProfileTagWhenEncoding)
When encoding resources, this setting configures the parser to include an entry in the resource's metadata section which indicates which profile(s) the resource claims to conform to.void
setDefaultTypeForProfile(String theProfile, Class<? extends IBaseResource> theClass)
Sets the default type which will be used when parsing a resource that is found to be of the given profile.void
setLocalizer(HapiLocalizer theMessages)
This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with cautionvoid
setNarrativeGenerator(INarrativeGenerator theNarrativeGenerator)
void
setParserErrorHandler(IParserErrorHandler theParserErrorHandler)
Sets a parser error handler to use by default on all parsersvoid
setParserOptions(ParserOptions theParserOptions)
Sets the parser options object which will be used to supply default options to newly created parsersvoid
setPerformanceOptions(PerformanceOptionsEnum... thePerformanceOptions)
Sets the configured performance optionsvoid
setPerformanceOptions(Collection<PerformanceOptionsEnum> theOptions)
Sets the configured performance optionsvoid
setRestfulClientFactory(IRestfulClientFactory theRestfulClientFactory)
Set the restful client factoryvoid
setValidationSupport(IValidationSupport theValidationSupport)
Sets the validation support module to use for this context.String
toString()
-
-
-
Constructor Detail
-
FhirContext
@Deprecated public FhirContext()
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.forDstu2()
orforDstu3()
orforR4()
-
FhirContext
@Deprecated public FhirContext(Class<? extends IBaseResource> theResourceType)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.forDstu2()
orforDstu3()
orforR4()
-
FhirContext
@Deprecated public FhirContext(Class<?>... theResourceTypes)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.forDstu2()
orforDstu3()
orforR4()
-
FhirContext
@Deprecated public FhirContext(Collection<Class<? extends IBaseResource>> theResourceTypes)
Deprecated.It is recommended that you use one of the static initializer methods instead of this method, e.g.forDstu2()
orforDstu3()
orforR4()
-
FhirContext
public FhirContext(FhirVersionEnum theVersion)
In most cases it is recommended that you use one of the static initializer methods instead of this method, e.g.forDstu2()
orforDstu3()
orforR4()
, but this method can also be used if you wish to supply the version programmatically.
-
-
Method Detail
-
getAddProfileTagWhenEncoding
public AddProfileTagEnum getAddProfileTagWhenEncoding()
When encoding resources, this setting configures the parser to include an entry in the resource's metadata section which indicates which profile(s) the resource claims to conform to. The default isAddProfileTagEnum.ONLY_FOR_CUSTOM
.- See Also:
for more information
-
setAddProfileTagWhenEncoding
public void setAddProfileTagWhenEncoding(AddProfileTagEnum theAddProfileTagWhenEncoding)
When encoding resources, this setting configures the parser to include an entry in the resource's metadata section which indicates which profile(s) the resource claims to conform to. The default isAddProfileTagEnum.ONLY_FOR_CUSTOM
.This feature is intended for situations where custom resource types are being used, avoiding the need to manually add profile declarations for these custom types.
See Profiling and Extensions for more information on using custom types.
Note that this feature automatically adds the profile, but leaves any profile tags which have been manually added in place as well.
- Parameters:
theAddProfileTagWhenEncoding
- The add profile mode (must not benull
)
-
getDefaultTypeForProfile
public Class<? extends IBaseResource> getDefaultTypeForProfile(String theProfile)
Returns the default resource type for the given profile- See Also:
setDefaultTypeForProfile(String, Class)
-
getElementDefinition
public BaseRuntimeElementDefinition<?> getElementDefinition(Class<? extends IBase> theElementType)
Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
-
getElementDefinition
@Nullable public BaseRuntimeElementDefinition<?> getElementDefinition(String theElementName)
Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.Note that this method is case insensitive!
-
getElementDefinitions
public Collection<BaseRuntimeElementDefinition<?>> getElementDefinitions()
Returns all element definitions (resources, datatypes, etc.)
-
getLocalizer
public HapiLocalizer getLocalizer()
This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with caution
-
setLocalizer
public void setLocalizer(HapiLocalizer theMessages)
This feature is not yet in its final state and should be considered an internal part of HAPI for now - use with caution
-
getNarrativeGenerator
public INarrativeGenerator getNarrativeGenerator()
-
setNarrativeGenerator
public void setNarrativeGenerator(INarrativeGenerator theNarrativeGenerator)
-
getParserOptions
public ParserOptions getParserOptions()
Returns the parser options object which will be used to supply default options to newly created parsers- Returns:
- The parser options - Will not return
null
-
setParserOptions
public void setParserOptions(ParserOptions theParserOptions)
Sets the parser options object which will be used to supply default options to newly created parsers- Parameters:
theParserOptions
- The parser options object - Must not benull
-
getPerformanceOptions
public Set<PerformanceOptionsEnum> getPerformanceOptions()
Get the configured performance options
-
setPerformanceOptions
public void setPerformanceOptions(Collection<PerformanceOptionsEnum> theOptions)
Sets the configured performance options- See Also:
for a list of available options
-
setPerformanceOptions
public void setPerformanceOptions(PerformanceOptionsEnum... thePerformanceOptions)
Sets the configured performance options- See Also:
for a list of available options
-
getResourceDefinition
public RuntimeResourceDefinition getResourceDefinition(Class<? extends IBaseResource> theResourceType)
Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
-
getResourceDefinition
public RuntimeResourceDefinition getResourceDefinition(FhirVersionEnum theVersion, String theResourceName)
-
getResourceDefinition
public RuntimeResourceDefinition getResourceDefinition(IBaseResource theResource)
Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
-
getResourceType
public String getResourceType(Class<? extends IBaseResource> theResourceType)
Returns the name of a given resource class.
-
getResourceType
public String getResourceType(IBaseResource theResource)
Returns the name of the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
-
getResourceType
public String getResourceType(String theResourceName) throws DataFormatException
- Throws:
DataFormatException
-
getResourceDefinition
public RuntimeResourceDefinition getResourceDefinition(String theResourceName) throws DataFormatException
- Throws:
DataFormatException
-
getResourceDefinitionById
public RuntimeResourceDefinition getResourceDefinitionById(String theId)
Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
-
getResourceDefinitionsWithExplicitId
public Collection<RuntimeResourceDefinition> getResourceDefinitionsWithExplicitId()
Returns the scanned runtime models. This is an advanced feature which is generally only needed for extending the core library.
-
getResourceTypes
public Set<String> getResourceTypes()
Returns an unmodifiable set containing all resource names known to this context- Since:
- 5.1.0
-
getRestfulClientFactory
public IRestfulClientFactory getRestfulClientFactory()
Get the restful client factory. If no factory has been set, this will be initialized with a new ApacheRestfulClientFactory.- Returns:
- the factory used to create the restful clients
-
setRestfulClientFactory
public void setRestfulClientFactory(IRestfulClientFactory theRestfulClientFactory)
Set the restful client factory- Parameters:
theRestfulClientFactory
- The new client factory (must not be null)
-
getRuntimeChildUndeclaredExtensionDefinition
public RuntimeChildUndeclaredExtensionDefinition getRuntimeChildUndeclaredExtensionDefinition()
-
getValidationSupport
public IValidationSupport getValidationSupport()
Returns the validation support module configured for this context, creating a default implementation if no module has been passed in via thesetValidationSupport(IValidationSupport)
method- See Also:
setValidationSupport(IValidationSupport)
-
setValidationSupport
public void setValidationSupport(IValidationSupport theValidationSupport)
Sets the validation support module to use for this context. The validation support module is used to supply underlying infrastructure such as conformance resources (StructureDefinition, ValueSet, etc) as well as to provide terminology services to modules such as the validator and FluentPath executor
-
getVersion
public IFhirVersion getVersion()
-
hasDefaultTypeForProfile
public boolean hasDefaultTypeForProfile()
Returnstrue
if any default types for specific profiles have been defined within this context.
-
newBundleFactory
public IVersionSpecificBundleFactory newBundleFactory()
-
newFluentPath
@Deprecated public IFhirPath newFluentPath()
Deprecated.Deprecated in HAPI FHIR 5.0.0. UsenewFhirPath()
instead.- Since:
- 2.2
-
newFhirPath
public IFhirPath newFhirPath()
Creates a new FhirPath engine which can be used to evaluate path expressions over FHIR resources. Note that this engine will use thecontext validation support
module which is configured on the context at the time this method is called.In other words, you may wish to call
setValidationSupport(IValidationSupport)
before callingnewFluentPath()
Note that this feature was added for FHIR DSTU3 and is not available for contexts configured to use an older version of FHIR. Calling this method on a context for a previous version of fhir will result in an
UnsupportedOperationException
- Since:
- 5.0.0
-
newJsonParser
public IParser newJsonParser()
Create and return a new JSON parser.Thread safety: Parsers are not guaranteed to be thread safe. Create a new parser instance for every thread or every message being parsed/encoded.
Performance Note: This method is cheap to call, and may be called once for every message being processed without incurring any performance penalty
-
newRDFParser
public IParser newRDFParser()
Create and return a new RDF parser.Thread safety: Parsers are not guaranteed to be thread safe. Create a new parser instance for every thread or every message being parsed/encoded.
Performance Note: This method is cheap to call, and may be called once for every message being processed without incurring any performance penalty
-
newRestfulClient
public <T extends IRestfulClient> T newRestfulClient(Class<T> theClientType, String theServerBase)
Instantiates a new client instance. This method requires an interface which is defined specifically for your use cases to contain methods for each of the RESTful operations you wish to implement (e.g. "read ImagingStudy", "search Patient by identifier", etc.). This interface must extendIRestfulClient
(or commonly its sub-interfaceIBasicClient
). See the RESTful Client documentation for more information on how to define this interface.Performance Note: This method is cheap to call, and may be called once for every operation invocation without incurring any performance penalty
- Parameters:
theClientType
- The client type, which is an interface type to be instantiatedtheServerBase
- The URL of the base for the restful FHIR server to connect to- Returns:
- A newly created client
- Throws:
ConfigurationException
- If the interface type is not an interface
-
newRestfulGenericClient
public IGenericClient newRestfulGenericClient(String theServerBase)
Instantiates a new generic client. A generic client is able to perform any of the FHIR RESTful operations against a compliant server, but does not have methods defining the specific functionality required (as is the case withnon-generic clients
).Performance Note: This method is cheap to call, and may be called once for every operation invocation without incurring any performance penalty
- Parameters:
theServerBase
- The URL of the base for the restful FHIR server to connect to
-
newTerser
public FhirTerser newTerser()
-
newValidator
public FhirValidator newValidator()
Create a new validator instance.Note on thread safety: Validators are thread safe, you may use a single validator in multiple threads. (This is in contrast to parsers)
-
newViewGenerator
public ViewGenerator newViewGenerator()
-
newXmlParser
public IParser newXmlParser()
Create and return a new XML parser.Thread safety: Parsers are not guaranteed to be thread safe. Create a new parser instance for every thread or every message being parsed/encoded.
Performance Note: This method is cheap to call, and may be called once for every message being processed without incurring any performance penalty
-
registerCustomType
public void registerCustomType(Class<? extends IBase> theType)
This method may be used to register a custom resource or datatype. Note that by using custom types, you are creating a system that will not interoperate with other systems that do not know about your custom type. There are valid reasons however for wanting to create custom types and this method can be used to enable them.THREAD SAFETY WARNING: This method is not thread safe. It should be called before any threads are able to call any methods on this context.
- Parameters:
theType
- The custom type to add (must not benull
)
-
registerCustomTypes
public void registerCustomTypes(Collection<Class<? extends IBase>> theTypes)
This method may be used to register a custom resource or datatype. Note that by using custom types, you are creating a system that will not interoperate with other systems that do not know about your custom type. There are valid reasons however for wanting to create custom types and this method can be used to enable them.THREAD SAFETY WARNING: This method is not thread safe. It should be called before any threads are able to call any methods on this context.
- Parameters:
theTypes
- The custom types to add (must not benull
or contain null elements in the collection)
-
setDefaultTypeForProfile
public void setDefaultTypeForProfile(String theProfile, Class<? extends IBaseResource> theClass)
Sets the default type which will be used when parsing a resource that is found to be of the given profile.For example, this method is invoked with the profile string of
"http://example.com/some_patient_profile"
and the type ofMyPatient.class
, if the parser is parsing a resource and finds that it declares that it conforms to that profile, theMyPatient
type will be used unless otherwise specified.- Parameters:
theProfile
- The profile string, e.g."http://example.com/some_patient_profile"
. Must not benull
or empty.theClass
- The resource type, ornull
to clear any existing type
-
setParserErrorHandler
public void setParserErrorHandler(IParserErrorHandler theParserErrorHandler)
Sets a parser error handler to use by default on all parsers- Parameters:
theParserErrorHandler
- The error handler
-
forDstu2
public static FhirContext forDstu2()
Creates and returns a new FhirContext with versionDSTU2
-
forDstu2Hl7Org
public static FhirContext forDstu2Hl7Org()
Creates and returns a new FhirContext with versionDSTU2
(using the Reference Implementation Structures)
-
forDstu2_1
public static FhirContext forDstu2_1()
Creates and returns a new FhirContext with versionDSTU2
(2016 May DSTU3 Snapshot)
-
forDstu3
public static FhirContext forDstu3()
Creates and returns a new FhirContext with versionDSTU3
- Since:
- 1.4
-
forR4
public static FhirContext forR4()
Creates and returns a new FhirContext with versionR4
- Since:
- 3.0.0
-
forR5
public static FhirContext forR5()
Creates and returns a new FhirContext with versionR5
- Since:
- 4.0.0
-
forCached
public static FhirContext forCached(FhirVersionEnum theFhirVersionEnum)
Returns a statically cached FhirContext instance for the given version, creating one if none exists in the cache. One FhirContext will be kept in the cache for each FHIR version that is requested (by calling this method for that version), and the cache will never be expired.- Since:
- 5.1.0
-
-