View Javadoc
1   package ca.uhn.fhir.jpa.util;
2   
3   import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
4   import ca.uhn.fhir.model.dstu2.valueset.ResourceTypeEnum;
5   import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
6   import ca.uhn.fhir.rest.api.server.RequestDetails;
7   import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
8   import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
9   import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter;
10  import org.hl7.fhir.instance.model.api.IBaseResource;
11  import org.hl7.fhir.r4.model.Subscription;
12  import org.hl7.fhir.r4.model.Subscription.SubscriptionChannelType;
13  import org.hl7.fhir.r4.model.Subscription.SubscriptionStatus;
14  import org.springframework.beans.factory.annotation.Autowired;
15  import org.springframework.beans.factory.annotation.Qualifier;
16  
17  import static org.apache.commons.lang3.StringUtils.isNotBlank;
18  
19  /*
20   * #%L
21   * HAPI FHIR JPA Server
22   * %%
23   * Copyright (C) 2014 - 2019 University Health Network
24   * %%
25   * Licensed under the Apache License, Version 2.0 (the "License");
26   * you may not use this file except in compliance with the License.
27   * You may obtain a copy of the License at
28   * 
29   *      http://www.apache.org/licenses/LICENSE-2.0
30   * 
31   * Unless required by applicable law or agreed to in writing, software
32   * distributed under the License is distributed on an "AS IS" BASIS,
33   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34   * See the License for the specific language governing permissions and
35   * limitations under the License.
36   * #L%
37   */
38  
39  /**
40   * Interceptor which requires newly created {@link Subscription subscriptions} to be in
41   * {@link SubscriptionStatus#REQUESTED} state and prevents clients from changing the status.
42   */
43  public class SubscriptionsRequireManualActivationInterceptorR4 extends ServerOperationInterceptorAdapter {
44  
45  	@Autowired
46  	@Qualifier("mySubscriptionDaoR4")
47  	private IFhirResourceDao<Subscription> myDao;
48  
49  	@Override
50  	public void resourceCreated(RequestDetails theRequest, IBaseResource theResource) {
51  		if (myDao.getContext().getResourceDefinition(theResource).getName().equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
52  			verifyStatusOk(RestOperationTypeEnum.CREATE, null, theResource);
53  		}
54  	}
55  
56  	@Override
57  	public void resourceUpdated(RequestDetails theRequest, IBaseResource theOldResource, IBaseResource theNewResource) {
58  		if (myDao.getContext().getResourceDefinition(theNewResource).getName().equals(ResourceTypeEnum.SUBSCRIPTION.getCode())) {
59  			verifyStatusOk(RestOperationTypeEnum.UPDATE, theOldResource, theNewResource);
60  		}
61  	}
62  
63  
64  	public void setDao(IFhirResourceDao<Subscription> theDao) {
65  		myDao = theDao;
66  	}
67  
68  	private void verifyStatusOk(RestOperationTypeEnum theOperation, IBaseResource theOldResourceOrNull, IBaseResource theResource) {
69  		Subscription subscription = (Subscription) theResource;
70  		SubscriptionStatus newStatus = subscription.getStatusElement().getValue();
71  
72  		if (newStatus == SubscriptionStatus.REQUESTED || newStatus == SubscriptionStatus.OFF) {
73  			return;
74  		}
75  
76  		if (newStatus == null) {
77  			String actualCode = subscription.getStatusElement().getValueAsString();
78  			throw new UnprocessableEntityException("Can not " + theOperation.getCode() + " resource: Subscription.status must be populated on this server" + ((isNotBlank(actualCode)) ? " (invalid value " + actualCode + ")" : ""));
79  		}
80  
81  		if (theOldResourceOrNull != null) {
82  			try {
83  				Subscription existing = (Subscription) theOldResourceOrNull;
84  				SubscriptionStatus existingStatus = existing.getStatusElement().getValue();
85  				if (existingStatus != newStatus) {
86  					verifyActiveStatus(theOperation, subscription, newStatus, existingStatus);
87  				}
88  			} catch (ResourceNotFoundException e) {
89  				verifyActiveStatus(theOperation, subscription, newStatus, null);
90  			}
91  		} else {
92  			verifyActiveStatus(theOperation, subscription, newStatus, null);
93  		}
94  	}
95  
96  	private void verifyActiveStatus(RestOperationTypeEnum theOperation, Subscription theSubscription, SubscriptionStatus newStatus, SubscriptionStatus theExistingStatus) {
97  		SubscriptionChannelType channelType = theSubscription.getChannel().getTypeElement().getValue();
98  
99  		if (channelType == null) {
100 			throw new UnprocessableEntityException("Subscription.channel.type must be populated");
101 		}
102 
103 		if (channelType == SubscriptionChannelType.WEBSOCKET) {
104 			return;
105 		}
106 
107 		if (theExistingStatus != null) {
108 			throw new UnprocessableEntityException("Subscription.status can not be changed from " + describeStatus(theExistingStatus) + " to " + describeStatus(newStatus));
109 		}
110 
111 		if (theSubscription.getStatus() == null) {
112 			throw new UnprocessableEntityException("Can not " + theOperation.getCode().toLowerCase() + " resource: Subscription.status must be populated on this server");
113 		}
114 
115 		throw new UnprocessableEntityException("Subscription.status must be '" + SubscriptionStatus.OFF.toCode() + "' or '" + SubscriptionStatus.REQUESTED.toCode() + "' on a newly created subscription");
116 	}
117 
118 	private String describeStatus(SubscriptionStatus existingStatus) {
119 		String existingStatusString;
120 		if (existingStatus != null) {
121 			existingStatusString = '\'' + existingStatus.toCode() + '\'';
122 		} else {
123 			existingStatusString = "null";
124 		}
125 		return existingStatusString;
126 	}
127 
128 }