001/*- 002 * #%L 003 * HAPI FHIR - Server Framework 004 * %% 005 * Copyright (C) 2014 - 2024 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.server.interceptor; 021 022import ca.uhn.fhir.context.FhirContext; 023import ca.uhn.fhir.interceptor.api.Hook; 024import ca.uhn.fhir.interceptor.api.Interceptor; 025import ca.uhn.fhir.interceptor.api.Pointcut; 026import ca.uhn.fhir.rest.api.EncodingEnum; 027import ca.uhn.fhir.rest.api.server.RequestDetails; 028import ca.uhn.fhir.util.ClasspathUtil; 029import org.apache.commons.lang3.Validate; 030import org.hl7.fhir.instance.model.api.IBaseConformance; 031 032/** 033 * This interceptor replaces the auto-generated CapabilityStatement that is generated 034 * by the HAPI FHIR Server with a static hard-coded resource. 035 */ 036@Interceptor 037public class StaticCapabilityStatementInterceptor { 038 039 private String myCapabilityStatementResource; 040 private volatile IBaseConformance myCapabilityStatement; 041 042 /** 043 * Sets the CapabilityStatement to use 044 * 045 * @see #setCapabilityStatementResource(String) #setCapabilityStatementResource(String) is an alternate way to supply the CapabilityStatement 046 */ 047 public void setCapabilityStatement(IBaseConformance theCapabilityStatement) { 048 myCapabilityStatement = theCapabilityStatement; 049 } 050 051 /** 052 * Sets the classpath location of the CapabilityStatement to use. If this method is used to supply 053 * the CapabiltyStatement, then the given classpath resource will be read and parsed as a FHIR 054 * CapabilityStatement. 055 * 056 * @see #setCapabilityStatement(IBaseConformance) #setCapabilityStatement(IBaseConformance) is an alternate way to supply the CapabilityStatement 057 */ 058 public void setCapabilityStatementResource(String theClasspath) { 059 myCapabilityStatementResource = theClasspath; 060 myCapabilityStatement = null; 061 } 062 063 @Hook(Pointcut.SERVER_CAPABILITY_STATEMENT_GENERATED) 064 public IBaseConformance hook(RequestDetails theRequestDetails) { 065 IBaseConformance retVal = myCapabilityStatement; 066 067 if (retVal == null) { 068 Validate.notBlank(myCapabilityStatementResource, "No CapabilityStatement defined"); 069 String output = ClasspathUtil.loadResource(myCapabilityStatementResource); 070 071 FhirContext ctx = theRequestDetails.getFhirContext(); 072 EncodingEnum encoding = EncodingEnum.detectEncodingNoDefault(output); 073 Validate.notNull( 074 encoding, "Could not determine FHIR encoding for resource: %s", myCapabilityStatementResource); 075 076 retVal = (IBaseConformance) encoding.newParser(ctx).parseResource(output); 077 myCapabilityStatement = retVal; 078 } 079 080 return retVal; 081 } 082}