001package ca.uhn.fhir.rest.client.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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 ca.uhn.fhir.i18n.Msg;
024import java.lang.reflect.Method;
025import java.util.*;
026
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.hl7.fhir.instance.model.api.IIdType;
029
030import ca.uhn.fhir.context.FhirContext;
031import ca.uhn.fhir.rest.annotation.Delete;
032import ca.uhn.fhir.rest.api.RequestTypeEnum;
033import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
034import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
035import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
036import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
037
038public class DeleteMethodBinding extends BaseOutcomeReturningMethodBindingWithResourceIdButNoResourceBody {
039
040        public DeleteMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
041                super(theMethod, theContext, theProvider, Delete.class, theMethod.getAnnotation(Delete.class).type());
042        }
043
044        @Override
045        public RestOperationTypeEnum getRestOperationType() {
046                return RestOperationTypeEnum.DELETE;
047        }
048
049        @Override
050        protected Set<RequestTypeEnum> provideAllowableRequestTypes() {
051                return Collections.singleton(RequestTypeEnum.DELETE);
052        }
053
054        @Override
055        protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IBaseResource theResource) {
056                StringBuilder urlExtension = new StringBuilder();
057                urlExtension.append(getContext().getResourceType(theResource));
058
059                return new HttpPostClientInvocation(getContext(), theResource, urlExtension.toString());
060        }
061
062        @Override
063        protected boolean allowVoidReturnType() {
064                return true;
065        }
066
067        @Override
068        public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException {
069                IIdType id = (IIdType) theArgs[getIdParameterIndex()];
070                if (id == null) {
071                        throw new NullPointerException(Msg.code(1472) + "ID can not be null");
072                }
073
074                if (id.hasResourceType() == false) {
075                        id = id.withResourceType(getResourceName());
076                } else if (getResourceName().equals(id.getResourceType()) == false) {
077                        throw new InvalidRequestException(Msg.code(1473) + "ID parameter has the wrong resource type, expected '" + getResourceName() + "', found: " + id.getResourceType());
078                }
079
080                HttpDeleteClientInvocation retVal = createDeleteInvocation(getContext(), id, Collections.emptyMap());
081
082                for (int idx = 0; idx < theArgs.length; idx++) {
083                        IParameter nextParam = getParameters().get(idx);
084                        nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null);
085                }
086
087                return retVal;
088        }
089
090        public static HttpDeleteClientInvocation createDeleteInvocation(FhirContext theContext, IIdType theId, Map<String, List<String>> theAdditionalParams) {
091                return new HttpDeleteClientInvocation(theContext, theId, theAdditionalParams);
092        }
093
094
095        @Override
096        protected String getMatchingOperation() {
097                return null;
098        }
099
100        public static HttpDeleteClientInvocation createDeleteInvocation(FhirContext theContext, String theSearchUrl, Map<String, List<String>> theParams) {
101                return new HttpDeleteClientInvocation(theContext, theSearchUrl, theParams);
102        }
103
104}