001package ca.uhn.fhir.rest.server.tenant; 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 023import ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.i18n.HapiLocalizer; 025import ca.uhn.fhir.rest.api.server.RequestDetails; 026import ca.uhn.fhir.rest.server.RestfulServer; 027import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 028import ca.uhn.fhir.util.UrlPathTokenizer; 029import org.apache.commons.lang3.Validate; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033import static org.apache.commons.lang3.StringUtils.defaultIfBlank; 034 035/** 036 * This class is a tenant identification strategy which assumes that a single path 037 * element will be present between the server base URL and individual request. 038 * <p> 039 * For example, 040 * with this strategy enabled, given the following URL on a server with base URL <code>http://example.com/base</code>, 041 * the server will extract the <code>TENANT-A</code> portion of the URL and use it as the tenant identifier. The 042 * request will then proceed to read the resource with ID <code>Patient/123</code>. 043 * </p> 044 * <p> 045 * GET http://example.com/base/TENANT-A/Patient/123 046 * </p> 047 */ 048public class UrlBaseTenantIdentificationStrategy implements ITenantIdentificationStrategy { 049 050 private static final Logger ourLog = LoggerFactory.getLogger(UrlBaseTenantIdentificationStrategy.class); 051 052 @Override 053 public void extractTenant(UrlPathTokenizer theUrlPathTokenizer, RequestDetails theRequestDetails) { 054 String tenantId = null; 055 if (theUrlPathTokenizer.hasMoreTokens()) { 056 tenantId = defaultIfBlank(theUrlPathTokenizer.nextTokenUnescapedAndSanitized(), null); 057 ourLog.trace("Found tenant ID {} in request string", tenantId); 058 theRequestDetails.setTenantId(tenantId); 059 } 060 061 if (tenantId == null) { 062 HapiLocalizer localizer = theRequestDetails.getServer().getFhirContext().getLocalizer(); 063 throw new InvalidRequestException(Msg.code(307) + localizer.getMessage(RestfulServer.class, "rootRequest.multitenant")); 064 } 065 } 066 067 @Override 068 public String massageServerBaseUrl(String theFhirServerBase, RequestDetails theRequestDetails) { 069 Validate.notNull(theRequestDetails.getTenantId(), "theTenantId is not populated on this request"); 070 return theFhirServerBase + '/' + theRequestDetails.getTenantId(); 071 } 072 073}