001package ca.uhn.fhir.rest.server.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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 ca.uhn.fhir.context.ConfigurationException;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.interceptor.api.HookParams;
026import ca.uhn.fhir.interceptor.api.Pointcut;
027import ca.uhn.fhir.model.valueset.BundleTypeEnum;
028import ca.uhn.fhir.rest.annotation.Metadata;
029import ca.uhn.fhir.rest.api.CacheControlDirective;
030import ca.uhn.fhir.rest.api.Constants;
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.RestfulServer;
037import ca.uhn.fhir.rest.server.SimpleBundleProvider;
038import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
039import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
040import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor;
041import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
042import org.hl7.fhir.instance.model.api.IBaseConformance;
043
044import javax.annotation.Nonnull;
045import java.lang.reflect.Method;
046import java.util.concurrent.ExecutorService;
047import java.util.concurrent.LinkedBlockingQueue;
048import java.util.concurrent.ThreadFactory;
049import java.util.concurrent.ThreadPoolExecutor;
050import java.util.concurrent.TimeUnit;
051import java.util.concurrent.atomic.AtomicLong;
052import java.util.concurrent.atomic.AtomicReference;
053
054public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding {
055        public static final String CACHE_THREAD_PREFIX = "capabilitystatement-cache-";
056        /*
057         * Note: This caching mechanism should probably be configurable and maybe
058         * even applicable to other bindings. It's particularly important for this
059         * operation though, so a one-off is fine for now
060         */
061        private final AtomicReference<IBaseConformance> myCachedResponse = new AtomicReference<>();
062        private final AtomicLong myCachedResponseExpires = new AtomicLong(0L);
063        private final ExecutorService myThreadPool;
064        private long myCacheMillis = 60 * 1000;
065
066        ConformanceMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
067                super(theMethod.getReturnType(), theMethod, theContext, theProvider);
068
069                MethodReturnTypeEnum methodReturnType = getMethodReturnType();
070                Class<?> genericReturnType = (Class<?>) theMethod.getGenericReturnType();
071                if (methodReturnType != MethodReturnTypeEnum.RESOURCE || !IBaseConformance.class.isAssignableFrom(genericReturnType)) {
072                        throw new ConfigurationException("Conformance resource provider method '" + theMethod.getName() + "' should return a Conformance resource class, returns: " + theMethod.getReturnType());
073                }
074
075                Metadata metadata = theMethod.getAnnotation(Metadata.class);
076                if (metadata != null) {
077                        setCacheMillis(metadata.cacheMillis());
078                }
079
080                ThreadFactory threadFactory = r -> {
081                        Thread t = new Thread(r);
082                        t.setName(CACHE_THREAD_PREFIX + t.getId());
083                        t.setDaemon(false);
084                        return t;
085                };
086                myThreadPool = new ThreadPoolExecutor(1, 1,
087                        0L, TimeUnit.MILLISECONDS,
088                        new LinkedBlockingQueue<>(1),
089                        threadFactory,
090                        new ThreadPoolExecutor.DiscardOldestPolicy());
091        }
092
093        /**
094         * Returns the number of milliseconds to cache the generated CapabilityStatement for. Default is one minute, and can be
095         * set to 0 to never cache.
096         *
097         * @see #setCacheMillis(long)
098         * @see Metadata#cacheMillis()
099         * @since 4.1.0
100         */
101        private long getCacheMillis() {
102                return myCacheMillis;
103        }
104
105        /**
106         * Returns the number of milliseconds to cache the generated CapabilityStatement for. Default is one minute, and can be
107         * set to 0 to never cache.
108         *
109         * @see #getCacheMillis()
110         * @see Metadata#cacheMillis()
111         * @since 4.1.0
112         */
113        public void setCacheMillis(long theCacheMillis) {
114                myCacheMillis = theCacheMillis;
115        }
116
117        @Override
118        public ReturnTypeEnum getReturnType() {
119                return ReturnTypeEnum.RESOURCE;
120        }
121
122        @Override
123        public void close() {
124                super.close();
125
126                myThreadPool.shutdown();
127        }
128
129        @Override
130        public IBundleProvider invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest, Object[] theMethodParams) throws BaseServerResponseException {
131                IBaseConformance conf;
132
133                CacheControlDirective cacheControlDirective = new CacheControlDirective().parse(theRequest.getHeaders(Constants.HEADER_CACHE_CONTROL));
134
135                if (cacheControlDirective.isNoCache())
136                        conf = null;
137                else {
138                        conf = myCachedResponse.get();
139                        if ("true".equals(System.getProperty("test"))) {
140                                conf = null;
141                        }
142                        if (conf != null) {
143                                long expires = myCachedResponseExpires.get();
144                                if (expires < System.currentTimeMillis()) {
145                                        myCachedResponseExpires.set(System.currentTimeMillis() + getCacheMillis());
146                                        myThreadPool.submit(() -> createCapabilityStatement(theRequest, theMethodParams));
147                                }
148                        }
149                }
150                if (conf != null) {
151                        // Handle server action interceptors
152                        RestOperationTypeEnum operationType = getRestOperationType(theRequest);
153                        if (operationType != null) {
154                                IServerInterceptor.ActionRequestDetails details = new IServerInterceptor.ActionRequestDetails(theRequest);
155                                populateActionRequestDetailsForInterceptor(theRequest, details, theMethodParams);
156                                // Interceptor hook: SERVER_INCOMING_REQUEST_PRE_HANDLED
157                                if (theRequest.getInterceptorBroadcaster() != null) {
158                                        HookParams preHandledParams = new HookParams();
159                                        preHandledParams.add(RestOperationTypeEnum.class, theRequest.getRestOperationType());
160                                        preHandledParams.add(RequestDetails.class, theRequest);
161                                        preHandledParams.addIfMatchesType(ServletRequestDetails.class, theRequest);
162                                        preHandledParams.add(IServerInterceptor.ActionRequestDetails.class, details);
163                                        theRequest
164                                                .getInterceptorBroadcaster()
165                                                .callHooks(Pointcut.SERVER_INCOMING_REQUEST_PRE_HANDLED, preHandledParams);
166                                }
167                        }
168                }
169
170                if (conf == null) {
171                        conf = createCapabilityStatement(theRequest, theMethodParams);
172                }
173
174                return new SimpleBundleProvider(conf);
175        }
176
177        private IBaseConformance createCapabilityStatement(RequestDetails theRequest, Object[] theMethodParams) {
178                IBaseConformance conf = (IBaseConformance) invokeServerMethod(theRequest, theMethodParams);
179
180                // Interceptor hook: SERVER_CAPABILITY_STATEMENT_GENERATED
181                if (theRequest.getInterceptorBroadcaster() != null) {
182                        HookParams params = new HookParams();
183                        params.add(IBaseConformance.class, conf);
184                        params.add(RequestDetails.class, theRequest);
185                        params.addIfMatchesType(ServletRequestDetails.class, theRequest);
186                        IBaseConformance outcome = (IBaseConformance) theRequest
187                                .getInterceptorBroadcaster()
188                                .callHooksAndReturnObject(Pointcut.SERVER_CAPABILITY_STATEMENT_GENERATED, params);
189                        if (outcome != null) {
190                                conf = outcome;
191                        }
192                }
193
194                if (myCacheMillis > 0) {
195                        myCachedResponse.set(conf);
196                        myCachedResponseExpires.set(System.currentTimeMillis() + getCacheMillis());
197                }
198                
199                return conf;
200        }
201
202        @Override
203        public MethodMatchEnum incomingServerRequestMatchesMethod(RequestDetails theRequest) {
204                if (theRequest.getRequestType() == RequestTypeEnum.OPTIONS) {
205                        if (theRequest.getOperation() == null && theRequest.getResourceName() == null) {
206                                return MethodMatchEnum.EXACT;
207                        }
208                }
209
210                if (theRequest.getResourceName() != null) {
211                        return MethodMatchEnum.NONE;
212                }
213
214                if ("metadata".equals(theRequest.getOperation())) {
215                        if (theRequest.getRequestType() == RequestTypeEnum.GET) {
216                                return MethodMatchEnum.EXACT;
217                        }
218                        throw new MethodNotAllowedException("/metadata request must use HTTP GET", RequestTypeEnum.GET);
219                }
220
221                return MethodMatchEnum.NONE;
222        }
223
224        @Nonnull
225        @Override
226        public RestOperationTypeEnum getRestOperationType() {
227                return RestOperationTypeEnum.METADATA;
228        }
229
230        @Override
231        protected BundleTypeEnum getResponseBundleType() {
232                return null;
233        }
234
235        /**
236         * Create and return the server's CapabilityStatement
237         */
238        public IBaseConformance provideCapabilityStatement(RestfulServer theServer, RequestDetails theRequest) {
239                Object[] params = createMethodParams(theRequest);
240                IBundleProvider resultObj = invokeServer(theServer, theRequest, params);
241                return (IBaseConformance) resultObj.getResources(0,1).get(0);
242        }
243
244}