001/*-
002 * #%L
003 * HAPI FHIR Subscription 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.subscription.match.matcher.subscriber;
021
022import ca.uhn.fhir.broker.api.IMessageListener;
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
027import ca.uhn.fhir.jpa.model.config.PartitionSettings;
028import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionCanonicalizer;
029import ca.uhn.fhir.jpa.subscription.match.registry.SubscriptionRegistry;
030import ca.uhn.fhir.jpa.subscription.model.ResourceModifiedMessage;
031import ca.uhn.fhir.rest.api.server.RequestDetails;
032import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
033import ca.uhn.fhir.rest.server.messaging.IMessage;
034import jakarta.annotation.Nonnull;
035import org.hl7.fhir.instance.model.api.IBaseResource;
036import org.hl7.fhir.instance.model.api.IIdType;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039import org.springframework.beans.factory.annotation.Autowired;
040
041/**
042 * Responsible for transitioning subscription resources from REQUESTED to ACTIVE
043 * Once activated, the subscription is added to the SubscriptionRegistry.
044 * <p>
045 * Also validates criteria.  If invalid, rejects the subscription without persisting the subscription.
046 */
047public class SubscriptionRegisteringListener implements IMessageListener<ResourceModifiedMessage> {
048        private static final Logger ourLog = LoggerFactory.getLogger(SubscriptionRegisteringListener.class);
049
050        @Autowired
051        private FhirContext myFhirContext;
052
053        @Autowired
054        private SubscriptionRegistry mySubscriptionRegistry;
055
056        @Autowired
057        private SubscriptionCanonicalizer mySubscriptionCanonicalizer;
058
059        @Autowired
060        private DaoRegistry myDaoRegistry;
061
062        @Autowired(required = false)
063        private PartitionSettings myPartitionSettings;
064
065        /**
066         * Constructor
067         */
068        public SubscriptionRegisteringListener() {
069                super();
070        }
071
072        public Class<ResourceModifiedMessage> getPayloadType() {
073                return ResourceModifiedMessage.class;
074        }
075
076        @Override
077        public void handleMessage(@Nonnull IMessage<ResourceModifiedMessage> theMessage) {
078                ResourceModifiedMessage payload = theMessage.getPayload();
079
080                if (!payload.hasResourceType(this.myFhirContext, "Subscription")) {
081                        return;
082                }
083
084                switch (payload.getOperationType()) {
085                        case MANUALLY_TRIGGERED:
086                        case TRANSACTION:
087                                return;
088                        case CREATE:
089                        case UPDATE:
090                        case DELETE:
091                                break;
092                }
093
094                // We read the resource back from the DB instead of using the supplied copy for
095                // two reasons:
096                // - in order to store partition id in the userdata of the resource for partitioned subscriptions
097                // - in case we're processing out of order and a create-then-delete has been processed backwards (or vice versa)
098
099                IIdType payloadId = payload.getPayloadId(myFhirContext).toUnqualifiedVersionless();
100                IFhirResourceDao<?> subscriptionDao = myDaoRegistry.getResourceDao("Subscription");
101                RequestDetails systemRequestDetails = getPartitionAwareRequestDetails(payload);
102                IBaseResource payloadResource = subscriptionDao.read(payloadId, systemRequestDetails, true);
103                if (payloadResource == null) {
104                        // Only for unit test
105                        payloadResource = payload.getResource(myFhirContext);
106                }
107                if (payloadResource.isDeleted()) {
108                        mySubscriptionRegistry.unregisterSubscriptionIfRegistered(payloadId.getIdPart());
109                        return;
110                }
111
112                String statusString = mySubscriptionCanonicalizer.getSubscriptionStatus(payloadResource);
113                if ("active".equals(statusString)) {
114                        mySubscriptionRegistry.registerSubscriptionUnlessAlreadyRegistered(payloadResource);
115                } else {
116                        mySubscriptionRegistry.unregisterSubscriptionIfRegistered(payloadId.getIdPart());
117                }
118        }
119
120        private Integer getDefaultPartitionId() {
121                if (myPartitionSettings != null) {
122                        return myPartitionSettings.getDefaultPartitionId();
123                }
124                /*
125                 * We log a warning because in most cases you will want a partitionsettings
126                 * object.
127                 * However, PartitionSettings beans are not provided in the same
128                 * config as the one that provides this bean; as such, it is the responsibility
129                 * of whomever includes the config for this bean to also provide a PartitionSettings
130                 * bean (or import a config that does)
131                 */
132                ourLog.warn("No PartitionSettings available.");
133                return null;
134        }
135
136        /**
137         * There were some situations where the RequestDetails attempted to use the default partition
138         * and the partition name was a list containing null values (i.e. using the package installer to STORE_AND_INSTALL
139         * Subscriptions while partitioning was enabled). If any partition matches these criteria,
140         * {@link RequestPartitionId#defaultPartition()} is used to obtain the default partition.
141         */
142        private RequestDetails getPartitionAwareRequestDetails(ResourceModifiedMessage payload) {
143                Integer defaultPartitionId = getDefaultPartitionId();
144                RequestPartitionId payloadPartitionId = payload.getPartitionId();
145                if (payloadPartitionId == null || payloadPartitionId.isPartition(defaultPartitionId)) {
146                        // This may look redundant but the package installer STORE_AND_INSTALL Subscriptions when partitioning is
147                        // enabled
148                        // creates a corrupt default partition.  This resets it to a clean one.
149                        payloadPartitionId = RequestPartitionId.defaultPartition();
150                }
151                return new SystemRequestDetails().setRequestPartitionId(payloadPartitionId);
152        }
153}