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