001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.config.r4;
021
022import ca.uhn.fhir.context.FhirContext;
023import ca.uhn.fhir.context.FhirVersionEnum;
024import ca.uhn.fhir.context.ParserOptions;
025import ca.uhn.fhir.narrative.INarrativeGenerator;
026import ca.uhn.fhir.rest.client.api.IRestfulClientFactory;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.context.annotation.Bean;
029import org.springframework.context.annotation.Primary;
030
031public class FhirContextR4Config {
032
033        public static final String DEFAULT_PRESERVE_VERSION_REFS_DSTU2 = "AuditEvent.object.reference";
034        public static final String DEFAULT_PRESERVE_VERSION_REFS_DSTU3 = "AuditEvent.entity.reference";
035        public static final String DEFAULT_PRESERVE_VERSION_REFS_R4_AND_LATER = "AuditEvent.entity.what";
036
037        @Bean(name = "primaryFhirContext")
038        @Primary
039        public FhirContext fhirContextR4(@Autowired(required = false) INarrativeGenerator theNarrativeGenerator) {
040                FhirContext retVal = FhirContext.forR4();
041
042                if (theNarrativeGenerator != null) {
043                        retVal.setNarrativeGenerator(theNarrativeGenerator);
044                }
045                configureFhirContext(retVal);
046
047                return retVal;
048        }
049
050        public static FhirContext configureFhirContext(FhirContext theFhirContext) {
051                // Don't strip versions in some places
052                ParserOptions parserOptions = theFhirContext.getParserOptions();
053                if (theFhirContext.getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) {
054                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_DSTU2);
055                } else if (theFhirContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
056                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_DSTU3);
057                } else {
058                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_R4_AND_LATER);
059                }
060
061                // We use this context to create subscription deliveries and that kind of thing. It doesn't
062                // make much sense to let the HTTP client pool be a blocker since we have delivery queue
063                // sizing as the lever to affect that. So make the pool big enough that it shouldn't get
064                // in the way.
065                IRestfulClientFactory clientFactory = theFhirContext.getRestfulClientFactory();
066                clientFactory.setPoolMaxPerRoute(1000);
067                clientFactory.setPoolMaxTotal(1000);
068
069                return theFhirContext;
070        }
071}