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.util;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.interceptor.api.Hook;
024import ca.uhn.fhir.interceptor.api.Interceptor;
025import ca.uhn.fhir.interceptor.api.Pointcut;
026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
027import ca.uhn.fhir.model.dstu2.resource.Subscription;
028import ca.uhn.fhir.model.dstu2.valueset.ResourceTypeEnum;
029import ca.uhn.fhir.model.dstu2.valueset.SubscriptionChannelTypeEnum;
030import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
031import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
032import ca.uhn.fhir.rest.api.server.RequestDetails;
033import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
034import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
035import org.hl7.fhir.instance.model.api.IBaseResource;
036import org.springframework.beans.factory.annotation.Autowired;
037import org.springframework.beans.factory.annotation.Qualifier;
038
039import static ca.uhn.fhir.subscription.SubscriptionConstants.ORDER_SUBSCRIPTION_ACTIVATING;
040import static org.apache.commons.lang3.StringUtils.isNotBlank;
041
042/**
043 * Interceptor which requires newly created {@link Subscription subscriptions} to be in
044 * {@link SubscriptionStatusEnum#REQUESTED} state and prevents clients from changing the status.
045 */
046@Interceptor
047public class SubscriptionsRequireManualActivationInterceptorDstu2 {
048
049        @Autowired
050        @Qualifier("mySubscriptionDaoDstu2")
051        private IFhirResourceDao<Subscription> myDao;
052
053        @Hook(value = Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED, order = ORDER_SUBSCRIPTION_ACTIVATING)
054        public void resourceCreated(RequestDetails theRequest, IBaseResource theResource) {
055                if (myDao.getContext().getResourceType(theResource).equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
056                        verifyStatusOk(RestOperationTypeEnum.CREATE, null, theResource);
057                }
058        }
059
060        @Hook(value = Pointcut.STORAGE_PRESTORAGE_RESOURCE_UPDATED, order = ORDER_SUBSCRIPTION_ACTIVATING)
061        public void resourceUpdated(RequestDetails theRequest, IBaseResource theOldResource, IBaseResource theNewResource) {
062                if (myDao.getContext().getResourceType(theNewResource).equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
063                        verifyStatusOk(RestOperationTypeEnum.UPDATE, theOldResource, theNewResource);
064                }
065        }
066
067        public void setDao(IFhirResourceDao<Subscription> theDao) {
068                myDao = theDao;
069        }
070
071        private void verifyStatusOk(
072                        RestOperationTypeEnum theOperation, IBaseResource theOldResourceOrNull, IBaseResource theResource) {
073                Subscription subscription = (Subscription) theResource;
074                SubscriptionStatusEnum newStatus = subscription.getStatusElement().getValueAsEnum();
075
076                if (newStatus == SubscriptionStatusEnum.REQUESTED || newStatus == SubscriptionStatusEnum.OFF) {
077                        return;
078                }
079
080                if (newStatus == null) {
081                        String actualCode = subscription.getStatusElement().getValueAsString();
082                        throw new UnprocessableEntityException(Msg.code(800) + "Can not " + theOperation.getCode()
083                                        + " resource: Subscription.status must be populated on this server"
084                                        + ((isNotBlank(actualCode)) ? " (invalid value " + actualCode + ")" : ""));
085                }
086
087                if (theOldResourceOrNull != null) {
088                        try {
089                                Subscription existing = (Subscription) theOldResourceOrNull;
090                                SubscriptionStatusEnum existingStatus =
091                                                existing.getStatusElement().getValueAsEnum();
092                                if (existingStatus != newStatus) {
093                                        verifyActiveStatus(theOperation, subscription, newStatus, existingStatus);
094                                }
095                        } catch (ResourceNotFoundException e) {
096                                verifyActiveStatus(theOperation, subscription, newStatus, null);
097                        }
098                } else {
099                        verifyActiveStatus(theOperation, subscription, newStatus, null);
100                }
101        }
102
103        private void verifyActiveStatus(
104                        RestOperationTypeEnum theOperation,
105                        Subscription theSubscription,
106                        SubscriptionStatusEnum newStatus,
107                        SubscriptionStatusEnum theExistingStatus) {
108                SubscriptionChannelTypeEnum channelType =
109                                theSubscription.getChannel().getTypeElement().getValueAsEnum();
110
111                if (channelType == null) {
112                        throw new UnprocessableEntityException(Msg.code(801) + "Subscription.channel.type must be populated");
113                }
114
115                if (channelType == SubscriptionChannelTypeEnum.WEBSOCKET) {
116                        return;
117                }
118
119                if (theExistingStatus != null) {
120                        throw new UnprocessableEntityException(Msg.code(802) + "Subscription.status can not be changed from "
121                                        + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus));
122                }
123
124                if (theSubscription.getStatus() == null) {
125                        throw new UnprocessableEntityException(
126                                        Msg.code(803) + "Can not " + theOperation.getCode().toLowerCase()
127                                                        + " resource: Subscription.status must be populated on this server");
128                }
129
130                throw new UnprocessableEntityException(
131                                Msg.code(804) + "Subscription.status must be '" + SubscriptionStatusEnum.OFF.getCode() + "' or '"
132                                                + SubscriptionStatusEnum.REQUESTED.getCode() + "' on a newly created subscription");
133        }
134
135        private String describeStatus(SubscriptionStatusEnum existingStatus) {
136                String existingStatusString;
137                if (existingStatus != null) {
138                        existingStatusString = '\'' + existingStatus.getCode() + '\'';
139                } else {
140                        existingStatusString = "null";
141                }
142                return existingStatusString;
143        }
144}