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