001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
004 * %%
005 * Copyright (C) 2014 - 2023 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 org.hl7.fhir.dstu2.model.Subscription;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.springframework.beans.factory.annotation.Autowired;
030
031import javax.annotation.PostConstruct;
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        @Autowired
040        private SubscriptionValidatingInterceptor mySubscriptionValidatingInterceptor;
041        @Autowired(required = false)
042        private SubscriptionTopicValidatingInterceptor mySubscriptionTopicValidatingInterceptor;
043        @Autowired
044        private StorageSettings myStorageSettings;
045        @Autowired
046        private IInterceptorService myInterceptorRegistry;
047        private boolean mySubscriptionValidatingInterceptorRegistered;
048        private boolean mySubscriptionMatcherInterceptorRegistered;
049        private boolean mySubscriptionTopicValidatingInterceptorRegistered;
050
051        @PostConstruct
052        public void start() {
053                Set<Subscription.SubscriptionChannelType> supportedSubscriptionTypes = myStorageSettings.getSupportedSubscriptionTypes();
054
055                if (supportedSubscriptionTypes.isEmpty()) {
056                        ourLog.info("Subscriptions are disabled on this server.  Subscriptions will not be activated and incoming resources will not be matched against subscriptions.");
057                } else {
058                        if (!mySubscriptionMatcherInterceptorRegistered) {
059                                ourLog.info("Registering subscription matcher interceptor");
060                                myInterceptorRegistry.registerInterceptor(mySubscriptionMatcherInterceptor);
061                                mySubscriptionMatcherInterceptorRegistered = true;
062                        }
063                }
064
065                if (!mySubscriptionValidatingInterceptorRegistered) {
066                        myInterceptorRegistry.registerInterceptor(mySubscriptionValidatingInterceptor);
067                        mySubscriptionValidatingInterceptorRegistered = true;
068                }
069
070                if (mySubscriptionTopicValidatingInterceptor != null && !mySubscriptionTopicValidatingInterceptorRegistered) {
071                        myInterceptorRegistry.registerInterceptor(mySubscriptionTopicValidatingInterceptor);
072                        mySubscriptionTopicValidatingInterceptorRegistered = true;
073                }
074        }
075
076        @VisibleForTesting
077        public void unregisterInterceptorsForUnitTest() {
078                myInterceptorRegistry.unregisterInterceptor(mySubscriptionMatcherInterceptor);
079                myInterceptorRegistry.unregisterInterceptor(mySubscriptionValidatingInterceptor);
080                mySubscriptionValidatingInterceptorRegistered = false;
081                mySubscriptionMatcherInterceptorRegistered = false;
082        }
083}