001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
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.context.FhirContext;
024import ca.uhn.fhir.model.api.IResource;
025import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
026import ca.uhn.fhir.model.primitive.IdDt;
027import ca.uhn.fhir.model.valueset.BundleTypeEnum;
028import ca.uhn.fhir.rest.annotation.History;
029import ca.uhn.fhir.rest.api.Constants;
030import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
031import ca.uhn.fhir.rest.api.server.IBundleProvider;
032import ca.uhn.fhir.rest.api.server.IRestfulServer;
033import ca.uhn.fhir.rest.api.server.RequestDetails;
034import ca.uhn.fhir.rest.param.ParameterUtil;
035import ca.uhn.fhir.rest.server.IResourceProvider;
036import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
037import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
038import org.apache.commons.lang3.StringUtils;
039import org.hl7.fhir.instance.model.api.IBaseResource;
040import org.hl7.fhir.instance.model.api.IPrimitiveType;
041
042import javax.annotation.Nonnull;
043import java.lang.reflect.Method;
044import java.lang.reflect.Modifier;
045import java.util.Date;
046import java.util.List;
047
048import static org.apache.commons.lang3.StringUtils.isBlank;
049
050public class HistoryMethodBinding extends BaseResourceReturningMethodBinding {
051
052        private final Integer myIdParamIndex;
053        private final RestOperationTypeEnum myResourceOperationType;
054        private final String myResourceName;
055
056        public HistoryMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
057                super(toReturnType(theMethod, theProvider), theMethod, theContext, theProvider);
058
059                myIdParamIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext());
060
061                History historyAnnotation = theMethod.getAnnotation(History.class);
062                Class<? extends IBaseResource> type = historyAnnotation.type();
063                if (Modifier.isInterface(type.getModifiers())) {
064                        if (theProvider instanceof IResourceProvider) {
065                                type = ((IResourceProvider) theProvider).getResourceType();
066                                if (myIdParamIndex != null) {
067                                        myResourceOperationType = RestOperationTypeEnum.HISTORY_INSTANCE;
068                                } else {
069                                        myResourceOperationType = RestOperationTypeEnum.HISTORY_TYPE;
070                                }
071                        } else {
072                                myResourceOperationType = RestOperationTypeEnum.HISTORY_SYSTEM;
073                        }
074                } else {
075                        if (myIdParamIndex != null) {
076                                myResourceOperationType = RestOperationTypeEnum.HISTORY_INSTANCE;
077                        } else {
078                                myResourceOperationType = RestOperationTypeEnum.HISTORY_TYPE;
079                        }
080                }
081
082                if (type != IBaseResource.class && type != IResource.class) {
083                        myResourceName = theContext.getResourceType(type);
084                } else {
085                        myResourceName = null;
086                }
087
088        }
089
090        @Override
091        protected BundleTypeEnum getResponseBundleType() {
092                return BundleTypeEnum.HISTORY;
093        }
094
095        @Nonnull
096        @Override
097        public RestOperationTypeEnum getRestOperationType() {
098                return myResourceOperationType;
099        }
100
101        @Override
102        public ReturnTypeEnum getReturnType() {
103                return ReturnTypeEnum.BUNDLE;
104        }
105
106        // ObjectUtils.equals is replaced by a JDK7 method..
107        @Override
108        public MethodMatchEnum incomingServerRequestMatchesMethod(RequestDetails theRequest) {
109                if (!Constants.PARAM_HISTORY.equals(theRequest.getOperation())) {
110                        return MethodMatchEnum.NONE;
111                }
112                if (theRequest.getResourceName() == null) {
113                        if (myResourceOperationType == RestOperationTypeEnum.HISTORY_SYSTEM) {
114                                return MethodMatchEnum.EXACT;
115                        } else {
116                                return MethodMatchEnum.NONE;
117                        }
118                }
119                if (!StringUtils.equals(theRequest.getResourceName(), myResourceName)) {
120                        return MethodMatchEnum.NONE;
121                }
122
123                boolean haveIdParam = theRequest.getId() != null && !theRequest.getId().isEmpty();
124                boolean wantIdParam = myIdParamIndex != null;
125                if (haveIdParam != wantIdParam) {
126                        return MethodMatchEnum.NONE;
127                }
128
129                if (theRequest.getId() == null) {
130                        if (myResourceOperationType != RestOperationTypeEnum.HISTORY_TYPE) {
131                                return MethodMatchEnum.NONE;
132                        }
133                } else if (theRequest.getId().hasVersionIdPart()) {
134                        return MethodMatchEnum.NONE;
135                }
136
137                return MethodMatchEnum.EXACT;
138        }
139
140
141        @Override
142        public IBundleProvider invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException {
143                if (myIdParamIndex != null) {
144                        theMethodParams[myIdParamIndex] = theRequest.getId();
145                }
146
147                Object response = invokeServerMethod(theRequest, theMethodParams);
148
149                final IBundleProvider resources = toResourceList(response);
150
151                /*
152                 * We wrap the response so we can verify that it has the ID and version set,
153                 * as is the contract for history
154                 */
155                return new IBundleProvider() {
156
157                        @Override
158                        public String getCurrentPageId() {
159                                return resources.getCurrentPageId();
160                        }
161
162                        @Override
163                        public String getNextPageId() {
164                                return resources.getNextPageId();
165                        }
166
167                        @Override
168                        public String getPreviousPageId() {
169                                return resources.getPreviousPageId();
170                        }
171
172                        @Override
173                        public IPrimitiveType<Date> getPublished() {
174                                return resources.getPublished();
175                        }
176
177                        @Nonnull
178                        @Override
179                        public List<IBaseResource> getResources(int theFromIndex, int theToIndex) {
180                                List<IBaseResource> retVal = resources.getResources(theFromIndex, theToIndex);
181                                int index = theFromIndex;
182                                for (IBaseResource nextResource : retVal) {
183                                        if (nextResource.getIdElement() == null || isBlank(nextResource.getIdElement().getIdPart())) {
184                                                throw new InternalErrorException("Server provided resource at index " + index + " with no ID set (using IResource#setId(IdDt))");
185                                        }
186                                        if (isBlank(nextResource.getIdElement().getVersionIdPart()) && nextResource instanceof IResource) {
187                                                //TODO: Use of a deprecated method should be resolved.
188                                                IdDt versionId = ResourceMetadataKeyEnum.VERSION_ID.get((IResource) nextResource);
189                                                if (versionId == null || versionId.isEmpty()) {
190                                                        throw new InternalErrorException("Server provided resource at index " + index + " with no Version ID set (using IResource#setId(IdDt))");
191                                                }
192                                        }
193                                        index++;
194                                }
195                                return retVal;
196                        }
197
198                        @Override
199                        public String getUuid() {
200                                return resources.getUuid();
201                        }
202
203                        @Override
204                        public Integer preferredPageSize() {
205                                return resources.preferredPageSize();
206                        }
207
208                        @Override
209                        public Integer size() {
210                                return resources.size();
211                        }
212                };
213        }
214
215        private static Class<? extends IBaseResource> toReturnType(Method theMethod, Object theProvider) {
216                if (theProvider instanceof IResourceProvider) {
217                        return ((IResourceProvider) theProvider).getResourceType();
218                }
219                History historyAnnotation = theMethod.getAnnotation(History.class);
220                Class<? extends IBaseResource> type = historyAnnotation.type();
221                if (type != IBaseResource.class && type != IResource.class) {
222                        return type;
223                }
224                return null;
225        }
226
227}