001package ca.uhn.fhir.rest.server.interceptor.partition;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2023 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.interceptor.api.Hook;
025import ca.uhn.fhir.interceptor.api.Interceptor;
026import ca.uhn.fhir.interceptor.api.Pointcut;
027import ca.uhn.fhir.interceptor.model.RequestPartitionId;
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
030import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
031import ca.uhn.fhir.rest.server.tenant.ITenantIdentificationStrategy;
032
033import javax.annotation.Nonnull;
034
035import static org.apache.commons.lang3.StringUtils.isBlank;
036
037/**
038 * This interceptor uses the request tenant ID (as supplied to the server using
039 * {@link ca.uhn.fhir.rest.server.RestfulServer#setTenantIdentificationStrategy(ITenantIdentificationStrategy)}
040 * to indicate the partition ID. With this interceptor registered, The server treats the tenant name
041 * supplied by the {@link ITenantIdentificationStrategy tenant identification strategy} as a partition name.
042 * <p>
043 * Partition names (aka tenant IDs) must be registered in advance using the partition management operations.
044 * </p>
045 *
046 * @since 5.0.0
047 */
048@Interceptor
049public class RequestTenantPartitionInterceptor {
050
051        @Hook(Pointcut.STORAGE_PARTITION_IDENTIFY_ANY)
052        public RequestPartitionId partitionIdentifyCreate(RequestDetails theRequestDetails) {
053                return extractPartitionIdFromRequest(theRequestDetails);
054        }
055
056        @Nonnull
057        protected RequestPartitionId extractPartitionIdFromRequest(RequestDetails theRequestDetails) {
058
059                // We will use the tenant ID that came from the request as the partition name
060                String tenantId = theRequestDetails.getTenantId();
061                if (isBlank(tenantId)) {
062                        if (theRequestDetails instanceof SystemRequestDetails) {
063                                SystemRequestDetails requestDetails = (SystemRequestDetails) theRequestDetails;
064                                if (requestDetails.getRequestPartitionId() != null) {
065                                        return requestDetails.getRequestPartitionId();
066                                }
067                                return RequestPartitionId.defaultPartition();
068                        }
069                        throw new InternalErrorException(Msg.code(343) + "No partition ID has been specified");
070                }
071
072                return RequestPartitionId.fromPartitionName(tenantId);
073        }
074
075
076}