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.ConfigurationException;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.context.RuntimeResourceDefinition;
026import ca.uhn.fhir.model.api.IResource;
027import ca.uhn.fhir.rest.annotation.TransactionParam;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
030import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
031import ca.uhn.fhir.util.BundleUtil;
032import org.hl7.fhir.instance.model.api.IBaseBundle;
033import org.hl7.fhir.instance.model.api.IBaseResource;
034
035import java.lang.reflect.Method;
036import java.lang.reflect.Modifier;
037import java.util.Collection;
038import java.util.List;
039
040public class TransactionParameter implements IParameter {
041
042        // private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TransactionParameter.class);
043        private FhirContext myContext;
044        private ParamStyle myParamStyle;
045        private Class<? extends IBaseResource> myResourceBundleType;
046
047        TransactionParameter(FhirContext theContext) {
048                myContext = theContext;
049        }
050
051        private String createParameterTypeError(Method theMethod) {
052                return "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + TransactionParam.class.getName()
053                        + " but is not of type Bundle, IBaseResource, IBaseBundle, or List<" + IResource.class.getCanonicalName() + ">";
054        }
055
056        @Override
057        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
058                if (theOuterCollectionType != null) {
059                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @"
060                                + TransactionParam.class.getName() + " but can not be a collection of collections");
061                }
062                if (Modifier.isInterface(theParameterType.getModifiers()) == false && IBaseResource.class.isAssignableFrom(theParameterType)) {
063                        @SuppressWarnings("unchecked")
064                        Class<? extends IBaseResource> parameterType = (Class<? extends IBaseResource>) theParameterType;
065                        RuntimeResourceDefinition def = myContext.getResourceDefinition(parameterType);
066                        if ("Bundle".equals(def.getName())) {
067                                myParamStyle = ParamStyle.RESOURCE_BUNDLE;
068                                myResourceBundleType = parameterType;
069                                return;
070                        }
071                } else if (theParameterType.equals(IBaseResource.class) || theParameterType.equals(IBaseBundle.class)) {
072                        myParamStyle = ParamStyle.RESOURCE_BUNDLE;
073                        myResourceBundleType = myContext.getResourceDefinition("Bundle").getImplementingClass();
074                        return;
075                } else if (theInnerCollectionType != null) {
076                        if (theInnerCollectionType.equals(List.class) == false) {
077                                throw new ConfigurationException(createParameterTypeError(theMethod));
078                        }
079                        if (theParameterType.equals(IResource.class) == false) {
080                                throw new ConfigurationException(createParameterTypeError(theMethod));
081                        }
082                        myParamStyle = ParamStyle.RESOURCE_LIST;
083                        return;
084                }
085
086                throw new ConfigurationException(createParameterTypeError(theMethod));
087        }
088
089        @Override
090        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
091                IBaseResource parsedBundle = ResourceParameter.parseResourceFromRequest(theRequest, theMethodBinding, myResourceBundleType);
092
093                switch (myParamStyle) {
094                        case RESOURCE_LIST:
095                                return BundleUtil.toListOfResources(myContext, (IBaseBundle) parsedBundle);
096                        case RESOURCE_BUNDLE:
097                        default:
098                                assert myParamStyle == ParamStyle.RESOURCE_BUNDLE;
099                                break;
100                }
101
102                return parsedBundle;
103        }
104
105        ParamStyle getParamStyle() {
106                return myParamStyle;
107        }
108
109        public enum ParamStyle {
110                /**
111                 * New style bundle (defined in hapi-fhir-structures-* as a resource definition itself
112                 */
113                RESOURCE_BUNDLE,
114                /**
115                 * List of resources
116                 */
117                RESOURCE_LIST
118        }
119
120}