
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.i18n.Msg; 024import ca.uhn.fhir.context.ConfigurationException; 025import ca.uhn.fhir.context.FhirContext; 026import ca.uhn.fhir.model.base.resource.BaseOperationOutcome; 027import ca.uhn.fhir.model.valueset.BundleTypeEnum; 028import ca.uhn.fhir.rest.annotation.Transaction; 029import ca.uhn.fhir.rest.annotation.TransactionParam; 030import ca.uhn.fhir.rest.api.RequestTypeEnum; 031import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 032import ca.uhn.fhir.rest.api.server.IBundleProvider; 033import ca.uhn.fhir.rest.api.server.IRestfulServer; 034import ca.uhn.fhir.rest.api.server.RequestDetails; 035import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 036import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 037import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; 038import ca.uhn.fhir.rest.server.method.TransactionParameter.ParamStyle; 039import org.hl7.fhir.instance.model.api.IBaseResource; 040 041import javax.annotation.Nonnull; 042import java.lang.reflect.Method; 043import java.util.List; 044 045import static org.apache.commons.lang3.StringUtils.isNotBlank; 046 047public class TransactionMethodBinding extends BaseResourceReturningMethodBinding { 048 049 private int myTransactionParamIndex; 050 private ParamStyle myTransactionParamStyle; 051 052 public TransactionMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) { 053 super(null, theMethod, theContext, theProvider); 054 055 myTransactionParamIndex = -1; 056 int index = 0; 057 for (IParameter next : getParameters()) { 058 if (next instanceof TransactionParameter) { 059 if (myTransactionParamIndex != -1) { 060 throw new ConfigurationException(Msg.code(372) + "Method '" + theMethod.getName() + "' in type " + theMethod.getDeclaringClass().getCanonicalName() + " has multiple parameters annotated with the @" 061 + TransactionParam.class + " annotation, exactly one is required for @" + Transaction.class 062 + " methods"); 063 } 064 myTransactionParamIndex = index; 065 myTransactionParamStyle = ((TransactionParameter) next).getParamStyle(); 066 } 067 index++; 068 } 069 070 if (myTransactionParamIndex == -1) { 071 throw new ConfigurationException(Msg.code(373) + "Method '" + theMethod.getName() + "' in type " + theMethod.getDeclaringClass().getCanonicalName() + " does not have a parameter annotated with the @" 072 + TransactionParam.class + " annotation"); 073 } 074 } 075 076 @Nonnull 077 @Override 078 public RestOperationTypeEnum getRestOperationType() { 079 return RestOperationTypeEnum.TRANSACTION; 080 } 081 082 @Override 083 protected BundleTypeEnum getResponseBundleType() { 084 return BundleTypeEnum.TRANSACTION_RESPONSE; 085 } 086 087 @Override 088 public ReturnTypeEnum getReturnType() { 089 return ReturnTypeEnum.BUNDLE; 090 } 091 092 @Override 093 public MethodMatchEnum incomingServerRequestMatchesMethod(RequestDetails theRequest) { 094 if (theRequest.getRequestType() != RequestTypeEnum.POST) { 095 return MethodMatchEnum.NONE; 096 } 097 if (isNotBlank(theRequest.getOperation())) { 098 return MethodMatchEnum.NONE; 099 } 100 if (isNotBlank(theRequest.getResourceName())) { 101 return MethodMatchEnum.NONE; 102 } 103 return MethodMatchEnum.EXACT; 104 } 105 106 @SuppressWarnings("unchecked") 107 @Override 108 public Object invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException { 109 110 /* 111 * The design of HAPI's transaction method for DSTU1 support assumed that a transaction was just an update on a 112 * bunch of resources (because that's what it was), but in DSTU2 transaction has become much more broad, so we 113 * no longer hold the user's hand much here. 114 */ 115 if (myTransactionParamStyle == ParamStyle.RESOURCE_BUNDLE) { 116 // This is the DSTU2 style 117 Object response = invokeServerMethod(theRequest, theMethodParams); 118 return response; 119 } 120 121 // Call the server implementation method 122 Object response = invokeServerMethod(theRequest, theMethodParams); 123 IBundleProvider retVal = toResourceList(response); 124 125 /* 126 * int offset = 0; if (retVal.size() != resources.size()) { if (retVal.size() > 0 && retVal.getResources(0, 127 * 1).get(0) instanceof OperationOutcome) { offset = 1; } else { throw new 128 * InternalErrorException("Transaction bundle contained " + resources.size() + 129 * " entries, but server method response contained " + retVal.size() + " entries (must be the same)"); } } 130 */ 131 132 List<IBaseResource> retResources = retVal.getAllResources(); 133 for (int i = 0; i < retResources.size(); i++) { 134 IBaseResource newRes = retResources.get(i); 135 if (newRes.getIdElement() == null || newRes.getIdElement().isEmpty()) { 136 if (!(newRes instanceof BaseOperationOutcome)) { 137 throw new InternalErrorException(Msg.code(374) + "Transaction method returned resource at index " + i + " with no id specified - IResource#setId(IdDt)"); 138 } 139 } 140 } 141 142 return retVal; 143 } 144 145 @Override 146 protected void populateActionRequestDetailsForInterceptor(RequestDetails theRequestDetails, ActionRequestDetails theDetails, Object[] theMethodParams) { 147 super.populateActionRequestDetailsForInterceptor(theRequestDetails, theDetails, theMethodParams); 148 149 /* 150 * If the method has no parsed resource parameter, we parse here in order to have something for the interceptor. 151 */ 152 IBaseResource resource; 153 if (myTransactionParamIndex != -1) { 154 resource = (IBaseResource) theMethodParams[myTransactionParamIndex]; 155 } else { 156 Class<? extends IBaseResource> resourceType = getContext().getResourceDefinition("Bundle").getImplementingClass(); 157 resource = ResourceParameter.parseResourceFromRequest(theRequestDetails, this, resourceType); 158 } 159 160 theRequestDetails.setResource(resource); 161 if (theDetails != null) { 162 theDetails.setResource(resource); 163 } 164 165 } 166 167}