001package ca.uhn.fhir.rest.server.interceptor; 002 003/*- 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 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 023 024import ca.uhn.fhir.context.FhirContext; 025import ca.uhn.fhir.interceptor.api.Hook; 026import ca.uhn.fhir.interceptor.api.Interceptor; 027import ca.uhn.fhir.interceptor.api.Pointcut; 028import ca.uhn.fhir.rest.api.Constants; 029import ca.uhn.fhir.rest.api.server.RequestDetails; 030import ca.uhn.fhir.util.MetaUtil; 031import org.hl7.fhir.instance.model.api.IBaseResource; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035import static org.apache.commons.lang3.StringUtils.isNotBlank; 036 037/** 038 * This interceptor examines a header on the incoming request and places it in 039 * <code>Resource.meta.source</code> (R4 and above) or in an extension on <code>Resource.meta</code> 040 * with the URL <code>http://hapifhir.io/fhir/StructureDefinition/resource-meta-source</code> (DSTU3). 041 * <p> 042 * This interceptor does not support versions of FHIR below DSTU3. 043 * </p> 044 * 045 * @see <a href="http://hl7.org/fhir/resource-definitions.html#Resource.meta">Meta.source</a> 046 */ 047@Interceptor 048public class CaptureResourceSourceFromHeaderInterceptor { 049 050 private static final Logger ourLog = LoggerFactory.getLogger(CaptureResourceSourceFromHeaderInterceptor.class); 051 private final FhirContext myFhirContext; 052 private String myHeaderName; 053 054 public CaptureResourceSourceFromHeaderInterceptor(FhirContext theFhirContext) { 055 myFhirContext = theFhirContext; 056 setHeaderName(Constants.HEADER_REQUEST_SOURCE); 057 } 058 059 /** 060 * Provides the header name to examine in incoming requests. Default is {@link ca.uhn.fhir.rest.api.Constants#HEADER_REQUEST_SOURCE "X-Request-Source"}. 061 */ 062 @SuppressWarnings("WeakerAccess") 063 public String getHeaderName() { 064 return myHeaderName; 065 } 066 067 /** 068 * Provides the header name to examine in incoming requests. Default is {@link ca.uhn.fhir.rest.api.Constants#HEADER_REQUEST_SOURCE "X-Request-Source"}. 069 */ 070 @SuppressWarnings("WeakerAccess") 071 public void setHeaderName(String theHeaderName) { 072 myHeaderName = theHeaderName; 073 } 074 075 @Hook(Pointcut.SERVER_INCOMING_REQUEST_PRE_HANDLED) 076 public void extractSource(RequestDetails theRequestDetails) { 077 IBaseResource resource = theRequestDetails.getResource(); 078 if (resource != null) { 079 String requestSource = theRequestDetails.getHeader(getHeaderName()); 080 if (isNotBlank(requestSource)) { 081 ourLog.trace("Setting Meta.source to \"{}\" because of header \"{}\"", requestSource, getHeaderName()); 082 MetaUtil.setSource(myFhirContext, resource, requestSource); 083 } 084 } 085 } 086}