001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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 java.io.Reader;
024import java.lang.reflect.Method;
025import java.lang.reflect.Modifier;
026import java.util.Collection;
027import java.util.List;
028
029import org.apache.commons.io.IOUtils;
030import org.hl7.fhir.instance.model.api.IBaseBundle;
031import org.hl7.fhir.instance.model.api.IBaseResource;
032
033import ca.uhn.fhir.context.*;
034import ca.uhn.fhir.model.api.IResource;
035import ca.uhn.fhir.parser.IParser;
036import ca.uhn.fhir.rest.annotation.TransactionParam;
037import ca.uhn.fhir.rest.api.EncodingEnum;
038import ca.uhn.fhir.rest.api.server.RequestDetails;
039import ca.uhn.fhir.rest.server.RestfulServerUtils;
040import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
041import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
042import ca.uhn.fhir.util.BundleUtil;
043
044public class TransactionParameter implements IParameter {
045
046        // private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TransactionParameter.class);
047        private FhirContext myContext;
048        private ParamStyle myParamStyle;
049        private Class<? extends IBaseResource> myResourceBundleType;
050
051        public TransactionParameter(FhirContext theContext) {
052                myContext = theContext;
053        }
054
055        private String createParameterTypeError(Method theMethod) {
056                return "Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @" + TransactionParam.class.getName()
057                                + " but is not of type List<" + IResource.class.getCanonicalName() + "> or Bundle";
058        }
059
060        @Override
061        public void initializeTypes(Method theMethod, Class<? extends Collection<?>> theOuterCollectionType, Class<? extends Collection<?>> theInnerCollectionType, Class<?> theParameterType) {
062                if (theOuterCollectionType != null) {
063                        throw new ConfigurationException("Method '" + theMethod.getName() + "' in type '" + theMethod.getDeclaringClass().getCanonicalName() + "' is annotated with @"
064                                        + TransactionParam.class.getName() + " but can not be a collection of collections");
065                }
066                if (Modifier.isInterface(theParameterType.getModifiers()) == false && IBaseResource.class.isAssignableFrom(theParameterType)) {
067                        @SuppressWarnings("unchecked")
068                        Class<? extends IBaseResource> parameterType = (Class<? extends IBaseResource>) theParameterType;
069                        RuntimeResourceDefinition def = myContext.getResourceDefinition(parameterType);
070                        if ("Bundle".equals(def.getName())) {
071                                myParamStyle = ParamStyle.RESOURCE_BUNDLE;
072                                myResourceBundleType = parameterType;
073                        } else {
074                                throw new ConfigurationException(createParameterTypeError(theMethod));
075                        }
076                } else {
077                        if (theInnerCollectionType.equals(List.class) == false) {
078                                throw new ConfigurationException(createParameterTypeError(theMethod));
079                        }
080                        if (theParameterType.equals(IResource.class) == false) {
081                                throw new ConfigurationException(createParameterTypeError(theMethod));
082                        }
083                        myParamStyle = ParamStyle.RESOURCE_LIST;
084                }
085        }
086
087        @Override
088        public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException {
089                // TODO: don't use a default encoding, just fail!
090                EncodingEnum encoding = RestfulServerUtils.determineRequestEncoding(theRequest);
091
092                IParser parser = encoding.newParser(theRequest.getServer().getFhirContext());
093
094                Reader reader = ResourceParameter.createRequestReader(theRequest);
095                try {
096
097                        switch (myParamStyle) {
098                        case RESOURCE_LIST: {
099                                Class<? extends IBaseResource> type = myContext.getResourceDefinition("Bundle").getImplementingClass();
100                                IBaseResource bundle = parser.parseResource(type, reader);
101                                List<IBaseResource> resourceList = BundleUtil.toListOfResources(myContext, (IBaseBundle) bundle);
102                                return resourceList;
103                        }
104                        case RESOURCE_BUNDLE:
105                                return parser.parseResource(myResourceBundleType, reader);
106                        }
107
108                        throw new IllegalStateException("Unknown type: " + myParamStyle); // should not happen
109
110                } finally {
111                        IOUtils.closeQuietly(reader);
112                }
113        }
114
115        public ParamStyle getParamStyle() {
116                return myParamStyle;
117        }
118
119        public enum ParamStyle {
120                /** New style bundle (defined in hapi-fhir-structures-* as a resource definition itself */
121                RESOURCE_BUNDLE,
122                /** List of resources */
123                RESOURCE_LIST
124        }
125
126}