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