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;
024
025import org.hl7.fhir.instance.model.api.IBaseConformance;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027
028import ca.uhn.fhir.context.ConfigurationException;
029import ca.uhn.fhir.context.FhirContext;
030import ca.uhn.fhir.model.valueset.BundleTypeEnum;
031import ca.uhn.fhir.rest.api.RequestTypeEnum;
032import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
033import ca.uhn.fhir.rest.server.IBundleProvider;
034import ca.uhn.fhir.rest.server.IRestfulServer;
035import ca.uhn.fhir.rest.server.SimpleBundleProvider;
036import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
037import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
038
039public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding {
040
041        public ConformanceMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
042                super(theMethod.getReturnType(), theMethod, theContext, theProvider);
043
044//              if (Modifier.isAbstract(theMethod.getReturnType().getModifiers())) {
045//                      throw new ConfigurationException("Conformance resource provider method '" + theMethod.getName() + "' must not be abstract");
046//              }
047                MethodReturnTypeEnum methodReturnType = getMethodReturnType();
048                Class<?> genericReturnType = (Class<?>) theMethod.getGenericReturnType();
049                if (methodReturnType != MethodReturnTypeEnum.RESOURCE || !IBaseConformance.class.isAssignableFrom(genericReturnType)) {
050                        throw new ConfigurationException("Conformance resource provider method '" + theMethod.getName() + "' should return a Conformance resource class, returns: " + theMethod.getReturnType());
051                }
052
053        }
054
055        @Override
056        public ReturnTypeEnum getReturnType() {
057                return ReturnTypeEnum.RESOURCE;
058        }
059
060        @Override
061        public HttpGetClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException {
062                HttpGetClientInvocation retVal = MethodUtil.createConformanceInvocation();
063
064                if (theArgs != null) {
065                        for (int idx = 0; idx < theArgs.length; idx++) {
066                                IParameter nextParam = getParameters().get(idx);
067                                nextParam.translateClientArgumentIntoQueryArgument(getContext(), theArgs[idx], null, null);
068                        }
069                }
070
071                return retVal;
072        }
073
074        @Override
075        public IBundleProvider invokeServer(IRestfulServer theServer, RequestDetails theRequest, Object[] theMethodParams) throws BaseServerResponseException {
076                IBaseResource conf = (IBaseResource) invokeServerMethod(theServer, theRequest, theMethodParams);
077                return new SimpleBundleProvider(conf);
078        }
079
080        @Override
081        public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
082                if (theRequest.getRequestType() == RequestTypeEnum.OPTIONS) {
083                        return true;
084                }
085
086                if (theRequest.getRequestType() == RequestTypeEnum.GET && "metadata".equals(theRequest.getOperation())) {
087                        return true;
088                }
089
090                return false;
091        }
092
093        @Override
094        public RestOperationTypeEnum getRestOperationType() {
095                return RestOperationTypeEnum.METADATA;
096        }
097
098        @Override
099        protected BundleTypeEnum getResponseBundleType() {
100                return null;
101        }
102
103}