001package ca.uhn.fhir.rest.client.method; 002 003/*- 004 * #%L 005 * HAPI FHIR - Client 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 static org.apache.commons.lang3.StringUtils.isBlank; 024 025import java.lang.reflect.Method; 026import java.util.Collections; 027import java.util.Set; 028 029import org.hl7.fhir.instance.model.api.IBaseResource; 030import org.hl7.fhir.instance.model.api.IIdType; 031 032import ca.uhn.fhir.context.FhirContext; 033import ca.uhn.fhir.model.primitive.IdDt; 034import ca.uhn.fhir.rest.annotation.Update; 035import ca.uhn.fhir.rest.api.RequestTypeEnum; 036import ca.uhn.fhir.rest.api.RestOperationTypeEnum; 037import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; 038import ca.uhn.fhir.rest.param.ParameterUtil; 039import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 040 041public class UpdateMethodBinding extends BaseOutcomeReturningMethodBindingWithResourceParam { 042 043 private Integer myIdParameterIndex; 044 045 public UpdateMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) { 046 super(theMethod, theContext, Update.class, theProvider); 047 048 myIdParameterIndex = ParameterUtil.findIdParameterIndex(theMethod, getContext()); 049 } 050 051 052 @Override 053 protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IBaseResource theResource) { 054 IIdType idDt = (IIdType) theArgs[myIdParameterIndex]; 055 if (idDt == null) { 056 throw new NullPointerException("ID can not be null"); 057 } 058 059 FhirContext context = getContext(); 060 061 HttpPutClientInvocation retVal = MethodUtil.createUpdateInvocation(theResource, null, idDt, context); 062 063 for (int idx = 0; idx < theArgs.length; idx++) { 064 IParameter nextParam = getParameters().get(idx); 065 nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null); 066 } 067 068 return retVal; 069 } 070 071 @Override 072 protected String getMatchingOperation() { 073 return null; 074 } 075 076 @Override 077 public RestOperationTypeEnum getRestOperationType() { 078 return RestOperationTypeEnum.UPDATE; 079 } 080 081 /* 082 * @Override public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) { if 083 * (super.incomingServerRequestMatchesMethod(theRequest)) { if (myVersionIdParameterIndex != null) { if 084 * (theRequest.getVersionId() == null) { return false; } } else { if (theRequest.getVersionId() != null) { return 085 * false; } } return true; } else { return false; } } 086 */ 087 088 @Override 089 protected Set<RequestTypeEnum> provideAllowableRequestTypes() { 090 return Collections.singleton(RequestTypeEnum.PUT); 091 } 092 093 @Override 094 protected void validateResourceIdAndUrlIdForNonConditionalOperation(IBaseResource theResource, String theResourceId, String theUrlId, String theMatchUrl) { 095 if (isBlank(theMatchUrl)) { 096 if (isBlank(theUrlId)) { 097 String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "noIdInUrlForUpdate"); 098 throw new InvalidRequestException(msg); 099 } 100 if (isBlank(theResourceId)) { 101 String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "noIdInBodyForUpdate"); 102 throw new InvalidRequestException(msg); 103 } 104 if (!theResourceId.equals(theUrlId)) { 105 String msg = getContext().getLocalizer().getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "incorrectIdForUpdate", theResourceId, theUrlId); 106 throw new InvalidRequestException(msg); 107 } 108 } else { 109 theResource.setId((IIdType)null); 110 } 111 112 } 113}