001package ca.uhn.fhir.rest.server.method;
002
003import static org.apache.commons.lang3.StringUtils.isNotBlank;
004
005/*
006 * #%L
007 * HAPI FHIR - Server Framework
008 * %%
009 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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.rest.annotation.Create;
035import ca.uhn.fhir.rest.api.RequestTypeEnum;
036import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
037import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
038
039import javax.annotation.Nonnull;
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 String getMatchingOperation() {
049                return null;
050        }
051
052        @Nonnull
053        @Override
054        public RestOperationTypeEnum getRestOperationType() {
055                return RestOperationTypeEnum.CREATE;
056        }
057
058        @Override
059        protected Set<RequestTypeEnum> provideAllowableRequestTypes() {
060                return Collections.singleton(RequestTypeEnum.POST);
061        }
062
063        @Override
064        protected void validateResourceIdAndUrlIdForNonConditionalOperation(IBaseResource theResource, String theResourceId,
065                        String theUrlId, String theMatchUrl) {
066                if (isNotBlank(theUrlId)) {
067                        String msg = getContext().getLocalizer()
068                                        .getMessage(BaseOutcomeReturningMethodBindingWithResourceParam.class, "idInUrlForCreate", theUrlId);
069                        throw new InvalidRequestException(msg);
070                }
071                if (getContext().getVersion().getVersion().isOlderThan(FhirVersionEnum.DSTU3)) {
072                        if (isNotBlank(theResourceId)) {
073                                String msg = getContext().getLocalizer().getMessage(
074                                                BaseOutcomeReturningMethodBindingWithResourceParam.class, "idInBodyForCreate", theResourceId);
075                                throw new InvalidRequestException(msg);
076                        }
077                } else {
078                        theResource.setId((IIdType) null);
079                }
080        }
081
082}