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