001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2019 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.api.server.IBundleProvider;
034import ca.uhn.fhir.rest.api.server.IRestfulServer;
035import ca.uhn.fhir.rest.api.server.RequestDetails;
036import ca.uhn.fhir.rest.server.SimpleBundleProvider;
037import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
038import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
039
040import javax.annotation.Nonnull;
041
042public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding {
043
044        public ConformanceMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
045                super(theMethod.getReturnType(), theMethod, theContext, theProvider);
046
047                // if (Modifier.isAbstract(theMethod.getReturnType().getModifiers())) {
048                // throw new ConfigurationException("Conformance resource provider method '" + theMethod.getName() + "' must not be abstract");
049                // }
050                MethodReturnTypeEnum methodReturnType = getMethodReturnType();
051                Class<?> genericReturnType = (Class<?>) theMethod.getGenericReturnType();
052                if (methodReturnType != MethodReturnTypeEnum.RESOURCE || !IBaseConformance.class.isAssignableFrom(genericReturnType)) {
053                        throw new ConfigurationException("Conformance resource provider method '" + theMethod.getName() + "' should return a Conformance resource class, returns: " + theMethod.getReturnType());
054                }
055
056        }
057
058        @Override
059        public ReturnTypeEnum getReturnType() {
060                return ReturnTypeEnum.RESOURCE;
061        }
062
063        @Override
064        public IBundleProvider invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest, Object[] theMethodParams) throws BaseServerResponseException {
065                IBaseResource conf = (IBaseResource) invokeServerMethod(theServer, theRequest, theMethodParams);
066                return new SimpleBundleProvider(conf);
067        }
068
069        @Override
070        public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
071                if (theRequest.getRequestType() == RequestTypeEnum.OPTIONS) {
072                        if (theRequest.getOperation() == null && theRequest.getResourceName() == null) {
073                                return true;
074                        }
075                }
076
077                if (theRequest.getResourceName() != null) {
078                        return false;
079                }
080
081                if ("metadata".equals(theRequest.getOperation())) {
082                        if (theRequest.getRequestType() == RequestTypeEnum.GET) {
083                                return true;
084                        }
085                        throw new MethodNotAllowedException("/metadata request must use HTTP GET", RequestTypeEnum.GET);
086                }
087
088                return false;
089        }
090
091        @Nonnull
092        @Override
093        public RestOperationTypeEnum getRestOperationType() {
094                return RestOperationTypeEnum.METADATA;
095        }
096
097        @Override
098        protected BundleTypeEnum getResponseBundleType() {
099                return null;
100        }
101
102}