001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 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.valueset.BundleTypeEnum;
025import ca.uhn.fhir.rest.annotation.OperationParam;
026import ca.uhn.fhir.rest.annotation.Validate;
027import ca.uhn.fhir.rest.api.Constants;
028import ca.uhn.fhir.rest.api.EncodingEnum;
029import ca.uhn.fhir.util.ParametersUtil;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031
032import java.lang.annotation.Annotation;
033import java.lang.reflect.Method;
034import java.util.ArrayList;
035import java.util.List;
036
037public class ValidateMethodBindingDstu2Plus extends OperationMethodBinding {
038
039        public ValidateMethodBindingDstu2Plus(Class<?> theReturnResourceType, Class<? extends IBaseResource> theReturnTypeFromRp, Method theMethod, FhirContext theContext, Object theProvider,
040                        Validate theAnnotation) {
041                super(theReturnResourceType, theReturnTypeFromRp, theMethod, theContext, theProvider, true, false, Constants.EXTOP_VALIDATE, theAnnotation.type(), null, new OperationParam[0], BundleTypeEnum.COLLECTION, false);
042
043                List<IParameter> newParams = new ArrayList<>();
044                int idx = 0;
045                for (IParameter next : getParameters()) {
046                        if (next instanceof ResourceParameter) {
047                                if (IBaseResource.class.isAssignableFrom(((ResourceParameter) next).getResourceType())) {
048                                        Class<?> parameterType = theMethod.getParameterTypes()[idx];
049                                        if (String.class.equals(parameterType) || EncodingEnum.class.equals(parameterType)) {
050                                                newParams.add(next);
051                                        } else {
052                                                Annotation[] parameterAnnotations = theMethod.getParameterAnnotations()[idx];
053                                                String description = ParametersUtil.extractDescription(parameterAnnotations);
054                                                List<String> examples = ParametersUtil.extractExamples(parameterAnnotations);
055                                                OperationParameter parameter = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_RESOURCE, 0, 1, description, examples);
056                                                parameter.initializeTypes(theMethod, null, null, parameterType);
057                                                newParams.add(parameter);
058                                        }
059                                } else {
060                                        newParams.add(next);
061                                }
062                        } else {
063                                newParams.add(next);
064                        }
065                        idx++;
066                }
067                setParameters(newParams);
068
069        }
070
071}