001package ca.uhn.fhir.jpa.subscription.submit.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR Subscription Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.interceptor.api.IInterceptorService;
024import ca.uhn.fhir.jpa.api.config.DaoConfig;
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;
030import org.springframework.context.ApplicationContext;
031
032import javax.annotation.PostConstruct;
033import java.util.Set;
034
035public class SubscriptionSubmitInterceptorLoader {
036        private static final Logger ourLog = LoggerFactory.getLogger(SubscriptionSubmitInterceptorLoader.class);
037
038        @Autowired
039        private SubscriptionMatcherInterceptor mySubscriptionMatcherInterceptor;
040        @Autowired
041        private SubscriptionValidatingInterceptor mySubscriptionValidatingInterceptor;
042        @Autowired
043        private DaoConfig myDaoConfig;
044        @Autowired
045        private ApplicationContext myApplicationContext;
046        @Autowired
047        private IInterceptorService myInterceptorRegistry;
048
049        @PostConstruct
050        public void start() {
051                Set<Subscription.SubscriptionChannelType> supportedSubscriptionTypes = myDaoConfig.getSupportedSubscriptionTypes();
052
053                if (supportedSubscriptionTypes.isEmpty()) {
054                        ourLog.info("Subscriptions are disabled on this server.  Subscriptions will not be activated and incoming resources will not be matched against subscriptions.");
055                } else {
056                        ourLog.info("Registering subscription matcher interceptor");
057                        myInterceptorRegistry.registerInterceptor(mySubscriptionMatcherInterceptor);
058                }
059
060                myInterceptorRegistry.registerInterceptor(mySubscriptionValidatingInterceptor);
061        }
062
063        @VisibleForTesting
064        public void unregisterInterceptorsForUnitTest() {
065                myInterceptorRegistry.unregisterInterceptor(mySubscriptionMatcherInterceptor);
066                myInterceptorRegistry.unregisterInterceptor(mySubscriptionValidatingInterceptor);
067        }
068}