
001package ca.uhn.fhir.rest.client.method; 002 003import ca.uhn.fhir.i18n.Msg; 004import static org.apache.commons.lang3.StringUtils.isNotBlank; 005 006/* 007 * #%L 008 * HAPI FHIR - Client Framework 009 * %% 010 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 011 * %% 012 * Licensed under the Apache License, Version 2.0 (the "License"); 013 * you may not use this file except in compliance with the License. 014 * You may obtain a copy of the License at 015 * 016 * http://www.apache.org/licenses/LICENSE-2.0 017 * 018 * Unless required by applicable law or agreed to in writing, software 019 * distributed under the License is distributed on an "AS IS" BASIS, 020 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 021 * See the License for the specific language governing permissions and 022 * limitations under the License. 023 * #L% 024 */ 025 026import java.lang.reflect.Method; 027import java.util.Collections; 028import java.util.Set; 029 030import org.hl7.fhir.instance.model.api.IBaseResource; 031import org.hl7.fhir.instance.model.api.IIdType; 032 033import ca.uhn.fhir.context.FhirContext; 034import ca.uhn.fhir.context.FhirVersionEnum; 035import ca.uhn.fhir.model.api.IResource; 036import ca.uhn.fhir.rest.annotation.Create; 037import ca.uhn.fhir.rest.api.RequestTypeEnum; 038import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 039import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; 040import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 041 042public class CreateMethodBinding extends BaseOutcomeReturningMethodBindingWithResourceParam { 043 044 public CreateMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) { 045 super(theMethod, theContext, Create.class, theProvider); 046 } 047 048 @Override 049 protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IBaseResource theResource) { 050 FhirContext context = getContext(); 051 052 BaseHttpClientInvocation retVal = MethodUtil.createCreateInvocation(theResource, context); 053 054 if (theArgs != null) { 055 for (int idx = 0; idx < theArgs.length; idx++) { 056 IParameter nextParam = getParameters().get(idx); 057 nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null); 058 } 059 } 060 061 return retVal; 062 } 063 064 @Override 065 protected String getMatchingOperation() { 066 return null; 067 } 068 069 @Override 070 public RestOperationTypeEnum getRestOperationType() { 071 return RestOperationTypeEnum.CREATE; 072 } 073 074 @Override 075 protected Set<RequestTypeEnum> provideAllowableRequestTypes() { 076 return Collections.singleton(RequestTypeEnum.POST); 077 } 078 079 @Override 080 protected void validateResourceIdAndUrlIdForNonConditionalOperation(IBaseResource theResource, String theResourceId, String theUrlId, String theMatchUrl) { 081 if (isNotBlank(theUrlId)) { 082 String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "idInUrlForCreate", theUrlId); 083 throw new InvalidRequestException(Msg.code(1411) + msg); 084 } 085 if (getContext().getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) { 086 if (isNotBlank(theResourceId)) { 087 String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "idInBodyForCreate", theResourceId); 088 throw new InvalidRequestException(Msg.code(1412) + msg); 089 } 090 } else { 091 theResource.setId((IIdType)null); 092 } 093 } 094 095}