001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.rest.client.api.IRestfulClientFactory;
026import org.springframework.context.annotation.Bean;
027import org.springframework.context.annotation.Primary;
028
029public class FhirContextR4Config {
030
031        public static final String DEFAULT_PRESERVE_VERSION_REFS_DSTU2 = "AuditEvent.object.reference";
032        public static final String DEFAULT_PRESERVE_VERSION_REFS_DSTU3 = "AuditEvent.entity.reference";
033        public static final String DEFAULT_PRESERVE_VERSION_REFS_R4_AND_LATER = "AuditEvent.entity.what";
034
035        @Bean(name = "primaryFhirContext")
036        @Primary
037        public FhirContext fhirContextR4() {
038                FhirContext retVal = FhirContext.forR4();
039
040                configureFhirContext(retVal);
041
042                return retVal;
043        }
044
045        public static FhirContext configureFhirContext(FhirContext theFhirContext) {
046                // Don't strip versions in some places
047                ParserOptions parserOptions = theFhirContext.getParserOptions();
048                if (theFhirContext.getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) {
049                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_DSTU2);
050                } else if (theFhirContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) {
051                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_DSTU3);
052                } else {
053                        parserOptions.setDontStripVersionsFromReferencesAtPaths(DEFAULT_PRESERVE_VERSION_REFS_R4_AND_LATER);
054                }
055
056                // We use this context to create subscription deliveries and that kind of thing. It doesn't
057                // make much sense to let the HTTP client pool be a blocker since we have delivery queue
058                // sizing as the lever to affect that. So make the pool big enough that it shouldn't get
059                // in the way.
060                IRestfulClientFactory clientFactory = theFhirContext.getRestfulClientFactory();
061                clientFactory.setPoolMaxPerRoute(1000);
062                clientFactory.setPoolMaxTotal(1000);
063
064                return theFhirContext;
065        }
066}