001package ca.uhn.fhir.rest.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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.Set;
026
027import org.hl7.fhir.instance.model.api.IBaseResource;
028
029import ca.uhn.fhir.context.FhirContext;
030import ca.uhn.fhir.model.api.IResource;
031import ca.uhn.fhir.model.primitive.IdDt;
032import ca.uhn.fhir.rest.annotation.Validate;
033import ca.uhn.fhir.rest.api.RequestTypeEnum;
034import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
035import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
036import ca.uhn.fhir.rest.server.Constants;
037
038public class ValidateMethodBindingDstu1 extends BaseOutcomeReturningMethodBindingWithResourceParam {
039
040        private Integer myIdParameterIndex;
041
042        public ValidateMethodBindingDstu1(Method theMethod, FhirContext theContext, Object theProvider) {
043                super(theMethod, theContext, Validate.class, theProvider);
044
045                myIdParameterIndex = MethodUtil.findIdParameterIndex(theMethod, getContext());
046        }
047
048        @Override
049        public RestOperationTypeEnum getRestOperationType() {
050                return RestOperationTypeEnum.VALIDATE;
051        }
052
053        @Override
054        protected void addParametersForServerRequest(RequestDetails theRequest, Object[] theParams) {
055                if (myIdParameterIndex != null) {
056                        theParams[myIdParameterIndex] = theRequest.getId();
057                }
058        }
059
060        @Override
061        protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) {
062                FhirContext context = getContext();
063                
064                IdDt idDt=null;
065                if (myIdParameterIndex != null) {
066                        idDt = (IdDt) theArgs[myIdParameterIndex];
067                }
068
069                HttpPostClientInvocation retVal = createValidateInvocation(theResource, idDt, context);
070                
071                for (int idx = 0; idx < theArgs.length; idx++) {
072                        IParameter nextParam = getParameters().get(idx);
073                        nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null);
074                }
075
076                return retVal;
077        }
078
079        public static HttpPostClientInvocation createValidateInvocation(IBaseResource theResource, IdDt theId, FhirContext theContext) {
080                StringBuilder urlExtension = new StringBuilder();
081                urlExtension.append(theContext.getResourceDefinition(theResource).getName());
082                urlExtension.append('/');
083                urlExtension.append(Constants.PARAM_VALIDATE);
084
085                if (theId != null && theId.isEmpty() == false) {
086                        String id = theId.getValue();
087                        urlExtension.append('/');
088                        urlExtension.append(id);
089                }
090                // TODO: is post correct here?
091                HttpPostClientInvocation retVal = new HttpPostClientInvocation(theContext, theResource, urlExtension.toString());
092                return retVal;
093        }
094
095
096        @Override
097        protected boolean allowVoidReturnType() {
098                return true;
099        }
100
101        @Override
102        protected Set<RequestTypeEnum> provideAllowableRequestTypes() {
103                // TODO: is post correct here?
104                return Collections.singleton(RequestTypeEnum.POST);
105        }
106
107
108        @Override
109        protected String getMatchingOperation() {
110                return Constants.PARAM_VALIDATE;
111        }
112
113}