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