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 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 List<" + IResource.class.getCanonicalName() + "> or Bundle"; 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 } else { 070 throw new ConfigurationException(createParameterTypeError(theMethod)); 071 } 072 } else { 073 if (theInnerCollectionType.equals(List.class) == false) { 074 throw new ConfigurationException(createParameterTypeError(theMethod)); 075 } 076 if (theParameterType.equals(IResource.class) == false) { 077 throw new ConfigurationException(createParameterTypeError(theMethod)); 078 } 079 myParamStyle = ParamStyle.RESOURCE_LIST; 080 } 081 } 082 083 @Override 084 public Object translateQueryParametersIntoServerArgument(RequestDetails theRequest, BaseMethodBinding<?> theMethodBinding) throws InternalErrorException, InvalidRequestException { 085 IBaseResource parsedBundle = ResourceParameter.parseResourceFromRequest(theRequest, theMethodBinding, myResourceBundleType); 086 087 switch (myParamStyle) { 088 case RESOURCE_LIST: 089 return BundleUtil.toListOfResources(myContext, (IBaseBundle) parsedBundle); 090 case RESOURCE_BUNDLE: 091 default: 092 assert myParamStyle == ParamStyle.RESOURCE_BUNDLE; 093 break; 094 } 095 096 return parsedBundle; 097 } 098 099 ParamStyle getParamStyle() { 100 return myParamStyle; 101 } 102 103 public enum ParamStyle { 104 /** 105 * New style bundle (defined in hapi-fhir-structures-* as a resource definition itself 106 */ 107 RESOURCE_BUNDLE, 108 /** 109 * List of resources 110 */ 111 RESOURCE_LIST 112 } 113 114}