001/*-
002 * #%L
003 * HAPI FHIR Subscription 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.subscription.submit.interceptor;
021
022import ca.uhn.fhir.interceptor.api.IInterceptorService;
023import ca.uhn.fhir.jpa.model.entity.StorageSettings;
024import ca.uhn.fhir.jpa.topic.SubscriptionTopicValidatingInterceptor;
025import com.google.common.annotations.VisibleForTesting;
026import jakarta.annotation.PostConstruct;
027import org.hl7.fhir.dstu2.model.Subscription;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.beans.factory.annotation.Autowired;
031
032import java.util.Set;
033
034public class SubscriptionSubmitInterceptorLoader {
035        private static final Logger ourLog = LoggerFactory.getLogger(SubscriptionSubmitInterceptorLoader.class);
036
037        @Autowired
038        private SubscriptionMatcherInterceptor mySubscriptionMatcherInterceptor;
039
040        @Autowired
041        private SubscriptionValidatingInterceptor mySubscriptionValidatingInterceptor;
042
043        @Autowired(required = false)
044        private SubscriptionTopicValidatingInterceptor mySubscriptionTopicValidatingInterceptor;
045
046        @Autowired
047        private StorageSettings myStorageSettings;
048
049        @Autowired
050        private IInterceptorService myInterceptorRegistry;
051
052        private boolean mySubscriptionValidatingInterceptorRegistered;
053        private boolean mySubscriptionMatcherInterceptorRegistered;
054        private boolean mySubscriptionTopicValidatingInterceptorRegistered;
055
056        @PostConstruct
057        public void start() {
058                Set<Subscription.SubscriptionChannelType> supportedSubscriptionTypes =
059                                myStorageSettings.getSupportedSubscriptionTypes();
060
061                if (supportedSubscriptionTypes.isEmpty()) {
062                        ourLog.info(
063                                        "Subscriptions are disabled on this server.  Subscriptions will not be activated and incoming resources will not be matched against subscriptions.");
064                } else {
065                        if (!mySubscriptionMatcherInterceptorRegistered) {
066                                ourLog.info("Registering subscription matcher interceptor");
067                                myInterceptorRegistry.registerInterceptor(mySubscriptionMatcherInterceptor);
068                                mySubscriptionMatcherInterceptorRegistered = true;
069                        }
070                }
071
072                if (!mySubscriptionValidatingInterceptorRegistered) {
073                        myInterceptorRegistry.registerInterceptor(mySubscriptionValidatingInterceptor);
074                        mySubscriptionValidatingInterceptorRegistered = true;
075                }
076
077                if (mySubscriptionTopicValidatingInterceptor != null && !mySubscriptionTopicValidatingInterceptorRegistered) {
078                        myInterceptorRegistry.registerInterceptor(mySubscriptionTopicValidatingInterceptor);
079                        mySubscriptionTopicValidatingInterceptorRegistered = true;
080                }
081        }
082
083        @VisibleForTesting
084        public void unregisterInterceptorsForUnitTest() {
085                myInterceptorRegistry.unregisterInterceptor(mySubscriptionMatcherInterceptor);
086                myInterceptorRegistry.unregisterInterceptor(mySubscriptionValidatingInterceptor);
087                mySubscriptionValidatingInterceptorRegistered = false;
088                mySubscriptionMatcherInterceptorRegistered = false;
089        }
090}