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